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