4 REMOTE_DIR=/tmp/backmeupscotty/test
5 ARCHIVE_KEEPNBACKUPS=30
7 BACKUP_RUNEVERYNTHDAY=1
10 echo $(basename $0) | tr '[:lower:]' '[:upper:]'
17 function scottyerror {
18 echo $(upperme): $@ >&2
21 if [ $(pidof -x $(basename $0) | wc -w) -gt 2 ]; then
22 scottyerror Another instance of $(basename $0) is already running!
26 while getopts "qn:" opt; do
32 BACKUP_RUNEVERYNTHDAY=$OPTARG
37 function grepbackups {
38 ssh $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+'
41 function isIncomplete {
42 if ( ssh $REMOTE_HOST '[ -d '$REMOTE_DIR/incomplete' ]' ); then
49 function isIncompleteOrNthDay {
51 [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ];
62 scottyinfo Syncing $SYNC_SRC to $REMOTE_HOST:$REMOTE_DIR @$timestamp
64 if [ ! -d "$SYNC_SRC" ]; then
65 scottyerror Source dir $SYNC_SRC does not exist. Not syncing!
69 if [ $(ls -A "$SYNC_SRC" | wc -l) -eq 0 ]; then
70 scottyerror Source dir $SYNC_SRC is empty. Not syncing!
74 dir_current=$REMOTE_DIR/current
75 dir_incomplete=$REMOTE_DIR/incomplete
76 dir_timestamped=$REMOTE_DIR/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
78 if [ -z $SYNC_EXC ]; then
81 rsync_exclude=$(eval echo --exclude={$SYNC_EXC} | tr -d {})
84 if (ssh $REMOTE_HOST '[ ! -d '$REMOTE_DIR' ]'); then
85 scottyinfo Creating destination directory $REMOTE_HOST:$REMOTE_DIR
86 ssh $REMOTE_HOST "mkdir $REMOTE_DIR"
90 scottyerror Continuing old incomplete backup
93 scottyinfo Starting rsync
95 -v -aHAX --numeric-ids --delete --delete-excluded \
96 --link-dest=$dir_current \
98 $SYNC_SRC/ $REMOTE_HOST:$dir_incomplete/
100 if [ $? -eq 0 ]; then
101 scottyinfo Timestamping completed backup and linking to current backup
103 "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $(basename $dir_timestamped) $dir_current"
106 while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
107 oldestbackup=$(grepbackups | head -1)
108 oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
110 if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
111 scottyinfo Removing old backup $oldestbackup
112 ssh $REMOTE_HOST rm -r "$REMOTE_DIR/$oldestbackup"
120 scottyinfo "Preparing for sync"
123 function cleanup_abrt {
124 scottyinfo "No cleanup_abrt function was defined."
127 function _cleanup_abrt {
128 scottyerror "Caught exit signal! Cleaning up..."
130 if [ $(jobs -p) ]; then
131 scottyerror TERMinating remaining child processes...
138 scottyinfo "No prepare function was defined."
145 function cleanup_normal {
146 scottyinfo "No cleanup_normal function was defined."
149 function _cleanup_normal {
153 function backmeupscotty {
154 if ! isIncompleteOrNthDay; then
155 scottyinfo "This is not the nth day and no incomplete backup exists."
159 trap _cleanup_abrt EXIT