shell/tools/inotifywait-cp.sh

58 lines
1.8 KiB
Bash
Raw Normal View History

2022-09-27 23:41:07 +00:00
#!/bin/bash
# My use case:
# syncthing synchronizes ALL changes in DCIM directory on my android to PC.
# I wanted files to be copied somewhere else on my PC to stay forever, so I
# could sort them later and safely free some space on mobile without loss.
# Also I wish to have some stupid log with history of such events.
# inotify-tools package must be installed!
# CHANGE THIS PARAMETERS to ones you needed
dir_src="$HOME/Syncthing/Mobile/Camera"
dir_dest="$HOME/some/safe/place"
dir_logs="$HOME/inotifywait-cp-logs"
regexp="[0-9]{8}_[0-9]{6}.*\.(jpg|mp4|gif)"
2022-10-01 05:17:59 +00:00
mkdir -p "$dir_dest" "$dir_logs"
print() {
echo -e "[`date '+%H:%M:%S'`] $*" \
| tee -a "$dir_logs/`date '+%Y%m%d'`.log"
}
copy () {
if [ -f "$dir_dest/$1" ]; then
print "SKIPPED:\t$dir_dest/$1"
else
cp "$dir_src/$1" "$dir_dest/$1"
print "COPIED:\t$dir_src/$1 => $dir_dest/$1"
fi
}
print "START\t========================="
2022-09-27 23:41:07 +00:00
2022-10-01 05:17:59 +00:00
# First, try to backup files synced since last exec of this script
ls -1 "$dir_src" \
| grep -E "^${regexp}$" \
| while read filename; do copy "$filename"; done
# Next, run inotifywait against source directory with args:
2022-09-27 23:41:07 +00:00
# --quiet -- print less (only print events)
# --monitor -- don't stop after first event (like infinite loop)
# --event -- first syncthing creates hidden file to write data into
# then renames it according to source file name, so here
# we listen to MOVED_TO event to catch final filename
# --format %f -- print only filename
# --include -- filename regexp to catch event from, ensure your $regexp
2022-10-01 05:17:59 +00:00
# is correct or remove line 53 to catch synced ALL files
2022-09-27 23:41:07 +00:00
inotifywait \
--quiet \
--monitor \
--event moved_to \
--format %f \
--include "$regexp" \
"$dir_src" \
2022-10-01 05:17:59 +00:00
| while read filename; do copy "$filename"; done
print "FINISH\t========================="