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