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 dir_current=$REMOTE_BASE/$2/current
31 dir_incomplete=$REMOTE_BASE/$2/incomplete
32 dir_timestamped=$REMOTE_BASE/$2/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
37 rsync_exclude=$(eval echo --exclude={$3} | tr -d {})
40 if (ssh $REMOTE_HOST '[ ! -d '$REMOTE_BASE/$2' ]'); then
41 echo BACKMEUPSCOTTY: Creating destination directory $REMOTE_HOST:$REMOTE_BASE/$2
42 ssh $REMOTE_HOST "mkdir $REMOTE_BASE/$2"
45 if (ssh $REMOTE_HOST '[ -d '$REMOTE_BASE/$2/incomplete' ]'); then
46 echo BACKMEUPSCOTTY: Continuing old incomplete backup >&2
49 echo BACKMEUPSCOTTY: Starting rsync
51 -v -aHAX --numeric-ids --delete --delete-excluded \
52 --link-dest=$dir_current \
54 $1/ $REMOTE_HOST:$dir_incomplete/
57 echo BACKMEUPSCOTTY: Timestamping completed backup and linking to current backup
59 "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $dir_timestamped $dir_current"
62 while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
63 oldestbackup=$(grepbackups $2 | head -1)
64 oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
66 if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
67 echo BACKMEUPSCOTTY: Removing old backup $oldestbackup
68 ssh $REMOTE_HOST rm -r "$REMOTE_BASE/$2/$oldestbackup"