hcoop-backup: Throttle at 2.6Mbps.
[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-megacz.com-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
21 IFS=$'\n'
22
23 KEYFILE=/etc/backup-encryption-key
24 BACKUPDIR=/afs/megacz.com/private/hcoop-backup
25 BACKUPTMP=/var/backups/hcoop-backup
26 SUBDIR=`date +%Y.%m.%d`
27
28 #SYNC_CMD="rsync --bwlimit=$BWLIMIT --remove-source-files"
29
30 function copy_over () {
31 # Move file to its offsite destination
32 # $1: file, $2: relative directory (optional)
33 if test -z "$1" || test -n "$3"; then
34 echo "Bad programming"
35 exit 1
36 fi
37 local FILE=$1
38 local DEST=$BACKUPDIR/$SUBDIR
39 if test -n "$2"; then
40 DEST=$DEST/$2
41 fi
42 < $FILE catsync -b $BWLIMIT $DEST/$FILE
43 rm -f $FILE
44 }
45
46 cd $BACKUPDIR
47 find . -mindepth 1 -maxdepth 1 -type d -ctime +3 -delete || true
48
49 rm -rf $SUBDIR
50 mkdir -p $SUBDIR
51 mkdir -p $BACKUPTMP
52 cd $BACKUPTMP
53
54 groups
55 echo 'I am in:'
56 pwd
57 echo
58
59 echo building package lists...
60 dpkg-query -W -f='${Package}\n' > packages
61 (cd /; find / /usr/ /usr/local/ /var/ -xdev) | sort | uniq > allfiles
62 dpkg-query -W -f='${Package}\n' | xargs dpkg -L | sort | uniq > debfiles
63 dpkg-query -W -f='${Conffiles}\n' | grep / | cut -b2- | \
64 sed 's_ .*__' | sort | uniq > conffiles
65
66 diff allfiles debfiles | grep '^<' | cut -b 3- | \
67 grep -v ^/var/cache | \
68 grep -v ^/var/tmp | \
69 grep -v ^/var/lib/dpkg | \
70 grep -v ^/var/backups | \
71 grep -v ^/var/lib/changetrack | \
72 grep -v ^/var/local/lib/spamd | \
73 grep -v ^/var/run | \
74 grep -v ^/var/lock | \
75 grep -v ^/var/lib/ucf | \
76 grep -v ^/vicepa | \
77 grep -v ^/home | \
78 grep -v ^/tmp | \
79 grep -v '^/afs$' | \
80 grep -v '^/$' | \
81 grep -v '^/usr/$' | \
82 grep -v ^/usr/src | \
83 grep -v '^/usr/.*\.pyc' | \
84 grep -v '^/usr/.*\.elc' | \
85 grep -v '^/usr/bin/perldoc\.stub$' | \
86 grep -v '^/usr/bin/.*\.notslocate$' | \
87 grep -v '^/usr/lib/courier/.*\.rand$' | \
88 grep -v '^/usr/lib/gconv/gconv-modules\.cache$' | \
89 grep -v '^/usr/lib/graphviz/config$' | \
90 grep -v '^/usr/lib/locale/locale-archive$' | \
91 grep -v '^/usr/share/info/dir$' | \
92 grep -v '^/usr/share/info/dir\.old$' | \
93 grep -v '^/usr/share/emacs21/site-lisp/' | \
94 grep -v '^/usr/share/emacs22/site-lisp/' | \
95 grep -v '^/usr/share/vim/addons/doc/tags$' \
96 > backupfiles
97
98 cat conffiles >> backupfiles
99
100 cat backupfiles | \
101 grep -v ^/home | \
102 grep -v ^/usr/local | \
103 grep -v ^/var/spool | \
104 grep -v ^/var/log | \
105 grep -v ^/usr/lib/python2.4/ | \
106 grep -v ^/var/lib/python-support | \
107 grep -v ^/usr/share/jed/lib | \
108 grep -v ^/usr/share/man | \
109 grep -v ^/usr/share/perl5/IkiWiki/Plugin | \
110 grep -v ^/media | \
111 grep -v ^/vmlinuz | \
112 grep -v ^/vmlinuz.old | \
113 grep -v '^/sbin/[a-z\-]*\.modutils$' | \
114 grep -v ^/opt/dell/srvadmin/ | \
115 grep -v ^/boot/ | \
116 grep -v ^/dev/ | \
117 grep -v ^/etc/ | \
118 grep -v ^/root/ | \
119 grep -v ^/var/ | \
120 grep -v ^/lib/modules/ | \
121 grep -v ^/var/domtool/ | \
122 grep -v ^/var/lib/mysql/ | \
123 grep -v ^/var/lib/postgres/ | \
124 grep -v ^/var/lib/postgresql/ | \
125 xargs -I{} -d\\n -- bash -c "test -L '{}' || echo '{}'" > complain
126
127 F=hcoop.backup.tar$COMPRESS_EXT.aescrypt
128 tar clpf - --ignore-failed-read --no-recursion -C / -T backupfiles | \
129 $COMPRESS_PROG | \
130 ccrypt -k $KEYFILE -e > $F
131 copy_over $F
132
133 # Acquire lock before messing with spamd
134 COUNT=0
135 LOCK=/var/local/lib/spamd/.lock
136 while test -f $LOCK; do
137 sleep 2m
138 COUNT=$(expr $COUNT + 1)
139 if test $COUNT -eq 10; then
140 # Enough waiting. Kill the process.
141 P=$(cat $LOCK) || :
142 test -n "$P" && kill $P || :
143 rm -f $LOCK
144 break
145 fi
146 done
147 touch $LOCK
148
149 F=common.spamd.tar$COMPRESS_EXT.aescrypt
150 tar clpf - --ignore-failed-read -C / /var/local/lib/spamd | \
151 $COMPRESS_PROG | \
152 ccrypt -k $KEYFILE -e > $F.new
153 rm -f $LOCK
154 copy_over $F.new ..
155
156 test -s $BACKUPDIR/$F.new && \
157 mv $BACKUPDIR/$F.new $BACKUPDIR/$F
158
159 vos listvol deleuze | \
160 tail -n +2 | \
161 head -n -3 | \
162 cut -b1-34 | \
163 grep -v "\.backup .*$" | \
164 grep -v "\.readonly .*$" | \
165 sed 's_^ .*__' | \
166 sed 's_ .*$__' | \
167 grep '[A-Za-z]' \
168 > volumes
169
170 cat volumes | \
171 grep -v not-backed-up | \
172 xargs -I{} -d\\n -- \
173 bash -c \
174 "F={}.dump$COMPRESS_EXT.aescrypt ;
175 vos dump -id {} -localauth -clone |
176 $COMPRESS_PROG | ccrypt -k $KEYFILE -e > \$F ;
177 < \$F catsync -b $BWLIMIT $BACKUPDIR/$SUBDIR/\$F ;
178 rm -f \$F"
179
180 echo backing up databases
181 F=databases.tar$COMPRESS_EXT.aescrypt
182 tar -C /var/backups/databases/ -cf - . | \
183 $COMPRESS_PROG | \
184 ccrypt -k $KEYFILE -e > $F
185 copy_over $F
186
187 grep '[a-z/]' complain && \
188 mail -a 'From: The Backup Program <backups@deleuze.hcoop.net>' \
189 -s "automated message: annoying files found on deleuze (please do something about them)" admins@hcoop.net \
190 < complain \
191 || true
192
193 echo done
194