-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivesync
executable file
·75 lines (61 loc) · 1.94 KB
/
livesync
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
#!/bin/bash
# Default config file
CONFIG_FILE="custom.synconfig"
# Parse command line options for --config or -c flag
while [[ "$#" -gt 0 ]]; do
case $1 in
-c | --config)
CONFIG_FILE="$HOME/bin/$2.synconfig"
shift
;;
-l | --list)
find "$HOME/bin" -name "*.synconfig" ! -name "__template.synconfig" -exec basename {} \; | sed 's/\.synconfig$//'
exit 0
;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
shift
done
# Source the specified config file
source "$CONFIG_FILE"
# Check if LOCAL_DIR exists
if [ ! -d "$LOCAL_DIR" ]; then
echo "Local directory $LOCAL_DIR does not exist."
exit 1
fi
EXCLUDE_OPTIONS=""
for EXCLUDE in $EXCLUSIONS; do
EXCLUDE_OPTIONS+="--exclude '$EXCLUDE' "
done
# Check if sshpass is installed
if ! command -v sshpass &>/dev/null; then
echo "sshpass is not installed. Please install it using 'sudo apt install sshpass' or 'sudo yum install sshpass'."
exit 1
fi
# Check if inotifywait is installed
if ! command -v inotifywait &>/dev/null; then
echo "inotifywait is not installed. Please install it using 'sudo apt install inotify-tools' or 'sudo yum install inotify-tools'."
exit 1
fi
RSYNC_COMMAND="sshpass -p '$REMOTE_PASSWORD' rsync -azt --checksum --delete '$LOCAL_DIR/' '$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR' $EXCLUDE_OPTIONS"
# Do initial sync
echo -n "Initial sync..."
eval $RSYNC_COMMAND
echo -e "complete.\n"
# Monitor the local directory for changes and sync to the remote directory
LAST_SYNC_FILE=""
LAST_SYNC_TIME=0
DEBOUNCE_INTERVAL=2 # seconds
inotifywait -m -r -e modify,create,delete,move "$LOCAL_DIR" --format '%w%f' | while read FILE; do
CURRENT_TIME=$(date +%s)
# Debounce events which are essentially dupes. It happens...
if [[ "$FILE" != "$LAST_SYNC_FILE" || $((CURRENT_TIME - LAST_SYNC_TIME)) -ge $DEBOUNCE_INTERVAL ]]; then
eval $RSYNC_COMMAND
echo "$FILE @$(date +%I:%M:%S%p)"
LAST_SYNC_FILE="$FILE"
LAST_SYNC_TIME=$CURRENT_TIME
fi
done