4 REMOTE_DIR=/tmp/backmeupscotty/test
5 ARCHIVE_KEEPNBACKUPS=30
7 BACKUP_RUNEVERYNTHDAY=1
13 date +'[%y-%m-%d|%H:%M:%S]'
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 $(( ($BACKUP_RUNEVERYNTHDAY*24+12)*60*60 )) ]
85 scottyinfo "Syncing $SYNC_SRC to $REMOTE_HOST:$REMOTE_DIR @$timestamp."
87 if [ ! -d "$SYNC_SRC" ]; then
88 scottyerror "Source dir $SYNC_SRC does not exist. Not syncing!"
92 if [ $(ls -A "$SYNC_SRC" | wc -l) -eq 0 ]; then
93 scottyerror "Source dir $SYNC_SRC is empty. Not syncing!"
97 dir_current=$REMOTE_DIR/current
98 dir_incomplete=$REMOTE_DIR/incomplete
99 dir_timestamped=$REMOTE_DIR/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
101 if [ -z $SYNC_EXC ]; then
104 rsync_exclude=$(eval echo --exclude={$SYNC_EXC} | tr -d {})
107 if (ssh255 $REMOTE_HOST '[ ! -d '$REMOTE_DIR' ]'); then
108 scottyinfo "Creating destination directory $REMOTE_HOST:$REMOTE_DIR."
109 ssh255 $REMOTE_HOST "mkdir $REMOTE_DIR"
112 if isIncomplete; then
113 scottyerror "Continuing old incomplete backup."
116 scottyinfo "Starting rsync."
117 rsync -e ssh --bwlimit=$BWLIMIT \
118 -v -aHAX --numeric-ids --delete --delete-excluded \
119 --link-dest=$dir_current \
121 $SYNC_SRC/ $REMOTE_HOST:$dir_incomplete/
123 if [ $? -eq 0 ]; then
124 scottyinfo "Timestamping completed backup and linking to current backup."
125 ssh255 $REMOTE_HOST \
126 "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && \
127 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!"