inotifywait-cp.service
· 449 B · SYSTEMD
Bruto
# Daemon file
# Place or symlink it to /etc/systemd/system/inotifywait-cp.service
# Enable and start: sudo systemctl enable --now inotifywait-cp
# Check it: sudo systemctl status inotifywait-cp
[Unit]
Description=Photosync from android
[Service]
Type=simple
Restart=always
# correct these parameters as needed:
User=user
WorkingDirectory=/home/user
ExecStart=bash /home/user/.local/bin/inotifywait-cp.sh
[Install]
WantedBy=network.target
1 | # Daemon file |
2 | # Place or symlink it to /etc/systemd/system/inotifywait-cp.service |
3 | # Enable and start: sudo systemctl enable --now inotifywait-cp |
4 | # Check it: sudo systemctl status inotifywait-cp |
5 | |
6 | [Unit] |
7 | Description=Photosync from android |
8 | |
9 | [Service] |
10 | Type=simple |
11 | Restart=always |
12 | # correct these parameters as needed: |
13 | User=user |
14 | WorkingDirectory=/home/user |
15 | ExecStart=bash /home/user/.local/bin/inotifywait-cp.sh |
16 | |
17 | [Install] |
18 | WantedBy=network.target |
19 |
inotifywait-cp.sh
· 1.9 KiB · Bash
Bruto
#!/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)"
print() {
echo -e "[`date '+%H:%M:%S'`] $*" \
| tee -a "$dir_logs/`date '+%Y%m%d'`.log"
}
copy () {
mkdir -p "$dir_src" "$dir_dest" "$dir_logs"
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
}
mkdir -p "$dir_src" "$dir_dest" "$dir_logs"
print "START\t========================="
# 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:
# --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
# is correct or remove line 56 to catch synced ALL files
inotifywait \
--quiet \
--monitor \
--event moved_to \
--format %f \
--include "$regexp" \
"$dir_src" \
| while read filename; do copy "$filename"; done
print "FINISH\t========================="
1 | #!/bin/bash |
2 | # My use case: |
3 | # syncthing synchronizes ALL changes in DCIM directory on my android to PC. |
4 | # I wanted files to be copied somewhere else on my PC to stay forever, so I |
5 | # could sort them later and safely free some space on mobile without loss. |
6 | # Also I wish to have some stupid log with history of such events. |
7 | |
8 | # inotify-tools package must be installed! |
9 | |
10 | # CHANGE THIS PARAMETERS to ones you needed |
11 | dir_src="$HOME/Syncthing/Mobile/Camera" |
12 | dir_dest="$HOME/some/safe/place" |
13 | dir_logs="$HOME/inotifywait-cp-logs" |
14 | regexp="[0-9]{8}_[0-9]{6}.*\.(jpg|mp4|gif)" |
15 | |
16 | print() { |
17 | echo -e "[`date '+%H:%M:%S'`] $*" \ |
18 | | tee -a "$dir_logs/`date '+%Y%m%d'`.log" |
19 | } |
20 | |
21 | copy () { |
22 | mkdir -p "$dir_src" "$dir_dest" "$dir_logs" |
23 | if [ -f "$dir_dest/$1" ]; then |
24 | print "SKIPPED:\t$dir_dest/$1" |
25 | else |
26 | cp "$dir_src/$1" "$dir_dest/$1" |
27 | print "COPIED:\t$dir_src/$1 => $dir_dest/$1" |
28 | fi |
29 | } |
30 | |
31 | mkdir -p "$dir_src" "$dir_dest" "$dir_logs" |
32 | |
33 | print "START\t=========================" |
34 | |
35 | # First, try to backup files synced since last exec of this script |
36 | ls -1 "$dir_src" \ |
37 | | grep -E "^$regexp$" \ |
38 | | while read filename; do copy "$filename"; done |
39 | |
40 | # Next, run inotifywait against source directory with args: |
41 | # --quiet -- print less (only print events) |
42 | # --monitor -- don't stop after first event (like infinite loop) |
43 | # --event -- first syncthing creates hidden file to write data into |
44 | # then renames it according to source file name, so here |
45 | # we listen to MOVED_TO event to catch final filename |
46 | # --format %f -- print only filename |
47 | # --include -- filename regexp to catch event from, ensure your $regexp |
48 | # is correct or remove line 56 to catch synced ALL files |
49 | |
50 | inotifywait \ |
51 | --quiet \ |
52 | --monitor \ |
53 | --event moved_to \ |
54 | --format %f \ |
55 | --include "$regexp" \ |
56 | "$dir_src" \ |
57 | | while read filename; do copy "$filename"; done |
58 | |
59 | print "FINISH\t=========================" |
60 |