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 TAPEID";
21 # Try to read the tape ID from the tape into the file TAPEID
23 system "mt -f $tape rewind"; $? and die $?;
24 system "mt -f $tape setblk $blocksizebytes"; $? and die $?;
25 system "dd if=$tape bs=${blocksize}b count=10 | tar -b$blocksize -vvxf - TAPEID";
27 setstatus "FAILED during startup";
29 # We need some ID; if the tape has one already that takes precedence;
30 # otherwise the user might have set a tape ID that this should be
31 # by creating really-TAPEID.
32 if (open T, "TAPEID") {
33 unlink 'really-TAPEID';
34 } elsif (open T, "really-TAPEID") {
39 # read the ID; it had better be a non-empty string of alphanumeric chars.
41 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
42 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
45 setstatus "FAILED at tape identity check";
47 # We don't let the user overwrite the tape used for the last backup.
48 if (open L, "last-tape") {
49 chomp($lasttape= <L>);
55 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
57 # $tapeid identifies the individual tape; $tapedesc is its current
58 # identity and function, for printing in messages. You can make these
59 # namespaces the same if you like, or you can make the tape.<tapeid>
60 # files be links to tape.<tapedesc> files.
61 if (defined($tapedesc= readlink "$etc/tape.$tapeid")) {
62 $tapedesc =~ s/^.*\.//;
63 $tapedesc .= "($tapeid)";
68 # Parse the appropriate tape.nn file.
69 # Format is: empty lines and lines starting '#' are ignored. Trailing
70 # whitespace is ignored. File must end with 'end' on a line by itself.
71 # Either there should be a line 'incremental' to indicate that this is
72 # a tape for incremental backups, or a pair of lines 'filesystems fsg'
73 # and 'next tapeid', indicating that this tape is part of a full
74 # backup, containing the filesystem group fsg.
76 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
78 $_= <D> or die; chomp; s/\s+$//;
82 if (m/^filesystems (\w+)$/) {
84 } elsif (m/^next (\w+)$/) {
86 } elsif (m/^incremental$/) {
89 die "unknown entry in tape $tapeid at line $.: $_\n";
94 # Incremental backups are handled by increm, not us.
96 die "incremental tape $tapeid has next or filesystems\n"
97 if defined($next) || defined($fsys);
98 print STDERR "Incremental tape $tapeid.\n\n";
99 setstatus "FAILED during incremental startup";
100 exec "increm",$tapeid,$tapedesc;
104 # Read the filesystem group definition (file fsys.nnn)
108 $doing= "dump of $fsys to tape $tapedesc in drive $tape";
109 print LOG "$doing:\n" or die $!;
111 setstatus "FAILED writing tape ID";
112 # First write the tape ID to this tape.
113 open T, ">TAPEID" or die $!;
114 print T "$tapeid\n" or die $!;
117 system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
118 system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10"; $? and die $?;
120 unlink 'this-md5sums';
122 print "Doing $doing ...\n" or die $!;
125 system 'mknod p p'; $? and die $?;
127 setstatus "FAILED during dump";
130 close(DUMPOR); close(TEEOR); close(BUFOR); close(FINDOR);
131 close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
134 # work out a find option string that will exclude the required files
135 # Note that dump pays no attention to exclude options.
137 foreach $exc (@excldir) {
138 $exclopt .= "-regex $exc -prune -o ";
140 foreach $exc (@excl) {
141 $exclopt .= "-regex $exc -o ";
144 # For each filesystem to be put on this tape:
147 pipe(FINDOR,FINDOW) or die $!;
148 pipe(DUMPOR,DUMPOW) or die $!;
149 pipe(TEEOR,TEEOW) or die $!;
150 pipe(BUFOR,BUFOW) or die $!;
153 # We can back up via dump or cpio or zafio
155 $dumpcmd= "dump 0bfu $softblocksizekb - $atf";
156 $dumpin= '</dev/null';
157 } elsif ($tm eq 'cpio') {
158 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
159 $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
161 } elsif ($tm eq 'zafio') {
162 # compress-each-file-then-archive using afio
163 startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf $exclopt -print";
164 # don't use verbose flag as this generates 2MB report emails :->
165 $dumpcmd = "afio -b $softblocksizebytes -Zo -";
166 $dumpin = '<&FINDOR';
168 die "unknown method $tm for $prefix:$atf\n";
170 # This is a funky way of doing a pipeline which pays attention
171 # to the exit status of all the commands in the pipeline.
172 # It is roughly equivalent to:
173 # md5sum <p >>this-md5sums
174 # dump <$dumpin | tee p | writebuffer | dd >/dev/null
175 startprocess '<p','>>this-md5sums','md5sum';
176 startprocess $dumpin,'>&DUMPOW',$rstr.$dumpcmd;
177 startprocess '<&DUMPOR','>&TEEOW','tee p';
178 startprocess '<&TEEOR','>&BUFOW','writebuffer';
179 startprocess '<&BUFOR','>/dev/null'
180 ,"dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape";
185 # The backup should now be complete; verify it
187 setstatus "FAILED during check";
189 # Rewind the tape and skip the TAPEID record
190 system "mt -f $tape rewind"; $? and die $?;
191 system "mt -f $ntape fsf 1"; $? and die $?;
193 # Check the md5sums match for each filesystem on the tape
194 open S,"this-md5sums" or die $!;
198 $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
199 chomp($csum= `dd if=$ntape ibs=$blocksizebytes | readbuffer | md5sum`);
200 $orgsum eq $csum or die "MISMATCH $tf $csum $orgsum\n";
201 print "checksum ok $csum\t$tf\n" or die $!;
202 print LOG "checksum ok $csum\t$tf\n" or die $!;
205 system "mt -f $tape rewind"; $? and die $?;
207 setstatus "FAILED during cleanup";
209 # Write to some status files to indicate what the backup system
210 # ought to do when next invoked.
211 # reset incremental backup count to 1.
212 open IAN,">increm-advance.new" or die $!;
213 print IAN "1\n" or die $!;
216 # Next full backup is whatever the next link in the tape description
217 # file says it ought to be.
218 open TN,">next-full.new" or die $!;
219 print TN "$next\n" or die $!;
222 unlink 'last-tape','next-full';
223 # We are the last tape to have been backed up
224 rename 'TAPEID','last-tape' or die $!;
225 rename 'this-md5sums',"md5sums.$fsys" or die $!;
226 rename 'log',"log.$fsys" or die $!;
227 rename 'next-full.new',"next-full" or die $!;
228 rename 'increm-advance.new',"increm-advance" or die $!;
230 print "$doing completed.\nNext dump tape is $next.\n" or die $!;
232 setstatus "Successful: $tapedesc $fsys, next $next";