6200a65855e8702a058b110bea7ad6f062dbf9d3
[clinton/scripts.git] / hcoop-backup
1 #!/bin/bash -e
2
3 #
4 # it is dangerous to remove the "-e" above; please don't do that.
5 #
6
7 #
8 # run this script as root, on deleuze
9 #
10
11 exec >& /var/log/backup-to-s3-log
12
13 PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin
14 #COMPRESS_EXT=.bz2
15 #COMPRESS_PROG=bzip2
16 COMPRESS_EXT=.gz
17 COMPRESS_PROG=gzip
18 # units for BWLIMIT are KB/s
19 BWLIMIT=325
20 # units for CHUNKSIZE are MB/s
21 CHUNKSIZE=5000
22
23 IFS=$'\n'
24
25 SCRIPTDIR=$(dirname $0)
26 KEYFILE=/etc/backup-encryption-key
27 BUCKET=hcoop.net-backups
28 BACKUPDIR=full
29 BACKUPTMP=/var/backups/hcoop-backup-testing
30 SUBDIR=$(date +%Y.%m.%d)
31
32 export S3_ACCESS_KEY_ID=$(cat ~mwolson_admin/.amazon/access.key)
33 export S3_SECRET_ACCESS_KEY=~mwolson_admin/.amazon/secret.key
34
35 function s3_cmd () {
36 # $1: command (get|put|ls|rm)
37 # $2: remote file
38 # $3: local file
39 local cmd=$1
40 shift
41 local bwarg
42 if test "$cmd" = "put"; then
43 bwarg="${BWLIMIT}K";
44 else
45 bwarg=
46 fi
47 $SCRIPTDIR/s3 $cmd $BUCKET "$1" "$2" $bwarg
48 }
49
50 function copy_over () {
51 # Move file to its offsite destination
52 # $1: file, $2: relative directory (optional)
53 if test -z "$1" || test -n "$3"; then
54 echo "Bad programming"
55 exit 1
56 fi
57 local FILE=$1
58 local DEST=$BACKUPDIR/$SUBDIR
59 if test -n "$2"; then
60 DEST=$DEST/$2
61 fi
62 split -d -b ${CHUNKSIZE}m $FILE ${FILE}.
63 for i in ${FILE}.*; do
64 s3_cmd put $DEST/$i $i
65 rm -f $i
66 done
67 rm -f $FILE
68 }
69
70 function prune_old_backups () {
71 local oldpwd=$PWD
72 cd $BACKUPDIR
73 find . -mindepth 1 -maxdepth 1 -type d -ctime +7 \
74 -execdir rm -fr '{}' \; || true
75 rm -rf $SUBDIR
76 mkdir -p $SUBDIR
77 cd $oldpwd
78 }
79
80 #prune_old_backups
81
82 mkdir -p $BACKUPTMP
83 cd $BACKUPTMP
84
85 groups
86 echo 'I am in:'
87 pwd
88 echo
89
90 echo building package lists...
91 dpkg-query -W -f='${Package}\n' > packages
92 (cd /; find / /usr/ /usr/local/ /var/ -xdev) | sort | uniq > allfiles
93 dpkg-query -W -f='${Package}\n' | xargs dpkg -L | sort | uniq > debfiles
94 dpkg-query -W -f='${Conffiles}\n' | grep / | cut -b2- | \
95 sed 's_ .*__' | sort | uniq > conffiles
96
97 diff allfiles debfiles | grep '^<' | cut -b 3- | \
98 grep -v ^/var/cache | \
99 grep -v ^/var/tmp | \
100 grep -v ^/var/lib/dpkg | \
101 grep -v ^/var/backups | \
102 grep -v ^/var/lib/changetrack | \
103 grep -v ^/var/local/lib/spamd | \
104 grep -v ^/var/run | \
105 grep -v ^/var/lock | \
106 grep -v ^/var/lib/ucf | \
107 grep -v ^/vicepa | \
108 grep -v ^/home | \
109 grep -v ^/tmp | \
110 grep -v '^/afs$' | \
111 grep -v '^/$' | \
112 grep -v '^/usr/$' | \
113 grep -v ^/usr/src | \
114 grep -v '^/usr/.*\.pyc' | \
115 grep -v '^/usr/.*\.elc' | \
116 grep -v '^/usr/bin/perldoc\.stub$' | \
117 grep -v '^/usr/bin/.*\.notslocate$' | \
118 grep -v '^/usr/lib/courier/.*\.rand$' | \
119 grep -v '^/usr/lib/gconv/gconv-modules\.cache$' | \
120 grep -v '^/usr/lib/graphviz/config$' | \
121 grep -v '^/usr/lib/locale/locale-archive$' | \
122 grep -v '^/usr/share/info/dir$' | \
123 grep -v '^/usr/share/info/dir\.old$' | \
124 grep -v '^/usr/share/emacs21/site-lisp/' | \
125 grep -v '^/usr/share/emacs22/site-lisp/' | \
126 grep -v '^/usr/share/snmp/mibs/\.index$' | \
127 grep -v '^/usr/share/vim/addons/doc/tags$' \
128 > backupfiles
129
130 cat conffiles >> backupfiles
131
132 cat backupfiles | \
133 grep -v ^/home | \
134 grep -v ^/usr/local | \
135 grep -v ^/var/spool | \
136 grep -v ^/var/log | \
137 grep -v ^/usr/lib/python2.4/ | \
138 grep -v ^/var/lib/python-support | \
139 grep -v ^/usr/share/jed/lib | \
140 grep -v ^/usr/share/man | \
141 grep -v ^/usr/share/perl5/IkiWiki/Plugin | \
142 grep -v ^/media | \
143 grep -v ^/vmlinuz | \
144 grep -v ^/vmlinuz.old | \
145 grep -v '^/sbin/[a-z\-]*\.modutils$' | \
146 grep -v ^/opt/dell/srvadmin/ | \
147 grep -v ^/boot/ | \
148 grep -v ^/dev/ | \
149 grep -v ^/etc/ | \
150 grep -v ^/root/ | \
151 grep -v ^/var/ | \
152 grep -v ^/lib/modules/ | \
153 grep -v ^/var/domtool/ | \
154 grep -v ^/var/lib/mysql/ | \
155 grep -v ^/var/lib/postgres/ | \
156 grep -v ^/var/lib/postgresql/ | \
157 xargs -I{} -d\\n -- bash -c "test -L '{}' || echo '{}'" > complain
158
159 F=hcoop.backup.tar$COMPRESS_EXT.aescrypt
160 tar clpf - --ignore-failed-read --no-recursion -C / -T backupfiles | \
161 $COMPRESS_PROG | \
162 ccrypt -k $KEYFILE -e > $F
163 copy_over $F
164
165 # Acquire lock before messing with spamd
166 COUNT=0
167 LOCK=/var/local/lib/spamd/.lock
168 while test -f $LOCK; do
169 sleep 2m
170 COUNT=$(expr $COUNT + 1)
171 if test $COUNT -eq 10; then
172 # Enough waiting. Kill the process.
173 P=$(cat $LOCK) || :
174 test -n "$P" && kill $P || :
175 rm -f $LOCK
176 break
177 fi
178 done
179 touch $LOCK
180
181 F=common.spamd.tar$COMPRESS_EXT.aescrypt
182 tar clpf - --ignore-failed-read -C / /var/local/lib/spamd | \
183 $COMPRESS_PROG | \
184 ccrypt -k $KEYFILE -e > $F.new
185 rm -f $LOCK
186 copy_over $F.new ..
187
188 test -s $BACKUPDIR/$F.new && \
189 mv $BACKUPDIR/$F.new $BACKUPDIR/$F
190
191 vos listvol deleuze | \
192 tail -n +2 | \
193 head -n -3 | \
194 cut -b1-34 | \
195 grep -v "\.backup .*$" | \
196 grep -v "\.readonly .*$" | \
197 sed 's_^ .*__' | \
198 sed 's_ .*$__' | \
199 grep '[A-Za-z]' \
200 > volumes
201
202 cat volumes | \
203 grep -v not-backed-up | \
204 xargs -I{} -d\\n -- \
205 bash -c \
206 "F={}.dump$COMPRESS_EXT.aescrypt ;
207 vos dump -id {} -localauth -clone |
208 $COMPRESS_PROG | ccrypt -k $KEYFILE -e > \$F ;
209 < \$F catsync -b $BWLIMIT $BACKUPDIR/$SUBDIR/\$F ;
210 rm -f \$F"
211
212 echo backing up databases
213 F=databases.tar$COMPRESS_EXT.aescrypt
214 tar -C /var/backups/databases/ -cf - . | \
215 $COMPRESS_PROG | \
216 ccrypt -k $KEYFILE -e > $F
217 copy_over $F
218
219 grep '[a-z/]' complain && \
220 mail -a 'From: The Backup Program <backups@deleuze.hcoop.net>' \
221 -s "automated message: annoying files found on deleuze (please do something about them)" admins@hcoop.net \
222 < complain \
223 || true
224
225 echo done
226