6962450e809116ab0fa04c21cb458ddfc6210db0
[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
19 IFS=$'\n'
20
21 KEYFILE=/etc/backup-encryption-key
22 BACKUPDIR=/afs/megacz.com/private/hcoop-backup/
23
24 cd $BACKUPDIR
25 find * -prune -ctime +1 -exec rm -rf {} \; || true
26
27 SUBDIR=`date +%Y.%m.%d`
28 rm -rf $SUBDIR
29 mkdir -p $SUBDIR
30 cd $SUBDIR
31
32 groups
33 echo 'I am in:'
34 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- | 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/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 | \
59 grep -v '^/usr/.*\.pyc' | \
60 grep -v '^/usr/.*\.elc' | \
61 grep -v '^/usr/bin/perldoc\.stub$' | \
62 grep -v '^/usr/bin/.*\.notslocate$' | \
63 grep -v '^/usr/lib/courier/.*\.rand$' | \
64 grep -v '^/usr/lib/gconv/gconv-modules\.cache$' | \
65 grep -v '^/usr/lib/graphviz/config$' | \
66 grep -v '^/usr/lib/locale/locale-archive$' | \
67 grep -v '^/usr/share/info/dir$' | \
68 grep -v '^/usr/share/info/dir\.old$' | \
69 grep -v '^/usr/share/emacs21/site-lisp/' | \
70 grep -v '^/usr/share/emacs22/site-lisp/' | \
71 grep -v '^/usr/share/vim/addons/doc/tags$' | \
72 cat > backupfiles
73
74 cat conffiles >> backupfiles
75
76 cat backupfiles | \
77 grep -v ^/home | \
78 grep -v ^/usr/local | \
79 grep -v ^/var/spool | \
80 grep -v ^/var/log | \
81 grep -v ^/usr/lib/python2.4/ | \
82 grep -v ^/var/lib/python-support | \
83 grep -v ^/usr/share/man | \
84 grep -v ^/usr/share/perl5/IkiWiki/Plugin | \
85 grep -v ^/media | \
86 grep -v ^/vmlinuz | \
87 grep -v ^/vmlinuz.old | \
88 grep -v '^/sbin/[a-z\-]*\.modutils$' | \
89 grep -v ^/opt/dell/srvadmin/ | \
90 grep -v ^/boot/ | \
91 grep -v ^/dev/ | \
92 grep -v ^/etc/ | \
93 grep -v ^/root/ | \
94 grep -v ^/var/ | \
95 grep -v ^/lib/modules/ | \
96 grep -v ^/var/domtool/ | \
97 grep -v ^/var/lib/mysql/ | \
98 grep -v ^/var/lib/postgres/ | \
99 grep -v ^/var/lib/postgresql/ | \
100 xargs -I{} -d\\n -- bash -c "test -L '{}' || echo '{}'" | \
101 cat > complain
102
103 tar clpf - --ignore-failed-read --no-recursion -C / -T backupfiles | \
104 $COMPRESS_PROG | \
105 ccrypt -k $KEYFILE -e | \
106 catsync hcoop.backup.tar$COMPRESS_EXT.aescrypt
107
108 vos listvol deleuze | \
109 tail -n +2 | \
110 head -n -3 | \
111 cut -b1-34 | \
112 grep -v "\.backup .*$" | \
113 grep -v "\.readonly .*$" | \
114 sed 's_^ .*__' | \
115 sed 's_ .*$__' | \
116 grep '[A-Za-z]' | \
117 cat > volumes
118
119 cat volumes | \
120 grep -v not-backed-up | \
121 xargs -I{} -d\\n -- \
122 bash -c \
123 "vos dump -id {} -localauth -clone | $COMPRESS_PROG | ccrypt -k $KEYFILE -e | catsync {}.dump$COMPRESS_EXT.aescrypt"
124
125 echo backing up databases
126 tar -C /var/backups/databases/ -cf - . | \
127 $COMPRESS_PROG | \
128 ccrypt -k $KEYFILE -e \
129 | catsync databases.tar$COMPRESS_EXT.aescrypt
130
131 grep '[a-z/]' complain && \
132 mail -a 'From: Adam Megacz <megacz@hcoop.net>' \
133 -s "automated message: annoying files found on deleuze (please do something about them)" admins@hcoop.net \
134 < complain \
135 || true
136
137 echo done
138