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