chiark / gitweb /
Merge Peter Maydell's changes.
[chiark-utils.git] / backup / full
index 0608e6095234da93eb529759efe88942c339bdb8..5e6d23e0f0231c062ece1830bb9b445da6f72855 100755 (executable)
@@ -8,11 +8,17 @@ BEGIN {
 
 $|=1;
 
+# Check to see whether the tape.nn and fsys.nn files are sane.
+# checkallused checks that all the filesystems mounted are in fact
+# dumped in both full and incremental dumps.
+setstatus "FAILED configuration check";
 print "Configuration check ...\n" or die $!;
 system 'checkallused'; $? and die $?;
 
 printdate();
 
+setstatus "FAILED reading TAPEID";
+# Try to read the tape ID from the tape into the file TAPEID
 unlink 'TAPEID';
 system "mt -f $tape rewind"; $? and die $?;
 system "mt -f $tape setblk $blocksizebytes"; $? and die $?;
@@ -20,6 +26,9 @@ system "dd if=$tape bs=${blocksize}b count=10 | tar -b$blocksize -vvxf - TAPEID"
 
 setstatus "FAILED during startup";
 
+# We need some ID; if the tape has one already that takes precedence;
+# otherwise the user might have set a tape ID that this should be
+# by creating really-TAPEID.
 if (open T, "TAPEID") {
     unlink 'really-TAPEID';
 } elsif (open T, "really-TAPEID") {
@@ -27,6 +36,7 @@ if (open T, "TAPEID") {
     die "No TAPEID.\n";
 }
 
+# read the ID; it had better be a non-empty string of alphanumeric chars.
 chomp($tapeid= <T>);
 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
@@ -34,6 +44,7 @@ close T;
 
 setstatus "FAILED at tape identity check";
 
+# We don't let the user overwrite the tape used for the last backup.
 if (open L, "last-tape") {
     chomp($lasttape= <L>);
     close L;
@@ -43,6 +54,10 @@ if (open L, "last-tape") {
 
 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
 
+# $tapeid identifies the individual tape; $tapedesc is its current
+# identity and function, for printing in messages.  You can make these
+# namespaces the same if you like, or you can make the tape.<tapeid>
+# files be links to tape.<tapedesc> files.
 if (defined($tapedesc= readlink "$etc/tape.$tapeid")) {
     $tapedesc =~ s/^.*\.//;
     $tapedesc .= "($tapeid)";
@@ -50,6 +65,13 @@ if (defined($tapedesc= readlink "$etc/tape.$tapeid")) {
     $tapedesc = $tapeid;
 }
 
+# Parse the appropriate tape.nn file.
+# Format is: empty lines and lines starting '#' are ignored. Trailing
+# whitespace is ignored. File must end with 'end' on a line by itself.
+# Either there should be a line 'incremental' to indicate that this is
+# a tape for incremental backups, or a pair of lines 'filesystems fsg'
+# and 'next tapeid', indicating that this tape is part of a full 
+# backup, containing the filesystem group fsg. 
 undef $fsys;
 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
 for (;;) {
@@ -69,6 +91,7 @@ for (;;) {
 }
 close D or die $!;
 
+# Incremental backups are handled by increm, not us.
 if ($incremental) {
     die "incremental tape $tapeid has next or filesystems\n"
        if defined($next) || defined($fsys);
@@ -78,14 +101,17 @@ if ($incremental) {
     die $!;
 }
 
+# Read the filesystem group definition (file fsys.nnn)
 readfsys("$fsys");
 openlog();
 
 $doing= "dump of $fsys to tape $tapedesc in drive $tape";
 print LOG "$doing:\n" or die $!;
 
+setstatus "FAILED writing tape ID";
+# First write the tape ID to this tape.
 open T, ">TAPEID" or die $!;
-print T "$tapeid\n$tapedesc\n" or die $!;
+print T "$tapeid\n" or die $!;
 close T or die $!;
 
 system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
@@ -105,6 +131,17 @@ sub closepipes () {
     close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
 }
 
+# work out a find option string that will exclude the required files    
+# Note that dump pays no attention to exclude options.
+$exclopt = '';
+foreach $exc (@excldir) {
+    $exclopt .= "-regex $exc -prune -o ";
+}
+foreach $exc (@excl) {
+    $exclopt .= "-regex $exc -o ";
+}
+
+# For each filesystem to be put on this tape:
 for $tf (@fsys) {
     printdate();
     pipe(FINDOR,FINDOW) or die $!;
@@ -112,14 +149,29 @@ for $tf (@fsys) {
     pipe(TEEOR,TEEOW) or die $!;
     pipe(BUFOR,BUFOW) or die $!;
     parsefsys();
+    
+    # We can back up via dump or cpio or zafio
     if ($tm eq 'dump') {
        $dumpcmd= "dump 0bfu $softblocksizekb - $atf";
        $dumpin= '</dev/null';
-    } else {
+    } elsif ($tm eq 'cpio') {
        startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
        $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
        $dumpin= '<&FINDOR';
+    } elsif ($tm eq 'zafio') {
+        # compress-each-file-then-archive using afio
+        startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf $exclopt -print";
+        # don't use verbose flag as this generates 2MB report emails :->
+        $dumpcmd = "afio -b $softblocksizebytes -Zo -";
+        $dumpin = '<&FINDOR';
+    } else {
+       die "unknown method $tm for $prefix:$atf\n";
     }
+    # This is a funky way of doing a pipeline which pays attention
+    # to the exit status of all the commands in the pipeline.
+    # It is roughly equivalent to:
+    #    md5sum <p >>this-md5sums
+    #    dump <$dumpin | tee p | writebuffer | dd >/dev/null
     startprocess '<p','>>this-md5sums','md5sum';
     startprocess $dumpin,'>&DUMPOW',$rstr.$dumpcmd;
     startprocess '<&DUMPOR','>&TEEOW','tee p';
@@ -130,17 +182,21 @@ for $tf (@fsys) {
     endprocesses();
 }
 
+# The backup should now be complete; verify it
+
 setstatus "FAILED during check";
 
+# Rewind the tape and skip the TAPEID record
 system "mt -f $tape rewind"; $? and die $?;
 system "mt -f $ntape fsf 1"; $? and die $?;
 
+# Check the md5sums match for each filesystem on the tape
 open S,"this-md5sums" or die $!;
 for $tf (@fsys) {
     printdate();
     chomp($orgsum= <S>);
     $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
-    chomp($csum= `readbuffer <$ntape | md5sum`);
+    chomp($csum= `dd if=$ntape ibs=$blocksizebytes | readbuffer | md5sum`);
     $orgsum eq $csum or die "MISMATCH $tf $csum $orgsum\n";
     print "checksum ok $csum\t$tf\n" or die $!;
     print LOG "checksum ok $csum\t$tf\n" or die $!;
@@ -150,15 +206,21 @@ system "mt -f $tape rewind"; $? and die $?;
 
 setstatus "FAILED during cleanup";
 
+# Write to some status files to indicate what the backup system
+# ought to do when next invoked.
+# reset incremental backup count to 1.
 open IAN,">increm-advance.new" or die $!;
 print IAN "1\n" or die $!;
 close IAN or die $!;
 
+# Next full backup is whatever the next link in the tape description
+# file says it ought to be.
 open TN,">next-full.new" or die $!;
 print TN "$next\n" or die $!;
 close TN or die $!;
 
 unlink 'last-tape','next-full';
+# We are the last tape to have been backed up
 rename 'TAPEID','last-tape' or die $!;
 rename 'this-md5sums',"md5sums.$fsys" or die $!;
 rename 'log',"log.$fsys" or die $!;