4 REMOTE_BASE=/tmp/backmeupscotty
5 ARCHIVE_KEEPNBACKUPS=30
8 while getopts ":q" opt; do
16 function grepbackups {
17 ssh $REMOTE_HOST "ls $REMOTE_BASE/$1" | grep -E '[0-9]+-[0-9]+'
23 echo BACKMEUPSCOTTY: Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp
25 dir_current=$REMOTE_BASE/$2/current
26 dir_incomplete=$REMOTE_BASE/$2/incomplete
27 dir_timestamped=$REMOTE_BASE/$2/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
32 rsync_exclude=$(eval echo --exclude={$3} | tr -d {})
35 if (ssh $REMOTE_HOST '[ ! -d '$REMOTE_BASE/$2' ]'); then
36 echo BACKMEUPSCOTTY: Creating destination directory $REMOTE_HOST:$REMOTE_BASE/$2
37 ssh $REMOTE_HOST "mkdir $REMOTE_BASE/$2"
40 echo BACKMEUPSCOTTY: Starting rsync
42 -v -aHAX --numeric-ids --delete --delete-excluded \
43 --link-dest=$dir_current \
45 $1/ $REMOTE_HOST:$dir_incomplete/
48 echo BACKMEUPSCOTTY: Timestamping completed backup and linking to current backup
50 "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $dir_timestamped $dir_current"
53 while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
54 oldestbackup=$(grepbackups $2 | head -1)
55 oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
57 if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
58 echo BACKMEUPSCOTTY: Removing old backup $oldestbackup
59 ssh $REMOTE_HOST rm -r "$REMOTE_BASE/$2/$oldestbackup"