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 Ian Jackson <ian@chiark.greenend.org.uk>
10 # Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
12 # This is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2, or (at your option) any later version.
16 # This is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write to the Free Software Foundation, Inc.,
23 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 $etc= '/etc/chiark-backup';
27 require "$etc/settings.pl";
28 require 'backuplib.pl';
35 if (m/^\-\-no\-reten$/) {
37 } elsif (m/^\-\-no\-config\-check$/) {
40 die "unknown option/argument \`$_'\n";
44 # Check to see whether the tape.nn and fsys.nn files are sane.
45 # checkallused checks that all the filesystems mounted are in fact
46 # dumped in both full and incremental dumps.
49 setstatus "FAILED configuration check";
50 print "Configuration check ...\n" or die $!;
51 system 'backup-checkallused'; $? and die $?;
53 setstatus "FAILED rewinding";
59 setstatus "FAILED reading TAPEID";
60 # Try to read the tape ID from the tape into the file TAPEID
64 setstatus "FAILED during startup";
66 # We need some ID; if the tape has one already that takes precedence;
67 # otherwise the user might have set a tape ID that this should be
68 # by creating really-TAPEID.
69 if (open T, "TAPEID") {
70 unlink 'really-TAPEID';
71 } elsif (open T, "really-TAPEID") {
76 # read the ID; it had better be a non-empty string of alphanumeric chars.
78 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
79 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
82 setstatus "FAILED at tape identity check";
84 # We don't let the user overwrite the tape used for the last backup.
85 if (open L, "last-tape") {
86 chomp($lasttape= <L>);
92 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
94 # $tapeid identifies the individual tape; $tapedesc is its current
95 # identity and function, for printing in messages. You can make these
96 # namespaces the same if you like, or you can make the tape.<tapeid>
97 # files be links to tape.<tapedesc> files.
98 if (defined($tapedesc= readlink "$etc/tape.$tapeid")) {
99 $tapedesc =~ s/^.*\.//;
100 $tapedesc .= "($tapeid)";
105 # Parse the appropriate tape.nn file.
106 # Format is: empty lines and lines starting '#' are ignored. Trailing
107 # whitespace is ignored. File must end with 'end' on a line by itself.
108 # Either there should be a line 'incremental' to indicate that this is
109 # a tape for incremental backups, or a pair of lines 'filesystems fsg'
110 # and 'next tapeid', indicating that this tape is part of a full
111 # backup, containing the filesystem group fsg.
113 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
115 $_= <D> or die; chomp; s/\s+$//;
119 if (m/^filesystems (\w+)$/) {
121 } elsif (m/^next (\w+)$/) {
123 } elsif (m/^incremental$/) {
126 die "unknown entry in tape $tapeid at line $.: $_\n";
131 # Incremental backups are handled by increm, not us.
133 die "incremental tape $tapeid has next or filesystems\n"
134 if defined($next) || defined($fsys);
135 print STDERR "Incremental tape $tapeid.\n\n";
136 setstatus "FAILED during incremental startup";
137 exec "increm",$tapeid,$tapedesc;
141 # Read the filesystem group definition (file fsys.nnn)
145 $doing= "dump of $fsys to tape $tapedesc in drive $tape";
146 print LOG "$doing:\n" or die $!;
149 setstatus "FAILED retensioning";
150 system "mt -f $tape reten"; $? and die $?;
153 setstatus "FAILED writing tape ID";
154 # First write the tape ID to this tape.
156 writetapeid($tapeid,$tapedesc);
158 unlink 'this-md5sums';
160 print "Doing $doing ...\n" or die $!;
163 system 'mknod p p'; $? and die $?;
165 setstatus "FAILED during dump";
168 close(DUMPOR); close(TEEOR); close(BUFOR); close(FINDOR);
169 close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
170 close(GZOR); close(GZOW);
173 # work out a find option string that will exclude the required files
174 # Note that dump pays no attention to exclude options.
176 foreach $exc (@excldir) {
177 $exclopt .= "-regex $exc -prune -o ";
179 foreach $exc (@excl) {
180 $exclopt .= "-regex $exc -o ";
183 # For each filesystem to be put on this tape:
188 pipe(FINDOR,FINDOW) or die $!;
189 pipe(DUMPOR,DUMPOW) or die $!;
190 pipe(TEEOR,TEEOW) or die $!;
191 pipe(BUFOR,BUFOW) or die $!;
194 $ddcmd= "dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape";
198 pipe(GZOR,GZOW) or die $!;
199 $ddcmd .= " conv=sync";
202 # We can back up via dump or cpio or zafio
203 $dumpin= '</dev/null';
205 $dumpcmd= "dump 0bfu $softblocksizekb - $atf";
206 } elsif ($tm eq 'cpio') {
207 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
208 $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
210 } elsif ($tm eq 'zafio') {
211 # compress-each-file-then-archive using afio
212 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf $exclopt -print";
213 # don't use verbose flag as this generates 2MB report emails :->
214 $dumpcmd = "afio -b $softblocksizebytes -Zo -";
215 $dumpin = '<&FINDOR';
216 } elsif ($tm eq 'ntfsimage') {
217 $dumpcmd= "ntfsimage -vvf --dirty $dev";
219 die "unknown method $tm for $prefix:$atf\n";
221 # This is a funky way of doing a pipeline which pays attention
222 # to the exit status of all the commands in the pipeline.
223 # It is roughly equivalent to:
224 # md5sum <p >>this-md5sums
225 # dump <$dumpin | tee p [| gzip] | writebuffer | dd >/dev/null
226 startprocess '<p','>>this-md5sums',"$nice md5sum";
227 startprocess $dumpin,'>&DUMPOW',"$nice ".$rstr.$dumpcmd;
228 startprocess '<&DUMPOR','>&TEEOW',"$nice tee p";
230 startprocess '<&TEEOR','>&GZOW',"$nice gzip -v$gz";
232 startprocess "<&$bufir",'>&BUFOW',"$nasty writebuffer";
233 startprocess '<&BUFOR','>/dev/null',"$nasty $ddcmd";
238 # The backup should now be complete; verify it
240 setstatus "FAILED during check";
242 # Rewind the tape and skip the TAPEID record
243 system "mt -f $tape rewind"; $? and die $?;
244 system "mt -f $ntape fsf 1"; $? and die $?;
246 # Check the md5sums match for each filesystem on the tape
247 open S,"this-md5sums" or die $!;
251 chomp($orgsum= <S>); $orgsum =~ s/\ +\-?$//;
252 $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
253 $cmd= "$nasty dd if=$ntape ibs=$blocksizebytes";
254 $cmd .= " | $nasty readbuffer";
255 $cmd .= " | $nice gzip -vd" if $gz;
256 $cmd .= " | $nice md5sum";
257 print LOG " $cmd\n" or die $!;
258 print " $cmd\n" or die $!;
259 chomp($csum= `$cmd`);
260 $csum =~ s/\ +\-?$//;
261 $orgsum eq $csum or die "MISMATCH $tf $csum $orgsum\n";
262 print "checksum ok $csum\t$tf\n" or die $!;
263 print LOG "checksum ok $csum\t$tf\n" or die $!;
266 system "mt -f $tape rewind"; $? and die $?;
268 setstatus "FAILED during cleanup";
270 # Write to some status files to indicate what the backup system
271 # ought to do when next invoked.
272 # reset incremental backup count to 1.
273 open IAN,">increm-advance.new" or die $!;
274 print IAN "1\n" or die $!;
277 # Next full backup is whatever the next link in the tape description
278 # file says it ought to be.
279 open TN,">next-full.new" or die $!;
280 print TN "$next\n" or die $!;
283 unlink 'last-tape','next-full';
284 # We are the last tape to have been backed up
285 rename 'TAPEID','last-tape' or die $!;
286 rename 'this-md5sums',"md5sums.$fsys" or die $!;
287 rename 'log',"log.$fsys" or die $!;
288 rename 'next-full.new',"next-full" or die $!;
289 rename 'increm-advance.new',"increm-advance" or die $!;
291 print "$doing completed.\nNext dump tape is $next.\n" or die $!;
293 setstatus "Successful: $tapedesc $fsys, next $next";