4 REMOTE_DIR=/tmp/backmeupscotty/test
5 ARCHIVE_KEEPNBACKUPS=30
7 BACKUP_RUNEVERYNTHDAY=1
12 date +'[%y-%m-%d|%H:%M:%S]'
20 if [ $_ERROR_ENCOUNTERED -eq 0 ]; then
27 function scottyerror {
29 if [ $_ERROR_ENCOUNTERED -eq 0 ]; then
31 scottyline "Going into verbose mode after error encounter." >&2
39 if [ $sshret -eq 255 ]; then
40 scottyerror "SSH connection failed!"
47 function grepbackups {
48 ssh255 $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+'
51 function isIncomplete {
52 if ( ssh255 $REMOTE_HOST '[ -d '$REMOTE_DIR/incomplete' ]' ); then
60 if [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ];
68 function latestTooOld {
69 for oldbackup in $(grepbackups); do
70 tstamp=$(echo $oldbackup | cut -d'-' -f1)
72 if [ $(( $(date +%s) - $tstamp )) -lt \
73 $(( ($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."
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 && ln -s $(basename $dir_timestamped) $dir_current"
128 scottyerror "Rsync failed."
131 while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
132 oldestbackup=$(grepbackups | head -1)
133 oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
135 if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
136 scottyinfo "Removing old backup $oldestbackup."
137 ssh255 $REMOTE_HOST rm -r "$REMOTE_DIR/$oldestbackup"
144 function deleteLock {
145 if ! rmdir /var/lock/$(basename $0); then
146 scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!"
150 function cleanup_abort {
151 scottyerror "Caught exit signal! Cleaning up."
155 if [ $(jobs -p) ]; then
156 scottyerror "TERMinating remaining child processes."
166 scottyinfo "No cleanup function was defined."
170 scottyinfo "No prepare function was defined."
173 function cleanup_normal {
180 Usage: $(basename $0) [OPTION]...
183 -q Only output errors
184 -n Run only on nth day
185 -h Print out this help
189 function backmeupscotty {
190 while getopts "qn:h" opt; do
196 BACKUP_RUNEVERYNTHDAY=$OPTARG
205 ssh255 $REMOTE_HOST exit
207 if latestTooOld; then
208 scottyerror "The latest backup is too old."
210 scottyinfo "This is the nth day."
212 scottyinfo "No backup has to be done. Exiting."
216 scottyinfo "Performing backup."
218 trap cleanup_abort EXIT
223 trap cleanup_normal EXIT
228 if ! mkdir /var/lock/$(basename $0); then
229 scottyerror "Another instance of $(basename $0) is still running!"