3 # Main backup script - does a full dump or execs increm. Do NOT run directly!
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.
9 # Copyright (C) 1997-1998,2000-2001,2007
10 # Ian Jackson <ian@chiark.greenend.org.uk>
11 # Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
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.
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
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.
27 $etc= '/etc/chiark-backup';
28 require "$etc/settings.pl";
29 require 'backuplib.pl';
36 if (m/^\-\-no\-reten$/) {
38 } elsif (m/^\-\-no\-config\-check$/) {
41 die "unknown option/argument \`$_'\n";
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.
52 setstatus "FAILED configuration check";
53 print "Configuration check ...\n" or die $!;
54 system 'backup-checkallused'; $? and die $?;
56 setstatus "FAILED rewinding";
62 setstatus "FAILED reading TAPEID";
63 # Try to read the tape ID from the tape into the file TAPEID
67 setstatus "FAILED during startup";
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") {
79 # read the ID; it had better be a non-empty string of alphanumeric chars.
81 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
82 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
85 setstatus "FAILED at tape identity check";
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>);
95 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
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)";
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.
116 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
118 $_= <D> or die; chomp; s/\s+$//;
122 if (m/^filesystems (\w+)$/) {
124 } elsif (m/^next (\w+)$/) {
126 } elsif (m/^incremental$/) {
129 die "unknown entry in tape $tapeid at line $.: $_\n";
134 # Incremental backups are handled by increm, not us.
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;
144 # Read the filesystem group definition (file fsys.nnn)
147 $doing= "dump of $fsys to tape $tapedesc in drive $tape";
148 print LOG "$doing:\n" or die $!;
151 setstatus "FAILED retensioning";
152 runsystem("mt -f $tape reten");
155 setstatus "FAILED writing tape ID";
156 # First write the tape ID to this tape.
158 writetapeid($tapeid,$tapedesc);
160 unlink 'this-md5sums';
162 print "Doing $doing ...\n" or die $!;
165 system 'mknod -m600 p p'; $? and die $?;
167 setstatus "FAILED during dump";
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);
176 # work out a find option string that will exclude the required files
177 # Note that dump pays no attention to exclude options.
179 foreach $exc (@excldir) {
180 $exclopt .= "-regex $exc -prune -o ";
182 foreach $exc (@excl) {
183 $exclopt .= "-regex $exc -o ";
186 # For each filesystem to be put on this tape:
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 $!;
200 $ddcmd= "dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape 2>&1";
204 pipe(GZOR,GZOW) or die $!;
205 $ddcmd .= " conv=sync";
208 nexttapefile("full $prefix:$atf_print");
210 # We can back up via dump or cpio or zafio
211 $dumpin= '</dev/null';
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";
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 - .";
231 die "unknown method $tm for $prefix:$atf_print\n";
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
239 startprocess '<p','>>this-md5sums',"$nice md5sum";
240 startprocess $dumpin,'>&DUMPOW',"$nice ".$rstr.$dumpcmd;
241 startprocess '<&DUMPOR','>&TEEOW',"$nice tee p";
243 startprocess '<&TEEOR','>&GZOW',"$nice gzip -v$gz";
245 startprocess "<&$bufir",'>&BUFOW',"$nasty writebuffer";
246 startprocess '<&DDERRR','>/dev/null',"$nice tee dderr >&2";
247 startprocess '<&BUFOR','>&DDERRW',"$nasty $ddcmd";
251 open DDERR, "dderr" or die $!;
252 defined(read DDERR,$_,1023) or die $!;
254 m/\n(\d+)\+0 records out\n/ or die ">$dderr< ?";
255 push @tapefilesizes, [ $1, $currenttapefilename ];
257 pboth("total blocks written so far: $totalrecords\n");
260 execute("$rstr mv -f $fsidfile+new $fsidfile");
266 # The backup should now be complete; verify it
268 setstatus "FAILED during check";
270 # Rewind the tape and skip the TAPEID record
271 runsystem("mt -f $tape rewind");
272 runsystem("mt -f $ntape fsf 1");
274 # Check the md5sums match for each filesystem on the tape
275 open S,"this-md5sums" or die $!;
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";
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 $!;
293 runsystem("mt -f $tape rewind");
295 setstatus "FAILED during cleanup";
298 foreach $tfs (@tapefilesizes) {
299 $summary .= sprintf " %10d blocks for %s\n", $tfs->[0], $tfs->[1]
302 sprintf " %10d blocks total (of %d bytes) plus TAPEID and headers\n",
303 $totalrecords, $blocksizebytes;
305 pboth("size-summary:\n");
308 open SS, ">size-summary..new" or die $!;
309 print SS $summary or die $!;
311 rename 'size-summary..new',"size-summary.$fsys" or die $!;
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 $!;
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 $!;
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 $!;
334 print "$doing completed.\nNext dump tape is $next.\n" or die $!;
336 setstatus "Successful: $tapedesc $fsys, next $next";