s3: Display additional messages, sleep before retrying.
[clinton/scripts.git] / hcoop-backup
CommitLineData
6d52e269 1#!/bin/bash -e
2
3#
4b645870 4# it is dangerous to remove the "-e" above; please don't do that.
6d52e269 5#
6
4b645870 7#
8# run this script as root, on deleuze
9#
6d52e269 10
be9bd94d 11exec >& /var/log/backup-to-s3-log
44b7f284 12
4b645870 13PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin
44b7f284 14#COMPRESS_EXT=.bz2
15#COMPRESS_PROG=bzip2
16COMPRESS_EXT=.gz
17COMPRESS_PROG=gzip
6d52e269 18KEYFILE=/etc/backup-encryption-key
be9bd94d 19BACKUPTMP=/var/backups/hcoop-backup-testing
3b16fa86 20CURDATE=$(date +%Y.%m.%d)
37839f02 21
22MOVE_OVER=$(dirname $0)/s3-move
23
24IFS=$'\n'
be9bd94d 25
bac71193 26#prune_old_backups
be9bd94d 27
bee5bcbc 28mkdir -p $BACKUPTMP
29cd $BACKUPTMP
24b2faa6 30
4b645870 31groups
24b2faa6 32echo 'I am in:'
33pwd
34echo
6d52e269 35
36echo building package lists...
24b2faa6 37dpkg-query -W -f='${Package}\n' > packages
4b645870 38(cd /; find / /usr/ /usr/local/ /var/ -xdev) | sort | uniq > allfiles
24b2faa6 39dpkg-query -W -f='${Package}\n' | xargs dpkg -L | sort | uniq > debfiles
bee5bcbc 40dpkg-query -W -f='${Conffiles}\n' | grep / | cut -b2- | \
41 sed 's_ .*__' | sort | uniq > conffiles
6d52e269 42
43diff allfiles debfiles | grep '^<' | cut -b 3- | \
44 grep -v ^/var/cache | \
45 grep -v ^/var/tmp | \
46 grep -v ^/var/lib/dpkg | \
47 grep -v ^/var/backups | \
48 grep -v ^/var/lib/changetrack | \
12e40abc 49 grep -v ^/var/local/lib/spamd | \
6d52e269 50 grep -v ^/var/run | \
51 grep -v ^/var/lock | \
52 grep -v ^/var/lib/ucf | \
53 grep -v ^/vicepa | \
54 grep -v ^/home | \
55 grep -v ^/tmp | \
56 grep -v '^/afs$' | \
57 grep -v '^/$' | \
58 grep -v '^/usr/$' | \
59 grep -v ^/usr/src | \
92a7af97 60 grep -v '^/usr/.*\.pyc' | \
61 grep -v '^/usr/.*\.elc' | \
62 grep -v '^/usr/bin/perldoc\.stub$' | \
5b84f395 63 grep -v '^/usr/bin/.*\.notslocate$' | \
d327aed8 64 grep -v '^/usr/lib/courier/.*\.rand$' | \
50f51a78 65 grep -v '^/usr/lib/gconv/gconv-modules\.cache$' | \
4df0bc18 66 grep -v '^/usr/lib/graphviz/config$' | \
50f51a78 67 grep -v '^/usr/lib/locale/locale-archive$' | \
24b2faa6 68 grep -v '^/usr/share/info/dir$' | \
50f51a78 69 grep -v '^/usr/share/info/dir\.old$' | \
24b2faa6 70 grep -v '^/usr/share/emacs21/site-lisp/' | \
71 grep -v '^/usr/share/emacs22/site-lisp/' | \
2c5daf49 72 grep -v '^/usr/share/snmp/mibs/\.index$' | \
ae0e82f0 73 grep -v '^/usr/share/vim/addons/doc/tags$' \
74 > backupfiles
6d52e269 75
24b2faa6 76cat conffiles >> backupfiles
77
6d52e269 78cat backupfiles | \
79 grep -v ^/home | \
80 grep -v ^/usr/local | \
81 grep -v ^/var/spool | \
82 grep -v ^/var/log | \
83 grep -v ^/usr/lib/python2.4/ | \
84 grep -v ^/var/lib/python-support | \
ae0e82f0 85 grep -v ^/usr/share/jed/lib | \
6d52e269 86 grep -v ^/usr/share/man | \
4df0bc18 87 grep -v ^/usr/share/perl5/IkiWiki/Plugin | \
6d52e269 88 grep -v ^/media | \
89 grep -v ^/vmlinuz | \
90 grep -v ^/vmlinuz.old | \
92a7af97 91 grep -v '^/sbin/[a-z\-]*\.modutils$' | \
6d52e269 92 grep -v ^/opt/dell/srvadmin/ | \
93 grep -v ^/boot/ | \
24b2faa6 94 grep -v ^/dev/ | \
6d52e269 95 grep -v ^/etc/ | \
96 grep -v ^/root/ | \
97 grep -v ^/var/ | \
98 grep -v ^/lib/modules/ | \
99 grep -v ^/var/domtool/ | \
100 grep -v ^/var/lib/mysql/ | \
101 grep -v ^/var/lib/postgres/ | \
102 grep -v ^/var/lib/postgresql/ | \
bee5bcbc 103 xargs -I{} -d\\n -- bash -c "test -L '{}' || echo '{}'" > complain
6d52e269 104
bee5bcbc 105F=hcoop.backup.tar$COMPRESS_EXT.aescrypt
4b645870 106tar clpf - --ignore-failed-read --no-recursion -C / -T backupfiles | \
eede979f 107 $COMPRESS_PROG | \
a5a60f0c 108 ccrypt -k $KEYFILE -e | \
3b16fa86 109 $MOVE_OVER $CURDATE $F
481c2d5f 110
111# Acquire lock before messing with spamd
112COUNT=0
113LOCK=/var/local/lib/spamd/.lock
114while test -f $LOCK; do
115 sleep 2m
116 COUNT=$(expr $COUNT + 1)
117 if test $COUNT -eq 10; then
118 # Enough waiting. Kill the process.
119 P=$(cat $LOCK) || :
120 test -n "$P" && kill $P || :
121 rm -f $LOCK
122 break
123 fi
124done
125touch $LOCK
126
bee5bcbc 127F=common.spamd.tar$COMPRESS_EXT.aescrypt
481c2d5f 128tar clpf - --ignore-failed-read -C / /var/local/lib/spamd | \
129 $COMPRESS_PROG | \
a5a60f0c 130 ccrypt -k $KEYFILE -e > $F
481c2d5f 131rm -f $LOCK
3b16fa86 132< $F $MOVE_OVER $CURDATE $F
a5a60f0c 133rm -f $F
481c2d5f 134
24b2faa6 135vos listvol deleuze | \
24b2faa6 136 tail -n +2 | \
137 head -n -3 | \
138 cut -b1-34 | \
139 grep -v "\.backup .*$" | \
eede979f 140 grep -v "\.readonly .*$" | \
4b645870 141 sed 's_^ .*__' | \
142 sed 's_ .*$__' | \
ae0e82f0 143 grep '[A-Za-z]' \
144 > volumes
24b2faa6 145
4b645870 146cat volumes | \
147 grep -v not-backed-up | \
148 xargs -I{} -d\\n -- \
c8f28654 149 bash -e -c \
bee5bcbc 150 "F={}.dump$COMPRESS_EXT.aescrypt ;
151 vos dump -id {} -localauth -clone |
a5a60f0c 152 $COMPRESS_PROG | ccrypt -k $KEYFILE -e |
3b16fa86 153 $MOVE_OVER $CURDATE \$F"
4b645870 154
155echo backing up databases
bee5bcbc 156F=databases.tar$COMPRESS_EXT.aescrypt
4b645870 157tar -C /var/backups/databases/ -cf - . | \
158 $COMPRESS_PROG | \
a5a60f0c 159 ccrypt -k $KEYFILE -e | \
3b16fa86 160 $MOVE_OVER $CURDATE $F
4b645870 161
162grep '[a-z/]' complain && \
bee5bcbc 163 mail -a 'From: The Backup Program <backups@deleuze.hcoop.net>' \
4b645870 164 -s "automated message: annoying files found on deleuze (please do something about them)" admins@hcoop.net \
165 < complain \
166 || true
44b7f284 167
168echo done
169