hcoop-backup: Try to make xargs die if an error occurred.
[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 KEYFILE=/etc/backup-encryption-key
19 BACKUPTMP=/var/backups/hcoop-backup-testing
20 CURDATE=$(date +%Y.%m.%d)
21
22 MOVE_OVER=$(dirname $0)/s3-move
23
24 IFS=$'\n'
25
26 #prune_old_backups
27
28 mkdir -p $BACKUPTMP
29 cd $BACKUPTMP
30
31 groups
32 echo 'I am in:'
33 pwd
34 echo
35
36 echo building package lists...
37 dpkg-query -W -f='${Package}\n' > packages
38 (cd /; find / /usr/ /usr/local/ /var/ -xdev) | sort | uniq > allfiles
39 dpkg-query -W -f='${Package}\n' | xargs dpkg -L | sort | uniq > debfiles
40 dpkg-query -W -f='${Conffiles}\n' | grep / | cut -b2- | \
41 sed 's_ .*__' | sort | uniq > conffiles
42
43 diff 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 | \
49 grep -v ^/var/local/lib/spamd | \
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 | \
60 grep -v '^/usr/.*\.pyc' | \
61 grep -v '^/usr/.*\.elc' | \
62 grep -v '^/usr/bin/perldoc\.stub$' | \
63 grep -v '^/usr/bin/.*\.notslocate$' | \
64 grep -v '^/usr/lib/courier/.*\.rand$' | \
65 grep -v '^/usr/lib/gconv/gconv-modules\.cache$' | \
66 grep -v '^/usr/lib/graphviz/config$' | \
67 grep -v '^/usr/lib/locale/locale-archive$' | \
68 grep -v '^/usr/share/info/dir$' | \
69 grep -v '^/usr/share/info/dir\.old$' | \
70 grep -v '^/usr/share/emacs21/site-lisp/' | \
71 grep -v '^/usr/share/emacs22/site-lisp/' | \
72 grep -v '^/usr/share/snmp/mibs/\.index$' | \
73 grep -v '^/usr/share/vim/addons/doc/tags$' \
74 > backupfiles
75
76 cat conffiles >> backupfiles
77
78 cat 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 | \
85 grep -v ^/usr/share/jed/lib | \
86 grep -v ^/usr/share/man | \
87 grep -v ^/usr/share/perl5/IkiWiki/Plugin | \
88 grep -v ^/media | \
89 grep -v ^/vmlinuz | \
90 grep -v ^/vmlinuz.old | \
91 grep -v '^/sbin/[a-z\-]*\.modutils$' | \
92 grep -v ^/opt/dell/srvadmin/ | \
93 grep -v ^/boot/ | \
94 grep -v ^/dev/ | \
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/ | \
103 xargs -I{} -d\\n -- bash -c "test -L '{}' || echo '{}'" > complain
104
105 F=hcoop.backup.tar$COMPRESS_EXT.aescrypt
106 tar clpf - --ignore-failed-read --no-recursion -C / -T backupfiles | \
107 $COMPRESS_PROG | \
108 ccrypt -k $KEYFILE -e | \
109 $MOVE_OVER $CURDATE $F
110
111 # Acquire lock before messing with spamd
112 COUNT=0
113 LOCK=/var/local/lib/spamd/.lock
114 while 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
124 done
125 touch $LOCK
126
127 F=common.spamd.tar$COMPRESS_EXT.aescrypt
128 tar clpf - --ignore-failed-read -C / /var/local/lib/spamd | \
129 $COMPRESS_PROG | \
130 ccrypt -k $KEYFILE -e > $F
131 rm -f $LOCK
132 < $F $MOVE_OVER $CURDATE $F
133 rm -f $F
134
135 vos listvol deleuze | \
136 tail -n +2 | \
137 head -n -3 | \
138 cut -b1-34 | \
139 grep -v "\.backup .*$" | \
140 grep -v "\.readonly .*$" | \
141 sed 's_^ .*__' | \
142 sed 's_ .*$__' | \
143 grep '[A-Za-z]' \
144 > volumes
145
146 cat volumes | \
147 grep -v not-backed-up | \
148 xargs -I{} -d\\n -- \
149 bash -e -c \
150 "F={}.dump$COMPRESS_EXT.aescrypt ;
151 vos dump -id {} -localauth -clone |
152 $COMPRESS_PROG | ccrypt -k $KEYFILE -e |
153 $MOVE_OVER $CURDATE \$F"
154
155 echo backing up databases
156 F=databases.tar$COMPRESS_EXT.aescrypt
157 tar -C /var/backups/databases/ -cf - . | \
158 $COMPRESS_PROG | \
159 ccrypt -k $KEYFILE -e | \
160 $MOVE_OVER $CURDATE $F
161
162 grep '[a-z/]' complain && \
163 mail -a 'From: The Backup Program <backups@deleuze.hcoop.net>' \
164 -s "automated message: annoying files found on deleuze (please do something about them)" admins@hcoop.net \
165 < complain \
166 || true
167
168 echo done
169