5 require "$etc/settings.pl";
6 require 'backuplib.pl';
11 # Check to see whether the tape.nn and fsys.nn files are sane.
12 # checkallused checks that all the filesystems mounted are in fact
13 # dumped in both full and incremental dumps.
14 setstatus "FAILED configuration check";
15 print "Configuration check ...\n" or die $!;
16 system 'checkallused'; $? and die $?;
20 setstatus "FAILED reading tape ID";
21 # Try to read the tape ID from the tape into the file TAPEID
23 system "mt -f $tape setblk $blocksizebytes"; $? and die $?;
24 system "dd if=$tape bs=${blocksize}b count=10 | tar -b$blocksize -vvxf - TAPEID";
26 setstatus "FAILED during startup";
28 # We need some ID; if the tape has one already that takes precedence;
29 # otherwise the user might have set a tape ID that this should be
30 # by creating really-TAPEID.
31 if (open T, "TAPEID") {
32 unlink 'really-TAPEID';
33 } elsif (open T, "really-TAPEID") {
38 # read the ID; it had better be a non-empty string of alphanumeric chars.
40 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
41 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
44 setstatus "FAILED at tape identity check";
46 # We don't let the user overwrite the tape used for the last backup.
47 if (open L, "last-tape") {
48 chomp($lasttape= <L>);
54 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
56 # Parse the appropriate tape.nn file.
57 # Format is: empty lines and lines starting '#' are ignored. Trailing
58 # whitespace is ignored. File must end with 'end' on a line by itself.
59 # Either there should be a line 'incremental' to indicate that this is
60 # a tape for incremental backups, or a pair of lines 'filesystems fsg'
61 # and 'next tapeid', indicating that this tape is part of a full
62 # backup, containing the filesystem group fsg.
64 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
66 $_= <D> or die; chomp; s/\s+$//;
70 if (m/^filesystems (\w+)$/) {
72 } elsif (m/^next (\w+)$/) {
74 } elsif (m/^incremental$/) {
77 die "unknown entry in tape $tapeid at line $.: $_\n";
82 # Incremental backups are handled by increm, not us.
84 die "incremental tape $tapeid has next or filesystems\n"
85 if defined($next) || defined($fsys);
86 print STDERR "Incremental tape $tapeid.\n\n";
87 setstatus "FAILED during incremental startup";
88 exec "increm $tapeid";
92 # Read the filesystem group definition (file fsys.nnn)
96 $doing= "dump of $fsys to tape $tapeid in drive $tape";
97 print LOG "$doing:\n" or die $!;
99 setstatus "FAILED writing tape ID";
100 # First write the tape ID to this tape.
101 open T, ">TAPEID" or die $!;
102 print T "$tapeid\n" or die $!;
105 system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
106 system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10"; $? and die $?;
108 unlink 'this-md5sums';
110 print "Doing $doing ...\n" or die $!;
113 system 'mknod p p'; $? and die $?;
115 setstatus "FAILED during dump";
118 close(DUMPOR); close(TEEOR); close(BUFOR); close(FINDOR);
119 close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
122 # work out a find option string that will exclude the required files
123 # Note that dump pays no attention to exclude options.
125 foreach $exc (@excldir) {
126 $exclopt .= "-regex $exc -prune -o ";
128 foreach $exc (@excl) {
129 $exclopt .= "-regex $exc -o ";
132 # For each filesystem to be put on this tape:
135 pipe(FINDOR,FINDOW) or die $!;
136 pipe(DUMPOR,DUMPOW) or die $!;
137 pipe(TEEOR,TEEOW) or die $!;
138 pipe(BUFOR,BUFOW) or die $!;
141 # We can back up via dump or cpio
143 $dumpcmd= "dump 0bfu $softblocksizekb - $atf";
144 $dumpin= '</dev/null';
145 } elsif ($tm eq 'cpio') {
146 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
147 $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
149 } elsif ($tm eq 'zafio') {
150 # compress-each-file-then-archive using afio
151 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf $exclopt -print";
152 # don't use verbose flag as this generates 2MB report emails :->
153 $dumpcmd = "afio -b $softblocksizebytes -Zo -";
154 $dumpin = '<&FINDOR';
156 # This is a funky way of doing a pipeline which pays attention
157 # to the exit status of all the commands in the pipeline.
158 # It is roughly equivalent to:
159 # md5sum <p >>this-md5sums
160 # dump <$dumpin | tee p | writebuffer | dd >/dev/null
161 startprocess '<p','>>this-md5sums','md5sum';
162 startprocess $dumpin,'>&DUMPOW',$rstr.$dumpcmd;
163 startprocess '<&DUMPOR','>&TEEOW','tee p';
164 startprocess '<&TEEOR','>&BUFOW','writebuffer';
165 startprocess '<&BUFOR','>/dev/null'
166 ,"dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape";
171 # The backup should now be complete; verify it
173 setstatus "FAILED during check";
175 # Rewind the tape and skip the TAPEID record
176 system "mt -f $tape rewind"; $? and die $?;
177 system "mt -f $ntape fsf 1"; $? and die $?;
179 # Check the md5sums match for each filesystem on the tape
180 open S,"this-md5sums" or die $!;
184 $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
185 chomp($csum= `dd if=$ntape ibs=$blocksizebytes | readbuffer | md5sum`);
186 $orgsum eq $csum or die "MISMATCH $tf $csum $orgsum\n";
187 print "checksum ok $csum\t$tf\n" or die $!;
188 print LOG "checksum ok $csum\t$tf\n" or die $!;
191 system "mt -f $tape rewind"; $? and die $?;
193 setstatus "FAILED during cleanup";
195 # Write to some status files to indicate what the backup system
196 # ought to do when next invoked.
197 # reset incremental backup count to 1.
198 open IAN,">increm-advance.new" or die $!;
199 print IAN "1\n" or die $!;
202 # Next full backup is whatever the next link in the tape description
203 # file says it ought to be.
204 open TN,">next-full.new" or die $!;
205 print TN "$next\n" or die $!;
208 unlink 'last-tape','next-full';
209 # We are the last tape to have been backed up
210 rename 'TAPEID','last-tape' or die $!;
211 rename 'this-md5sums',"md5sums.$fsys" or die $!;
212 rename 'log',"log.$fsys" or die $!;
213 rename 'next-full.new',"next-full" or die $!;
214 rename 'increm-advance.new',"increm-advance" or die $!;
216 print "$doing completed.\nNext dump tape is $next.\n" or die $!;
218 setstatus "Successful ($tapeid $fsys, next $next)";