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