-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesync.sh
More file actions
executable file
·83 lines (68 loc) · 2.73 KB
/
filesync.sh
File metadata and controls
executable file
·83 lines (68 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# A variable to keep track of the scripts source folder
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Fetching config variables from config-file
source "$SCRIPT_PATH/config"
# Function to check if remote system has files that do not exist locally
check_remote_files_not_in_local() {
missing_on_local=$(rsync -arvh --dry-run --ignore-existing "$remotehost:$remotedir" "$localdir" 2>&1 | grep -E '^>f')
if [ -n "$missing_on_local" ]; then
echo "WARNING! The following files exist on the remote system but are NOT present locally:"
echo "$missing_on_local"
echo ""
read -p "Do you want to delete these files on the remote system? (y/n): " choice
case "$choice" in
y|Y ) echo "Proceeding with sync..."; return 0;;
n|N ) echo "Sync canceled to prevent deletion."; exit 1;;
* ) echo "Invalid choice. Sync canceled."; exit 1;;
esac
fi
}
# Function to check for deletions before syncing
confirm_deletion() {
src="$1"
dest="$2"
deletions=$(rsync -arvh --progress --delete --dry-run "$src" "$dest" 2>&1 | grep -oP '(?<=deleting ).*')
if [ -n "$deletions" ]; then
echo "WARNING! Sync will delete data in $dest:"
echo "$deletions"
echo ""
read -p "Do you want to proceed with deletion? (y/n): " choice
case "$choice" in
y|Y ) echo "Proceeding with sync..."; return 0;;
n|N ) echo "Sync canceled."; exit 1;;
* ) echo "Invalid choice. Sync canceled."; exit 1;;
esac
fi
}
# Display help if -h or --help is passed as an argument
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: filesync [OPTION]"
echo "Safe file syncronization between local and a remote host using rsync"
echo ""
echo "Optional arguments:"
echo "push Pushes data to the remote system"
echo "pull Pulls data from the remote system"
echo "-h, --help Displays this message"
echo ""
exit 0
fi
# Push data to the remote system
if [ "$1" = "push" ]; then
# Ensure we don't blindly delete remote files
check_remote_files_not_in_local
# Check what will be deleted on remote
confirm_deletion "$localdir" "$remotehost:$remotedir"
rsync -arvh --progress --delete "$localdir" "$remotehost:$remotedir"
exit 0
fi
# Pull data from the remote system
if [ "$1" = "pull" ]; then
# Check what will be deleted locally
confirm_deletion "$remotehost:$remotedir" "$localdir"
rsync -arvh --progress --delete "$remotehost:$remotedir" "$localdir"
exit 0
fi
# If no valid argument is provided, display a message
echo "Error: Invalid option. Use -h or --help for usage information."
exit 1