-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-directory.sh
More file actions
executable file
·133 lines (112 loc) · 3.88 KB
/
sync-directory.sh
File metadata and controls
executable file
·133 lines (112 loc) · 3.88 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
source config_lib.sh
# TEST:
# cd path/to/script
# ./sync-directory.sh "/mnt/user/source_path/Filme" "user@host:/home/user/dest_path/Filme" "SSH_PORT" "local/path/to/EXCLUDE_FILE.txt"
# ./sync-directory.sh "user@host:/path/to/source" "/path/to/destination" "SSH_PORT" "local/path/to/EXCLUDE_FILE.txt"
# Exit on any error
set -e
# Function for error handling
error_exit() {
echo "Error: $1" >&2
exit 1
}
# Funktion zur Überprüfung, ob es sich um einen Remote-Pfad handelt
is_remote_path() {
[[ "$1" =~ ^[^@]+@[^:]+: ]]
}
# Check if required arguments are provided
if [ "$#" -lt 4 ]; then
error_exit "Usage: $0 <src_path> <remote_path> <ssh_port> <exclude_file>"
fi
SRC_PATH="$1"
DEST_PATH="$2"
SSH_PORT="$3"
EXCLUDE_FILE="$4"
SSH_OPTS="$SSH_OPTS"
RSYNC_OPTS="-av --chown=$USERID:$GROUPID -q --partial-dir=.rsync-partials --prune-empty-dirs" # -av -q --partial --info=progress2 -q --no-o --no-g
SYNC_ENTRIES_MAX_LENGTH=$(get_sync_entries_max_length)
#echo "source: $SRC_PATH"
#echo "destination: $DEST_PATH"
#echo "ssh-port: $SSH_PORT"
#echo "exclude-file: $EXCLUDE_FILE"
#echo "$SRC_PATH -> $DEST_PATH"
printf "%-*s\n" "$SYNC_ENTRIES_MAX_LENGTH" "$SRC_PATH -> $DEST_PATH"
# Verify local path exists and is writable
if ! is_remote_path "$SRC_PATH"; then
if [ ! -d "$SRC_PATH" ]; then
error_exit "SRC_PATH $SRC_PATH does not exist or is not a directory"
fi
fi
if ! is_remote_path "$DEST_PATH"; then
if [ ! -d "$DEST_PATH" ]; then
error_exit "DEST_PATH $DEST_PATH does not exist or is not a directory"
fi
if [ ! -w "$DEST_PATH" ]; then
error_exit "DEST_PATH $DEST_PATH is not writable"
fi
fi
# Check if exclude file exists, if not create it
if [ ! -f "$EXCLUDE_FILE" ]; then
touch "$EXCLUDE_FILE" || error_exit "Failed to create $EXCLUDE_FILE"
fi
# Get list of all files and directories from remote, handling spaces correctly
if is_remote_path "$SRC_PATH"; then
# Remote-Pfad
user_host="${SRC_PATH%%:*}" # Extrahiere "user@host"
remote_path="${SRC_PATH#*:}" # Extrahiere den eigentlichen Pfad
#echo "user_host: $user_host - remote_path: $remote_path"
ITEMS=$(ssh -p "$SSH_PORT" "$user_host" "find \"$remote_path\" -type f") || error_exit "Failed to get remote directory listing for $path"
else
# Lokaler Pfad
ITEMS=$(find "$SRC_PATH" -type f) || error_exit "Failed to get local directory listing"
fi
# Retry loop for rsync
SUCCESS=0
MAX_ATTEMPTS=10
# rsync -av --partial --partial-dir=.rsync-partials --info=progress2 \
for (( ATTEMPT=1; ATTEMPT<=MAX_ATTEMPTS; ATTEMPT++ ))
do
if [ "$ATTEMPT" -eq 2 ]; then
echo -n "failed - try again.."
elif [ "$ATTEMPT" -gt 2 ]; then
echo -n "."
fi
if [ "$ATTEMPT" -eq 10 ]; then
echo -n " (Attempt $ATTEMPT of $MAX_ATTEMPTS)"
fi
rsync $RSYNC_OPTS \
--rsh="ssh -p $SSH_PORT $SSH_OPTS" \
--exclude-from="$EXCLUDE_FILE" \
"$SRC_PATH/" \
"$DEST_PATH"
if [ $? -eq 0 ]; then
SUCCESS=1
# echo "rsync done."
break
else
echo "Rsync failed, retrying in 2 minutes..."
sleep 120
fi
done
#echo "check..."
# Check if rsync was successful for the final actions
if [ $SUCCESS -eq 1 ]; then
diff_count=0
if [ -f "$EXCLUDE_FILE" ]; then
# Vergleiche die Variable mit der Datei und finde neue Dateien
diff_files=$(diff --new-line-format='%L' --unchanged-line-format='' <(echo "$ITEMS") "$EXCLUDE_FILE") || :
# Zähle die neuen Dateien
diff_count=$(echo "$diff_files" | grep -v '^$' | wc -l) || echo "diff_count failed"
fi
if [ "$diff_count" -gt 0 ]; then
echo "Success: $diff_count files. Update $EXCLUDE_FILE" || echo "Success"
else
#echo "Success"
true
fi
echo "$ITEMS" > "$EXCLUDE_FILE" || error_exit "Failed to update Exclude_File"
else
error_exit "Rsync failed after $MAX_ATTEMPTS attempts."
fi
#echo "return"