4 REMOTE_BASE=/tmp/backmeupscotty
5 ARCHIVE_KEEPNBACKUPS=30
8 if [ $(pidof -x $(basename $0) | wc -w) -gt 2 ]; then
9 echo BACKMEUPSCOTTY: Another instance of $(basename $0) is already running! >&2
13 while getopts ":q" opt; do
21 function grepbackups {
22 ssh $REMOTE_HOST "ls $REMOTE_BASE/$1" | grep -E '[0-9]+-[0-9]+'
28 echo BACKMEUPSCOTTY: Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp
30 if [ ! -d "$1" ]; then
31 echo BACKMEUPSCOTTY: Source dir $1 does not exist. Not syncing! >&2
35 if [ $(ls -A "$1" | wc -l) -eq 0 ]; then
36 echo BACKMEUPSCOTTY: Source dir $1 is empty. Not syncing! >&2
40 dir_current=$REMOTE_BASE/$2/current
41 dir_incomplete=$REMOTE_BASE/$2/incomplete
42 dir_timestamped=$REMOTE_BASE/$2/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
47 rsync_exclude=$(eval echo --exclude={$3} | tr -d {})
50 if (ssh $REMOTE_HOST '[ ! -d '$REMOTE_BASE/$2' ]'); then
51 echo BACKMEUPSCOTTY: Creating destination directory $REMOTE_HOST:$REMOTE_BASE/$2
52 ssh $REMOTE_HOST "mkdir $REMOTE_BASE/$2"
55 if (ssh $REMOTE_HOST '[ -d '$REMOTE_BASE/$2/incomplete' ]'); then
56 echo BACKMEUPSCOTTY: Continuing old incomplete backup >&2
59 echo BACKMEUPSCOTTY: Starting rsync
61 -v -aHAX --numeric-ids --delete --delete-excluded \
62 --link-dest=$dir_current \
64 $1/ $REMOTE_HOST:$dir_incomplete/
67 echo BACKMEUPSCOTTY: Timestamping completed backup and linking to current backup
69 "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $dir_timestamped $dir_current"
72 while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
73 oldestbackup=$(grepbackups $2 | head -1)
74 oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
76 if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
77 echo BACKMEUPSCOTTY: Removing old backup $oldestbackup
78 ssh $REMOTE_HOST rm -r "$REMOTE_BASE/$2/$oldestbackup"