-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·408 lines (357 loc) · 15 KB
/
sync.sh
File metadata and controls
executable file
·408 lines (357 loc) · 15 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env bash
# COPYRIGHT 2023 Ryan Peters
# This script version is licensed for your use via the terms of LGPL v2.1
# See: https://github.com/sloshy/dumb-sync/LICENSE
set -e
[ -f last_run_old.txt ] && rm last_run_old.txt
[ -f last_run.txt ] && mv last_run.txt last_run_old.txt
[ -f last_run_secs.txt ] || echo "0" >last_run_secs.txt
lastRunSecs=$(cat last_run_secs.txt)
syncStartTime=$(date +%s)
#Detect sudo
needsSudo=false
while read -r obj; do
needsSudo=$(echo "$obj" | jq -r '.sudo // false')
[[ "$needsSudo" = "true" ]] && break
done < <(
jq -c '.transformations[]' sync.json
jq -c '.cleanup_transformations[]' sync.json
)
if [[ "$needsSudo" == "true" ]]; then
echo "Sudo requirement detected"
echo -n "Enter Sudo password: "
read -rs sudoPass
echo ""
fi
logDirConf=$(jq -r '.log_dir // "./"' sync.json)
logDir=${logDirConf%/}
fileListDirConf=$(jq -r ".file_list_dir // \"$logDir\"" sync.json)
fileListDir=${fileListDirConf%/}
syncOffset=$(jq -r '.sync_time_offset_seconds // 0' sync.json)
defaultRemoteUrl=$(jq -r '.remote_url' sync.json)
preexistingFilesDir=$(mktemp -d)
trap 'rm -rf -- "$preexistingFilesDir"' EXIT
while read -r obj; do
remoteUrlName=$(echo "$obj" | jq -r '.url_name')
if [[ "$remoteUrlName" == "null" ]]; then
remoteUrl="$defaultRemoteUrl"
remoteUrlName="(default)"
else
while read -r remoteUrlConf; do
confName=$(echo "$remoteUrlConf" | jq -r '.name')
if [[ "$confName" == "$remoteUrlName" ]]; then
remoteUrl=$(echo "$remoteUrlConf" | jq -r '.url')
break
fi
done < <(jq -c '.remote_urls[] // []' sync.json)
fi
if [[ "$remoteUrl" == "null" ]]; then
echo "ERROR: Remote URL not specified. Add a value to the 'remote_url' key in 'sync.json' that is a valid rsync data source (SSH, rsync, another local folder, etc.)"
exit 1
fi
remote=$(echo "$obj" | jq -r '.remote')
localDirConf=$(echo "$obj" | jq -r '.local')
localDir=${localDirConf%/}
disabled=$(echo "$obj" | jq -r '.disabled // false')
if [[ "$disabled" == "true" ]]; then
echo "Skipping disabled config (Remote: $remote) (Local: $localDirConf)" | tee -a "$logDir"/last_run.txt
continue
fi
maxSizeBytes=$(echo "$obj" | jq -r '.max_size_bytes')
minSizeBytes=$(echo "$obj" | jq -r '.min_size_bytes')
fileListName=${localDir//\//_} # Replaces forward slashes with underscores
transformCount=$(echo "$obj" | jq -r '.transforms // [] | length')
transforms=$(echo "$obj" | jq -c '.transforms // []' | sed -e 's/\[\]/\(none\)/' -e's/"//g' -e 's/,/ -> /g' -e 's/\[//' -e 's/\]//')
cleanupCount=$(echo "$obj" | jq -r '.cleanup // [] | length')
cleanups=$(echo "$obj" | jq -c '.cleanup // []' | sed -e 's/\[\]/\(none\)/' -e's/"//g' -e 's/,/ -> /g' -e 's/\[//' -e 's/\]//')
comparison=$(echo "$obj" | jq -r '.comparison')
rmMissingFiles=$(echo "$obj" | jq -r '.rm_missing // false')
exclude=" "
include=" "
while read -r excl; do
# Ensure dollar signs and double quotes are escaped
# exclEsc=$(echo "$excl" | sed -e "s/\$/\\\$/g" -e "s/\"/\\\"/g")
exclude="$exclude--exclude=\"$excl\" "
done < <(echo "$obj" | jq -r '.exclude // [] | .[]')
while read -r incl; do
# Ensure dollar signs and double quotes are escaped
# inclEsc=$(echo "$incl" | sed -e "s/\$/\\\$/g" -e "s/\"/\\\"/g")
include="$include--include=\"$incl\" "
done < <(echo "$obj" | jq -r '.include // [] | .[]')
preexistingFilesList="$preexistingFilesDir"/"$fileListName"
touch "$preexistingFilesList"
echo "= = = = = = = = = =" | tee -a "$logDir"/last_run.txt
echo " " | tee -a "$logDir"/last_run.txt
echo "Syncing remote ($remoteUrlName -- $remote), To Local: ($localDirConf)" | tee -a "$logDir"/last_run.txt
mkdir -p "$localDir"
echo "Transform steps: $transforms" | tee -a "$logDir"/last_run.txt
echo "Cleanup steps: $cleanups" | tee -a "$logDir"/last_run.txt
echo "Include args: $include" | tee -a "$logDir"/last_run.txt
echo "Exclude args: $exclude" | tee -a "$logDir"/last_run.txt
if [[ "$maxSizeBytes" != "null" ]]; then
echo "Max size: $maxSizeBytes bytes" | tee -a "$logDir"/last_run.txt
else
echo "Max size not specified." | tee -a "$logDir"/last_run.txt
fi
if [[ "$minSizeBytes" != "null" ]]; then
echo "Min size: $minSizeBytes bytes" | tee -a "$logDir"/last_run.txt
else
echo "Min size not specified." | tee -a "$logDir"/last_run.txt
fi
if [[ "$comparison" == "null" ]] && [[ "$transformCount" -eq 0 ]] && [[ "$cleanupCount" -eq 0 ]] && [[ "$rmMissingFiles" != "true" ]]; then
echo "Skipping file list (no comparisons, transformations, cleanup, or removals specified)" | tee -a "$logDir"/last_run.txt
else
echo "Getting file list..." | tee -a "$logDir"/last_run.txt
eval "rsync --no-motd --list-only$include$exclude\"$remoteUrl$remote\" \"$localDir/\" 2>&1 | tee -a \"$logDir\"/last_run.txt > \"$fileListDir/$fileListName-file-list.txt\""
# File list sanity check
listSecongLine=$(head -n 2 "$fileListDir/$fileListName-file-list.txt" | tail -n 1)
fileLineRegex="\b([0-9]{4}/[0-9]{2}/[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) (.*)\b"
if ! [[ "$listSecongLine" =~ $fileLineRegex ]]; then
echo "Error getting file list. See '$fileListDir/$fileListName-file-list.txt' for more details." | tee -a "$logDir"/last_run.txt
exit 1
fi
# File existence / removal check
if [[ "$comparison" != "null" ]] || [[ "$rmMissingFiles" == "true" ]]; then
echo "Comparing file list to existing files..." | tee -a "$logDir"/last_run.txt
fileDel=0
for existFile in "$localDir"/*; do
[[ -f "$existFile" ]] || break
fileExists=false
fileUpdated=false
existFileBase=$(basename -- "$existFile")
if [ "$comparison" != "null" ]; then
# Attempt to compare with a user-defined comparison script
cmd=""
while read -r compObj; do
compName=$(echo "$compObj" | jq -r '.name')
compScript=$(echo "$compObj" | jq -r '.script')
if [[ "$comparison" == "$compName" ]]; then
cmd="$compScript"
while read -r compParam; do
case $compParam in
"<outdir>")
cmd="$cmd \"$localDir\"/"
;;
"<outdir_abs>")
cmd="$cmd \"$(pwd)/$localDir\""
;;
"<file_list>")
cmd="$cmd \"$fileListDir/$fileListName-file-list.txt\""
;;
"<filename_local>")
existFileClean=$(echo "$existFile" | sed -e 's/\"/\\"/g' -e 's/\$/\\$/g')
cmd="$cmd \"$existFileClean\""
;;
"<filename_local_base>")
existFileBaseClean=$(echo "$existFileBase" | sed -e 's/\"/\\"/g' -e 's/\$/\\$/g')
cmd="$cmd \"$existFileBaseClean\""
;;
"<last_sync_time_secs>")
cmd="$cmd \"$lastRunSecs\""
;;
"<current_time_secs>")
cmd="$cmd \"$(date +%s)\""
;;
"<sync_offset_secs>")
cmd="$cmd \"$syncOffset\""
;;
"<arg:"*)
trueArg=$(echo "$compParam" | sed -e "s/<arg://" -e "s/>//")
getArg=$(echo "$obj" | jq -r ".$trueArg")
cmd="$cmd \"$getArg\""
;;
*)
cmd="$cmd \"$compParam\""
;;
esac
done < <(echo "$compObj" | jq -r '.params[]')
break
fi
done < <(jq -c '.comparisons[] // []' sync.json)
if [[ -z "$cmd" ]]; then
echo "ERROR: Comparison script not found. Exiting..." | tee -a "$logDir"/last_run.txt
exit 1
fi
set +e
cmdResult=$(eval "$cmd")
set -e
case $cmdResult in
"missing")
# Do nothing at this time
# If files are set to be removed, this will be removed later, else included in preexistingFilesList
;;
"current"*)
# We want to exclude files that are 'current' but probably transformed
remoteFileName=${cmdResult#current }
# Ensure dollar signs and double quotes are escaped
# exclEsc=$(echo "$remoteFileName" | sed -e "s/\$/\\\$/g" -e "s/\"/\\\"/g")
remoteFileNameClean=$(echo "$remoteFileName" | sed -e 's/\[/\\[/g' -e 's/\]/\\]/g' -e 's/\$/\\$/g')
if [[ "$remoteFileNameClean" != "$remoteFileName" ]]; then
echo "File '$remoteFileName' is being excluded from results as '$remoteFileNameClean' due to rsync pattern rules." | tee -a "$logDir"/last_run.txt
fi
exclude="$exclude--exclude=\"$remoteFileNameClean\" "
fileExists=true
echo "$existFile" >>"$preexistingFilesList"
;;
"updated")
fileUpdated="true"
;;
"transform")
fileExists=true
echo "File '$existFile' is present but not transformed yet. Will transform after sync." | tee -a "$logDir"/last_run.txt
;;
*)
echo "Comparison script '$cmd' had invalid response '$cmdResult'. Exiting..." | tee -a "$logDir"/last_run.txt
exit 1
;;
esac
elif [[ "$rmMissingFiles" == "true" ]]; then
# Default comparison logic
# Saving some cycles by relying on rsync's built-in file updating
# This means we only need to worry about missing files, as updated files will be handled automatically
if grep -Fq "$existFileBase" "$fileListDir/$fileListName-file-list.txt"; then
fileExists=true
fi
fi
if [[ "$fileUpdated" == "true" ]]; then
echo "File $existFile is updated. Deleting and redownloading." | tee -a "$logDir"/last_run.txt
rm -f "$existFile"
fileDel=$((fileDel + 1))
elif [[ "$fileExists" == "false" ]] && [[ $rmMissingFiles = true ]]; then
echo "Deleting nonexistent file: $existFile" | tee -a "$logDir"/last_run.txt
rm -f "$existFile"
fileDel=$((fileDel + 1))
elif [[ "$fileExists" == "false" ]]; then
# For the case where files are "missing" but present locally, but not set to be removed, they're marked preexisting
echo "$existFile" >>"$preexistingFilesList"
fi
done
[[ $rmMissingFiles = true ]] && echo "Deleted $fileDel files" | tee -a "$logDir"/last_run.txt
else
echo "Skipping file check (no comparison or 'rm_missing' option specified)..." | tee -a "$logDir"/last_run.txt
fi
fi
#Sync
if [[ "$maxSizeBytes" != "null" ]]; then
maxSizeArg=" --max-size=\"$maxSizeBytes\""
else
maxSizeArg=""
fi
if [[ "$minSizeBytes" != "null" ]]; then
minSizeArg=" --min-size=\"$minSizeBytes\""
else
minSizeArg=""
fi
eval "rsync -hav$maxSizeArg$minSizeArg$include$exclude\"$remoteUrl$remote/\" \"$localDir/\" | tee -a \"$logDir\"/last_run.txt"
#Transforms
[[ "$transformCount" -gt 0 ]] && for f in "$localDir"/*; do
[[ -f "$f" ]] || break
# Checks if the file is marked as preexisting and should not be transformed
if ! grep -Fq "$f" "$preexistingFilesList"; then
while read -r tName; do
while read -r tObj; do
name=$(echo "$tObj" | jq -r '.name')
if [ "$tName" == "$name" ]; then
script=$(echo "$tObj" | jq -r '.script')
cmd="$script"
while read -r param; do
case $param in
"<outdir>")
cmd="$cmd \"$localDir\"/"
;;
"<outdir_abs>")
cmd="$cmd \"$(pwd)/$localDir\""
;;
"<file_list>")
cmd="$cmd \"$fileListDir/$fileListName-file-list.txt\""
;;
"<filename_remote>")
fClean=$(echo "$f" | sed -e 's/\"/\\"/g' -e 's/\$/\\$/g')
cmd="$cmd \"$fClean\""
;;
"<filename_remote_base>")
basefile=$(basename -- "$f")
basefileClean=$(echo "$basefile" | sed -e 's/\"/\\"/g' -e 's/\$/\\$/g')
cmd="$cmd \"$basefileClean\""
;;
"<arg:"*)
trueArg=$(echo "$param" | sed -e "s/<arg://" -e "s/>//")
getArg=$(echo "$obj" | jq -r ".$trueArg")
cmd="$cmd \"$getArg\""
;;
"<last_sync_time_secs>")
cmd="$cmd \"$lastRunSecs\""
;;
"<current_time_secs>")
cmd="$cmd \"$(date +%s)\""
;;
"<sync_offset_secs>")
cmd="$cmd \"$syncOffset\""
;;
*)
cmd="$cmd \"$param\""
;;
esac
done < <(echo "$tObj" | jq -r '.params[]')
needsSudo=$(echo "$tObj" | jq -r '.sudo // false')
[[ "$needsSudo" == "true" ]] && cmd="echo $sudoPass | $cmd"
eval "$cmd" | tee -a "$logDir"/last_run.txt
if [[ "${PIPESTATUS[0]}" -gt 0 ]]; then
exit 1
fi
fi
done < <(jq -c '.transformations[]' sync.json)
done < <(echo "$obj" | jq -r '.transforms[]')
fi
done
#Cleanup
if [[ "$cleanupCount" -gt 0 ]]; then
echo "Cleaning up remaining files..." | tee -a "$logDir"/last_run.txt
while read -r cName; do
while read -r cObj; do
name=$(echo "$cObj" | jq -r '.name')
if [ "$cName" = "$name" ]; then
script=$(echo "$cObj" | jq -r '.script')
cmd="$script"
while read -r param; do
case $param in
"<outdir>")
cmd="$cmd \"$localDir\""
;;
"<outdir_abs>")
cmd="$cmd \"$(pwd)/$localDir\""
;;
"<file_list>")
cmd="$cmd \"$fileListDir/$fileListName-file-list.txt\""
;;
"<last_sync_time_secs>")
cmd="$cmd \"$lastRunSecs\""
;;
"<current_time_secs>")
cmd="$cmd \"$(date +%s)\""
;;
"<sync_offset_secs>")
cmd="$cmd \"$syncOffset\""
;;
"<arg:"*)
trueArg=$(echo "$param" | sed -e "s/<arg://" -e "s/>//")
getArg=$(echo "$obj" | jq -r ".$trueArg")
cmd="$cmd \"$getArg\""
;;
*)
cmd="$cmd \"$param\""
;;
esac
done < <(echo "$cObj" | jq -r '.params[]')
needsSudo=$(echo "$cObj" | jq -r '.sudo // false')
[[ "$needsSudo" = "true" ]] && cmd="echo $sudoPass | $cmd"
eval "$cmd" | tee -a "$logDir"/last_run.txt
if [[ "${PIPESTATUS[0]}" -gt 0 ]]; then
exit 1
fi
fi
done < <(jq -c '.cleanup_transformations[]' sync.json)
done < <(echo "$obj" | jq -r '.cleanup[]')
fi
done < <(jq -c '.configs[]' sync.json)
echo "$syncStartTime" >"$logDir/last_run_secs.txt"