4 REMOTE_DIR=/tmp/backmeupscotty/test
 
   5 ARCHIVE_KEEPNBACKUPS=30
 
   7 BACKUP_RUNEVERYNTHDAY=1
 
  10 _UPPERME=$(echo $(basename $0) | tr '[:lower:]' '[:upper:]')
 
  13     date +'[%y-%m-%d|%H:%M:%S]'
 
  17     echo $(timestamp) $_UPPERME: $@
 
  21     if [ $_ERROR_ENCOUNTERED -eq 0 ]; then
 
  28 function scottyerror {
 
  30     if [ $_ERROR_ENCOUNTERED -eq 0 ]; then
 
  32         scottyline "Going into verbose mode after error encounter." >&2
 
  40     if [ $sshret -eq 255 ]; then
 
  41         scottyerror "SSH connection failed!"
 
  48 function grepbackups {
 
  49     ssh255 $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+'
 
  52 function isIncomplete {
 
  53     if ( ssh255 $REMOTE_HOST '[ -d '$REMOTE_DIR/incomplete' ]' ); then
 
  61     if [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ]; 
 
  69 function latestTooOld {
 
  70     for oldbackup in $(grepbackups); do
 
  71         tstamp=$(echo $oldbackup | cut -d'-' -f1)
 
  73         if [ $(( $(date +%s) - $tstamp )) -lt \
 
  74             $(( ($BACKUP_RUNEVERYNTHDAY*24+12)*60*60 )) ]
 
  86     scottyinfo "Syncing $SYNC_SRC to $REMOTE_HOST:$REMOTE_DIR @$timestamp."
 
  88     if [ ! -d "$SYNC_SRC" ]; then
 
  89         scottyerror "Source dir $SYNC_SRC does not exist. Not syncing!"
 
  93     if [ $(ls -A "$SYNC_SRC" | wc -l) -eq 0 ]; then
 
  94         scottyerror "Source dir $SYNC_SRC is empty. Not syncing!"
 
  98     dir_current=$REMOTE_DIR/current
 
  99     dir_incomplete=$REMOTE_DIR/incomplete
 
 100     dir_timestamped=$REMOTE_DIR/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
 
 102     if [ -z $SYNC_EXC ]; then
 
 105         rsync_exclude=$(eval echo --exclude={$SYNC_EXC} | tr -d {})
 
 108     if (ssh255 $REMOTE_HOST '[ ! -d '$REMOTE_DIR' ]'); then
 
 109         scottyinfo "Creating destination directory $REMOTE_HOST:$REMOTE_DIR."
 
 110         ssh255 $REMOTE_HOST "mkdir $REMOTE_DIR"
 
 113     if isIncomplete; then
 
 114         scottyerror "Continuing old incomplete backup."
 
 117     scottyinfo "Starting rsync."
 
 119         -v -aHAX --numeric-ids --delete --delete-excluded \
 
 120         --link-dest=$dir_current \
 
 122         $SYNC_SRC/ $REMOTE_HOST:$dir_incomplete/
 
 124     if [ $? -eq 0 ]; then
 
 125         scottyinfo "Timestamping completed backup and linking to current backup."
 
 126         ssh255 $REMOTE_HOST \
 
 127             "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $(basename $dir_timestamped) $dir_current"
 
 129         scottyerror "Rsync failed."
 
 132     while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
 
 133         oldestbackup=$(grepbackups | head -1)
 
 134         oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
 
 136         if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
 
 137             scottyinfo "Removing old backup $oldestbackup."
 
 138             ssh255 $REMOTE_HOST rm -r "$REMOTE_DIR/$oldestbackup"
 
 145 function deleteLock {
 
 146     if ! rmdir /var/lock/$(basename $0); then
 
 147         scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!"
 
 151 function cleanup_abort {
 
 152     scottyerror "Caught exit signal! Cleaning up."
 
 156     if [ $(jobs -p) ]; then
 
 157         scottyerror "TERMinating remaining child processes."
 
 167     scottyinfo "No cleanup function was defined."
 
 171     scottyinfo "No prepare function was defined."
 
 174 function cleanup_normal {
 
 181 Usage: $(basename $0) [OPTION]...
 
 184   -q   Only output errors
 
 185   -n   Run only on nth day
 
 186   -h   Print out this help
 
 190 function backmeupscotty {
 
 191     while getopts "qn:h" opt; do
 
 197                 BACKUP_RUNEVERYNTHDAY=$OPTARG
 
 206     ssh255 $REMOTE_HOST exit
 
 208     if latestTooOld; then
 
 209         scottyerror "The latest backup is too old."
 
 211         scottyinfo "This is the nth day."
 
 213         scottyinfo "No backup has to be done. Exiting."
 
 217     scottyinfo "Performing backup."
 
 219     trap cleanup_abort EXIT
 
 224     trap cleanup_normal EXIT
 
 229 if ! mkdir /var/lock/$(basename $0); then
 
 230     scottyerror "Another instance of $(basename $0) is still running!"