forked from not-implemented/raspicam-timelapse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.sh
executable file
·56 lines (44 loc) · 1.51 KB
/
sync.sh
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
#!/bin/bash
[ -e `dirname $0`/../global/global.inc.sh ] && . `dirname $0`/../global/global.inc.sh
# config parameters
. `dirname $0`/sync.conf || { echo "Error loading config file" >&2; exit 1; }
# default sync function
sync_directory() {
# sync
$SYNC_COMMAND $OPTS $OPTS_DIR "$ROOT_DIR"/ "$SYNC_DEST"
}
sync_file() {
# change to directory so we can sync with relative path (--relative parameter of rsync)
cd "$ROOT_DIR"
# sync
$SYNC_COMMAND $OPTS $OPTS_FILE "$FILE" "$SYNC_DEST"
}
cleanup_empty_directories() {
# cleanup all empty directories except newest (usually the latest/current raspistill directory)
ls -d1 "$ROOT_DIR"/*/ | grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}\.[0-9]{2}\.[0-9]{2}' | head -n-1 | xargs --no-run-if-empty --delim '\n' rmdir --ignore-fail-on-non-empty
}
# overwrite sync function with own implementation
[ -e `dirname $0`/sync.inc.sh ] && . `dirname $0`/sync.inc.sh
# Locking
[ -e `dirname $0`/../global/bash-locking.inc.sh ] && . `dirname $0`/../global/bash-locking.inc.sh
####
logrotate
exec >> $LOG
exec 2>> $LOG
# if used with inotify
## first parameter is the watched directory
## the second the relative path to the file
# if used as batch cronjob
## first parameter is the directory to sync recursively
# strip trailing slashes
ROOT_DIR=`realpath "$1"`
FILE="$2"
if [ $# -eq 1 ] && [ -d "$ROOT_DIR" ]
then
sync_directory
elif [ $# -eq 2 ] && [ -e "$ROOT_DIR"/"$FILE" ]
then
sync_file
fi
# rsync doesn't delete empty directories
cleanup_empty_directories