4 REMOTE_BASE=/tmp/backmeupscotty
 
   5 ARCHIVE_KEEPNBACKUPS=30
 
   9     echo $(basename $0) | tr '[:lower:]' '[:upper:]'
 
  16 function scottyerror {
 
  17     echo $(upperme): $@ >&2 
 
  20 if [ $(pidof -x $(basename $0) | wc -w) -gt 2 ]; then 
 
  21     scottyerror Another instance of $(basename $0) is already running!
 
  25 while getopts ":q" opt; do
 
  33 function grepbackups {
 
  34     ssh $REMOTE_HOST "ls $REMOTE_BASE/$1" | grep -E '[0-9]+-[0-9]+'
 
  40     scottyinfo Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp
 
  42     if [ ! -d "$1" ]; then
 
  43         scottyerror Source dir $1 does not exist. Not syncing!
 
  47     if [ $(ls -A "$1" | wc -l) -eq 0 ]; then
 
  48         scottyerror Source dir $1 is empty. Not syncing!
 
  52     dir_current=$REMOTE_BASE/$2/current
 
  53     dir_incomplete=$REMOTE_BASE/$2/incomplete
 
  54     dir_timestamped=$REMOTE_BASE/$2/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
 
  59         rsync_exclude=$(eval echo --exclude={$3} | tr -d {})
 
  62     if (ssh $REMOTE_HOST '[ ! -d '$REMOTE_BASE/$2' ]'); then
 
  63         scottyinfo Creating destination directory $REMOTE_HOST:$REMOTE_BASE/$2
 
  64         ssh $REMOTE_HOST "mkdir $REMOTE_BASE/$2"
 
  67     if (ssh $REMOTE_HOST '[ -d '$REMOTE_BASE/$2/incomplete' ]'); then
 
  68         scottyerror Continuing old incomplete backup
 
  71     scottyinfo Starting rsync
 
  73         -v -aHAX --numeric-ids --delete --delete-excluded \
 
  74         --link-dest=$dir_current \
 
  76         $1/ $REMOTE_HOST:$dir_incomplete/
 
  79         scottyinfo Timestamping completed backup and linking to current backup
 
  81             "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $dir_timestamped $dir_current"
 
  84     while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
 
  85         oldestbackup=$(grepbackups $2 | head -1)
 
  86         oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
 
  88         if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
 
  89             scottyinfo Removing old backup $oldestbackup
 
  90             ssh $REMOTE_HOST rm -r "$REMOTE_BASE/$2/$oldestbackup"