]> git.treefish.org Git - backmeupscotty.git/blob - backmeupscotty
44e9c54ae878f3be7eeba2e900a46cb3dcb528e7
[backmeupscotty.git] / backmeupscotty
1 #!/bin/bash
2
3 REMOTE_HOST=localhost
4 REMOTE_DIR=/tmp/backmeupscotty/test
5 ARCHIVE_KEEPNBACKUPS=30
6 ARCHIVE_KEEPNDAYS=30
7 BACKUP_RUNEVERYNTHDAY=1
8
9 function upperme {
10     echo $(basename $0) | tr '[:lower:]' '[:upper:]'
11 }
12
13 function scottyinfo {
14     echo $(upperme): $@ 
15 }
16
17 function scottyerror {
18     echo $(upperme): $@ >&2 
19 }
20
21 function ssh255 {
22     ssh $@
23     sshret=$?
24
25     if [ $sshret -eq 255 ]; then
26         scottyerror "SSH connection failed!"
27         exit 1
28     else
29         return $sshret
30     fi
31 }
32
33 function grepbackups {
34     ssh255 $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+'
35 }
36
37 function isIncomplete {
38     if ( ssh255 $REMOTE_HOST '[ -d '$REMOTE_DIR/incomplete' ]' ); then
39         return 0
40     else
41         return 1
42     fi
43 }
44
45 function isNthDay {
46     if [ $(( ( $(date +%s) / (60*60*24) ) % $BACKUP_RUNEVERYNTHDAY )) -eq 0 ]; 
47     then
48         return 0
49     else
50         return 1
51     fi  
52 }
53
54 function latestTooOld {
55     for oldbackup in $(grepbackups); do
56         tstamp=$(echo $oldbackup | cut -d'-' -f1)
57         
58         if [ $(( $(date +%s) - $tstamp )) -lt \
59             $(( ($BACKUP_RUNEVERYNTHDAY*24+12)*60*60 )) ]
60         then
61             return 1
62         fi
63     done
64
65     return 0
66 }
67
68 function scottysync {
69     timestamp=$(date +%s)
70
71     scottyinfo "Syncing $SYNC_SRC to $REMOTE_HOST:$REMOTE_DIR @$timestamp."
72
73     if [ ! -d "$SYNC_SRC" ]; then
74         scottyerror "Source dir $SYNC_SRC does not exist. Not syncing!"
75         return 1
76     fi
77
78     if [ $(ls -A "$SYNC_SRC" | wc -l) -eq 0 ]; then
79         scottyerror "Source dir $SYNC_SRC is empty. Not syncing!"
80         return 1
81     fi
82
83     dir_current=$REMOTE_DIR/current
84     dir_incomplete=$REMOTE_DIR/incomplete
85     dir_timestamped=$REMOTE_DIR/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
86
87     if [ -z $SYNC_EXC ]; then
88         rsync_exclude=""
89     else
90         rsync_exclude=$(eval echo --exclude={$SYNC_EXC} | tr -d {})
91     fi
92
93     if (ssh255 $REMOTE_HOST '[ ! -d '$REMOTE_DIR' ]'); then
94         scottyinfo "Creating destination directory $REMOTE_HOST:$REMOTE_DIR."
95         ssh255 $REMOTE_HOST "mkdir $REMOTE_DIR"
96     fi
97
98     if isIncomplete; then
99         scottyerror "Continuing old incomplete backup."
100     fi
101
102     scottyinfo "Starting rsync."
103     rsync -e ssh \
104         -v -aHAX --numeric-ids --delete --delete-excluded \
105         --link-dest=$dir_current \
106         $rsync_exclude \
107         $SYNC_SRC/ $REMOTE_HOST:$dir_incomplete/
108     
109     if [ $? -eq 0 ]; then
110         scottyinfo "Timestamping completed backup and linking to current backup."
111         ssh255 $REMOTE_HOST \
112             "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $(basename $dir_timestamped) $dir_current"
113     fi
114
115     while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
116         oldestbackup=$(grepbackups | head -1)
117         oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
118
119         if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
120             scottyinfo "Removing old backup $oldestbackup."
121             ssh255 $REMOTE_HOST rm -r "$REMOTE_DIR/$oldestbackup"
122         else
123             break
124         fi
125     done
126 }
127
128 function prepare {
129     scottyinfo "Preparing for sync."
130 }
131
132 function cleanup_abort {
133     scottyerror "Caught exit signal! Cleaning up."
134
135     cleanup ABORT
136
137     if [ $(jobs -p) ]; then
138         scottyerror "TERMinating remaining child processes."
139         kill $(jobs -p)
140     fi
141
142     exit
143 }
144
145 function cleanup {
146     scottyinfo "No cleanup function was defined."
147 }
148
149 function prepare {
150     scottyinfo "No prepare function was defined."
151 }
152
153 function printhelp {
154     cat <<EOF
155 Usage: $(basename $0) [OPTION]...
156
157 Recognized options:
158   -q   Only output errors
159   -n   Run only on nth day
160   -h   Print out this help
161 EOF
162 }
163
164 function backmeupscotty {
165     while getopts "qn:h" opt; do
166         case $opt in
167             q)
168                 exec > /dev/null
169                 ;;
170             n)
171                 BACKUP_RUNEVERYNTHDAY=$OPTARG
172                 ;;
173             h)
174                 printhelp
175                 exit 0
176                 ;;
177         esac
178     done
179     
180     ssh255 $REMOTE_HOST exit
181
182     if latestTooOld; then
183         scottyerror "The latest backup is too old."
184     elif isNthDay; then
185         scottyinfo "This is the nth day."
186     else
187         scottyinfo "No backup has to be done. Exiting."
188         exit 0
189     fi
190
191     scottyinfo "Performing backup."
192
193     trap cleanup_abort EXIT
194
195     prepare
196     scottysync
197
198     trap cleanup EXIT
199
200     exit 0
201 }
202
203 exec 200>/var/run/$(basename $0).pid
204 if ! flock -n 200; then
205     scottyerror "Another instance of $(basename $0) is still running!"
206     exit 1
207 fi
208 echo $$ 1>&200