4 REMOTE_DIR=/tmp/backmeupscotty/test
5 ARCHIVE_KEEPNBACKUPS=30
7 BACKUP_RUNEVERYNTHDAY=1
10 _UPPERME=$(echo $(basename $0) | tr '[:lower:]' '[:upper:]')
13 echo $(date +"[%y-%m-%d %H:%M:%S]")
17 echo $(timestamp) $_UPPERME: $@
21 if ($_ERROR_ENCOUNTERED -eq 0); then
28 function scottyerror {
37 if [ $sshret -eq 255 ]; then
38 scottyerror "SSH connection failed!"
45 function grepbackups {
46 ssh255 $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+'
49 function isIncomplete {
50 if ( ssh255 $REMOTE_HOST '[ -d '$REMOTE_DIR/incomplete' ]' ); then
58 if [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ];
66 function latestTooOld {
67 for oldbackup in $(grepbackups); do
68 tstamp=$(echo $oldbackup | cut -d'-' -f1)
70 if [ $(( $(date +%s) - $tstamp )) -lt \
71 $(( ($BACKUP_RUNEVERYNTHDAY*24+12)*60*60 )) ]
83 scottyinfo "Syncing $SYNC_SRC to $REMOTE_HOST:$REMOTE_DIR @$timestamp."
85 if [ ! -d "$SYNC_SRC" ]; then
86 scottyerror "Source dir $SYNC_SRC does not exist. Not syncing!"
90 if [ $(ls -A "$SYNC_SRC" | wc -l) -eq 0 ]; then
91 scottyerror "Source dir $SYNC_SRC is empty. Not syncing!"
95 dir_current=$REMOTE_DIR/current
96 dir_incomplete=$REMOTE_DIR/incomplete
97 dir_timestamped=$REMOTE_DIR/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
99 if [ -z $SYNC_EXC ]; then
102 rsync_exclude=$(eval echo --exclude={$SYNC_EXC} | tr -d {})
105 if (ssh255 $REMOTE_HOST '[ ! -d '$REMOTE_DIR' ]'); then
106 scottyinfo "Creating destination directory $REMOTE_HOST:$REMOTE_DIR."
107 ssh255 $REMOTE_HOST "mkdir $REMOTE_DIR"
110 if isIncomplete; then
111 scottyerror "Continuing old incomplete backup."
114 scottyinfo "Starting rsync."
116 -v -aHAX --numeric-ids --delete --delete-excluded \
117 --link-dest=$dir_current \
119 $SYNC_SRC/ $REMOTE_HOST:$dir_incomplete/
121 if [ $? -eq 0 ]; then
122 scottyinfo "Timestamping completed backup and linking to current backup."
123 ssh255 $REMOTE_HOST \
124 "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $(basename $dir_timestamped) $dir_current"
126 scottyerror "Rsync failed."
129 while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
130 oldestbackup=$(grepbackups | head -1)
131 oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
133 if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
134 scottyinfo "Removing old backup $oldestbackup."
135 ssh255 $REMOTE_HOST rm -r "$REMOTE_DIR/$oldestbackup"
142 function deleteLock {
143 if ! rmdir /var/lock/$(basename $0); then
144 scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!"
148 function cleanup_abort {
149 scottyerror "Caught exit signal! Cleaning up."
153 if [ $(jobs -p) ]; then
154 scottyerror "TERMinating remaining child processes."
164 scottyinfo "No cleanup function was defined."
168 scottyinfo "No prepare function was defined."
171 function cleanup_normal {
178 Usage: $(basename $0) [OPTION]...
181 -q Only output errors
182 -n Run only on nth day
183 -h Print out this help
187 function backmeupscotty {
188 while getopts "qn:h" opt; do
194 BACKUP_RUNEVERYNTHDAY=$OPTARG
203 ssh255 $REMOTE_HOST exit
205 if latestTooOld; then
206 scottyerror "The latest backup is too old."
208 scottyinfo "This is the nth day."
210 scottyinfo "No backup has to be done. Exiting."
214 scottyinfo "Performing backup."
216 trap cleanup_abort EXIT
221 trap cleanup_normal EXIT
226 if ! mkdir /var/lock/$(basename $0); then
227 scottyerror "Another instance of $(basename $0) is still running!"