Debian Codegraph @

backup/full

raw
1 #!/usr/bin/perl
2 # full
3 # Main backup script - does a full dump or execs increm. Do NOT run directly!
4 #
5 # This file is part of chiark backup, a system for backing up GNU/Linux and
6 # other UN*X-compatible machines, as used on chiark.greenend.org.uk.
7 #
8 # chiark backup is:
9 # Copyright (C) 1997-1998,2000-2001,2007
10 # Ian Jackson <[email protected]>
11 # Copyright (C) 1999 Peter Maydell <[email protected]>
12 #
13 # This is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 3, or (at your option) any later version.
16 #
17 # This is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 # details.
21 #
22 # You should have received a copy of the GNU General Public License along
23 # with this program; if not, consult the Free Software Foundation's
24 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
25
26 BEGIN {
27 $etc= '/etc/chiark-backup';
28 require "$etc/settings.pl";
29 require 'backuplib.pl';
30 }
31
32 $|=1;
33
34 while (@ARGV) {
35 $_= shift @ARGV;
36 if (m/^\-\-no\-reten$/) {
37 $noreten=1;
38 } elsif (m/^\-\-no\-config\-check$/) {
39 $nocheck=1;
40 } else {
41 die "unknown option/argument \`$_'\n";
42 }
43 }
44
45 # Check to see whether the tape.nn and fsys.nn files are sane.
46 # checkallused checks that all the filesystems mounted are in fact
47 # dumped in both full and incremental dumps.
48
49 openlog();
50
51 if (!$nocheck) {
52 setstatus "FAILED configuration check";
53 print "Configuration check ...\n" or die $!;
54 system 'backup-checkallused'; $? and die $?;
55 } else {
56 setstatus "FAILED rewinding";
57 rewind_raw();
58 }
59
60 printdate();
61
62 setstatus "FAILED reading TAPEID";
63 # Try to read the tape ID from the tape into the file TAPEID
64
65 readtapeid_raw();
66
67 setstatus "FAILED during startup";
68
69 # We need some ID; if the tape has one already that takes precedence;
70 # otherwise the user might have set a tape ID that this should be
71 # by creating really-TAPEID.
72 if (open T, "TAPEID") {
73 unlink 'really-TAPEID';
74 } elsif (open T, "really-TAPEID") {
75 } else {
76 die "No TAPEID.\n";
77 }
78
79 # read the ID; it had better be a non-empty string of alphanumeric chars.
80 chomp($tapeid= <T>);
81 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
82 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
83 close T;
84
85 setstatus "FAILED at tape identity check";
86
87 # We don't let the user overwrite the tape used for the last backup.
88 if (open L, "last-tape") {
89 chomp($lasttape= <L>);
90 close L;
91 } else {
92 undef $lasttape;
93 }
94
95 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
96
97 # $tapeid identifies the individual tape; $tapedesc is its current
98 # identity and function, for printing in messages. You can make these
99 # namespaces the same if you like, or you can make the tape.<tapeid>
100 # files be links to tape.<tapedesc> files.
101 if (defined($tapedesc= readlink "$etc/tape.$tapeid")) {
102 $tapedesc =~ s/^.*\.//;
103 $tapedesc .= "($tapeid)";
104 } else {
105 $tapedesc = $tapeid;
106 }
107
108 # Parse the appropriate tape.nn file.
109 # Format is: empty lines and lines starting '#' are ignored. Trailing
110 # whitespace is ignored. File must end with 'end' on a line by itself.
111 # Either there should be a line 'incremental' to indicate that this is
112 # a tape for incremental backups, or a pair of lines 'filesystems fsg'
113 # and 'next tapeid', indicating that this tape is part of a full
114 # backup, containing the filesystem group fsg.
115 undef $fsys;
116 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
117 for (;;) {
118 $_= <D> or die; chomp; s/\s+$//;
119 last if m/^end$/;
120 next unless m/\S/;
121 next if m/^\#/;
122 if (m/^filesystems (\w+)$/) {
123 $fsys= $1;
124 } elsif (m/^next (\w+)$/) {
125 $next= $1;
126 } elsif (m/^incremental$/) {
127 $incremental= 1;
128 } else {
129 die "unknown entry in tape $tapeid at line $.: $_\n";
130 }
131 }
132 close D or die $!;
133
134 # Incremental backups are handled by increm, not us.
135 if ($incremental) {
136 die "incremental tape $tapeid has next or filesystems\n"
137 if defined($next) || defined($fsys);
138 print STDERR "Incremental tape $tapeid.\n\n";
139 setstatus "FAILED during incremental startup";
140 exec "increm",$tapeid,$tapedesc;
141 die $!;
142 }
143
144 # Read the filesystem group definition (file fsys.nnn)
145 readfsys("$fsys");
146
147 $doing= "dump of $fsys to tape $tapedesc in drive $tape";
148 print LOG "$doing:\n" or die $!;
149
150 if (!$noreten) {
151 setstatus "FAILED retensioning";
152 runsystem("mt -f $tape reten");
153 }
154
155 setstatus "FAILED writing tape ID";
156 # First write the tape ID to this tape.
157
158 writetapeid($tapeid,$tapedesc);
159
160 unlink 'this-md5sums';
161
162 print "Doing $doing ...\n" or die $!;
163
164 unlink 'p';
165 system 'mknod -m600 p p'; $? and die $?;
166
167 setstatus "FAILED during dump";
168
169 sub closepipes () {
170 close(DUMPOR); close(TEEOR); close(BUFOR); close(FINDOR);
171 close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
172 close(GZOR); close(GZOW);
173 close(DDERRR); close(DDERRW);
174 }
175
176 # work out a find option string that will exclude the required files
177 # Note that dump pays no attention to exclude options.
178 $exclopt = '';
179 foreach $exc (@excldir) {
180 $exclopt .= "-regex $exc -prune -o ";
181 }
182 foreach $exc (@excl) {
183 $exclopt .= "-regex $exc -o ";
184 }
185
186 # For each filesystem to be put on this tape:
187 for $tf (@fsys) {
188 printdate();
189 parsefsys();
190 prepfsys();
191
192 pipe(FINDOR,FINDOW) or die $!;
193 pipe(DUMPOR,DUMPOW) or die $!;
194 pipe(TEEOR,TEEOW) or die $!;
195 pipe(TEEOR,TEEOW) or die $!;
196 pipe(BUFOR,BUFOW) or die $!;
197 pipe(DDERRR,DDERRW) or die $!;
198
199 $bufir='TEEOR';
200 $ddcmd= "dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape 2>&1";
201
202 if ($gz) {
203 $bufir='GZOR';
204 pipe(GZOR,GZOW) or die $!;
205 $ddcmd .= " conv=sync";
206 }
207
208 nexttapefile("full $prefix:$atf_print");
209
210 # We can back up via dump or cpio or zafio
211 $dumpin= '</dev/null';
212 if ($tm eq 'dump') {
213 $dumplabel= $pcstr.$atf_print.'$';
214 $dumpcmd= "dump 0Lbfu $dumplabel $softblocksizekb - $atf";
215 } elsif ($tm eq 'cpio') {
216 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
217 $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
218 $dumpin= '<&FINDOR';
219 } elsif ($tm eq 'zafio') {
220 # compress-each-file-then-archive using afio
221 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf $exclopt -print";
222 # don't use verbose flag as this generates 2MB report emails :->
223 $dumpcmd = "afio -b $softblocksizebytes -Zo -";
224 $dumpin = '<&FINDOR';
225 } elsif ($tm eq 'ntfsimage') {
226 $dumpcmd= "ntfsimage -svvf --dirty $dev";
227 } elsif ($tm eq 'gtar') {
228 execute("$rstr touch $fsidfile+new");
229 $dumpcmd= "tar Ccfl $atf - .";
230 } else {
231 die "unknown method $tm for $prefix:$atf_print\n";
232 }
233 # This is a funky way of doing a pipeline which pays attention
234 # to the exit status of all the commands in the pipeline.
235 # It is roughly equivalent to:
236 # md5sum <p >>this-md5sums
237 # dump <$dumpin | tee p [| gzip] | writebuffer | dd >/dev/null
238
239 startprocess '<p','>>this-md5sums',"$nice md5sum";
240 startprocess $dumpin,'>&DUMPOW',"$nice ".$rstr.$dumpcmd;
241 startprocess '<&DUMPOR','>&TEEOW',"$nice tee p";
242 if ($gz) {
243 startprocess '<&TEEOR','>&GZOW',"$nice gzip -v$gz";
244 }
245 startprocess "<&$bufir",'>&BUFOW',"$nasty writebuffer";
246 startprocess '<&DDERRR','>/dev/null',"$nice tee dderr >&2";
247 startprocess '<&BUFOR','>&DDERRW',"$nasty $ddcmd";
248 closepipes();
249 endprocesses();
250
251 open DDERR, "dderr" or die $!;
252 defined(read DDERR,$_,1023) or die $!;
253 close DDERR;
254 m/\n(\d+)\+0 records out\n/ or die ">$dderr< ?";
255 push @tapefilesizes, [ $1, $currenttapefilename ];
256 $totalrecords += $1;
257 pboth("total blocks written so far: $totalrecords\n");
258
259 if ($tm eq 'gtar') {
260 execute("$rstr mv -f $fsidfile+new $fsidfile");
261 }
262
263 finfsys();
264 }
265
266 # The backup should now be complete; verify it
267
268 setstatus "FAILED during check";
269
270 # Rewind the tape and skip the TAPEID record
271 runsystem("mt -f $tape rewind");
272 runsystem("mt -f $ntape fsf 1");
273
274 # Check the md5sums match for each filesystem on the tape
275 open S,"this-md5sums" or die $!;
276 for $tf (@fsys) {
277 printdate();
278 parsefsys();
279 chomp($orgsum= <S>); $orgsum =~ s/\ +\-?$//;
280 $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
281 $cmd= "$nasty dd if=$ntape ibs=$blocksizebytes";
282 $cmd .= " | $nasty readbuffer";
283 $cmd .= " | $nice gzip -vd" if $gz;
284 $cmd .= " | $nice md5sum";
285 pboth(" $cmd\n");
286 chomp($csum= `$cmd`);
287 $csum =~ s/\ +\-?$//;
288 $orgsum eq $csum or die "MISMATCH $tf $csum $orgsum\n";
289 print "checksum ok $csum\t$tf\n" or die $!;
290 print LOG "checksum ok $csum\t$tf\n" or die $!;
291 }
292 printdate();
293 runsystem("mt -f $tape rewind");
294
295 setstatus "FAILED during cleanup";
296
297 $summary= '';
298 foreach $tfs (@tapefilesizes) {
299 $summary .= sprintf " %10d blocks for %s\n", $tfs->[0], $tfs->[1]
300 }
301 $summary .=
302 sprintf " %10d blocks total (of %d bytes) plus TAPEID and headers\n",
303 $totalrecords, $blocksizebytes;
304
305 pboth("size-summary:\n");
306 pboth($summary);
307
308 open SS, ">size-summary..new" or die $!;
309 print SS $summary or die $!;
310 close SS or die $!;
311 rename 'size-summary..new',"size-summary.$fsys" or die $!;
312
313 # Write to some status files to indicate what the backup system
314 # ought to do when next invoked.
315 # reset incremental backup count to 1.
316 open IAN,">increm-advance.new" or die $!;
317 print IAN "1\n" or die $!;
318 close IAN or die $!;
319
320 # Next full backup is whatever the next link in the tape description
321 # file says it ought to be.
322 open TN,">next-full.new" or die $!;
323 print TN "$next\n" or die $!;
324 close TN or die $!;
325
326 unlink 'last-tape','next-full';
327 # We are the last tape to have been backed up
328 rename 'TAPEID','last-tape' or die $!;
329 rename 'this-md5sums',"md5sums.$fsys" or die $!;
330 rename 'log',"log.$fsys" or die $!;
331 rename 'next-full.new',"next-full" or die $!;
332 rename 'increm-advance.new',"increm-advance" or die $!;
333
334 print "$doing completed.\nNext dump tape is $next.\n" or die $!;
335
336 setstatus "Successful: $tapedesc $fsys, next $next";
337 exit 0;