chiark / gitweb /
Portability fix: Fix on libcs where st_mtime is not a macro (ie, ones lacking st_mtim.)
[chiark-utils.git] / backup / full
index 5e6d23e0f0231c062ece1830bb9b445da6f72855..61b62668ef47ba249ad1924690a6674c68786328 100755 (executable)
@@ -1,28 +1,68 @@
 #!/usr/bin/perl
+# full
+# Main backup script - does a full dump or execs increm.  Do NOT run directly!
+#
+# This file is part of chiark backup, a system for backing up GNU/Linux and
+# other UN*X-compatible machines, as used on chiark.greenend.org.uk.
+#
+# chiark backup is:
+#  Copyright (C) 1997-1998,2000-2001,2007
+#                     Ian Jackson <ian@chiark.greenend.org.uk>
+#  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
+#
+# This is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3, or (at your option) any later version.
+#
+# This is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, consult the Free Software Foundation's
+# website at www.fsf.org, or the GNU Project website at www.gnu.org.
 
 BEGIN {
-    $etc= '/etc/backup';
+    $etc= '/etc/chiark-backup';
     require "$etc/settings.pl";
     require 'backuplib.pl';
 }
 
 $|=1;
 
+while (@ARGV) {
+    $_= shift @ARGV;
+    if (m/^\-\-no\-reten$/) {
+       $noreten=1;
+    } elsif (m/^\-\-no\-config\-check$/) {
+       $nocheck=1;
+    } else {
+       die "unknown option/argument \`$_'\n";
+    }
+}
+
 # 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 $?;
+
+openlog();
+
+if (!$nocheck) {
+    setstatus "FAILED configuration check";
+    print "Configuration check ...\n" or die $!;
+    system 'backup-checkallused'; $? and die $?;
+} else {
+    setstatus "FAILED rewinding";
+    rewind_raw();
+}
 
 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 $?;
-system "dd if=$tape bs=${blocksize}b count=10 | tar -b$blocksize -vvxf - TAPEID";
+
+readtapeid_raw();
 
 setstatus "FAILED during startup";
 
@@ -103,32 +143,34 @@ if ($incremental) {
 
 # 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 $!;
 
+if (!$noreten) {
+    setstatus "FAILED retensioning";
+    runsystem("mt -f $tape reten");
+}
+
 setstatus "FAILED writing tape ID";
 # First write the tape ID to this tape.
-open T, ">TAPEID" or die $!;
-print T "$tapeid\n" or die $!;
-close T or die $!;
 
-system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
-system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10"; $? and die $?;
+writetapeid($tapeid,$tapedesc);
 
 unlink 'this-md5sums';
 
 print "Doing $doing ...\n" or die $!;
 
 unlink 'p';
-system 'mknod p p'; $? and die $?;
+system 'mknod -m600 p p'; $? and die $?;
 
 setstatus "FAILED during dump";
 
 sub closepipes () {
     close(DUMPOR); close(TEEOR); close(BUFOR); close(FINDOR);
     close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
+    close(GZOR); close(GZOW);
+    close(DDERRR); close(DDERRW);
 }
 
 # work out a find option string that will exclude the required files    
@@ -144,16 +186,32 @@ foreach $exc (@excl) {
 # For each filesystem to be put on this tape:
 for $tf (@fsys) {
     printdate();
+    parsefsys();
+    prepfsys();
+
     pipe(FINDOR,FINDOW) or die $!;
     pipe(DUMPOR,DUMPOW) or die $!;
     pipe(TEEOR,TEEOW) or die $!;
+    pipe(TEEOR,TEEOW) or die $!;
     pipe(BUFOR,BUFOW) or die $!;
-    parsefsys();
+    pipe(DDERRR,DDERRW) or die $!;
+    
+    $bufir='TEEOR';
+    $ddcmd= "dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape 2>&1";
+
+    if ($gz) {
+       $bufir='GZOR';
+       pipe(GZOR,GZOW) or die $!;
+       $ddcmd .= " conv=sync";
+    }
     
+    nexttapefile("full $prefix:$atf_print");
+
     # We can back up via dump or cpio or zafio
+    $dumpin= '</dev/null';
     if ($tm eq 'dump') {
-       $dumpcmd= "dump 0bfu $softblocksizekb - $atf";
-       $dumpin= '</dev/null';
+       $dumplabel= $pcstr.$atf_print.'$';
+       $dumpcmd= "dump 0Lbfu $dumplabel $softblocksizekb - $atf";
     } elsif ($tm eq 'cpio') {
        startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
        $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
@@ -164,22 +222,45 @@ for $tf (@fsys) {
         # don't use verbose flag as this generates 2MB report emails :->
         $dumpcmd = "afio -b $softblocksizebytes -Zo -";
         $dumpin = '<&FINDOR';
+    } elsif ($tm eq 'ntfsimage') {
+       $dumpcmd= "ntfsimage -svvf --dirty $dev";
+    } elsif ($tm eq 'gtar') {
+       execute("$rstr touch $fsidfile+new");
+       $dumpcmd= "tar Ccfl $atf - .";
     } else {
-       die "unknown method $tm for $prefix:$atf\n";
+       die "unknown method $tm for $prefix:$atf_print\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';
-    startprocess '<&TEEOR','>&BUFOW','writebuffer';
-    startprocess '<&BUFOR','>/dev/null'
-       ,"dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape";
+    #    dump <$dumpin | tee p [| gzip] | writebuffer | dd >/dev/null
+
+    startprocess '<p','>>this-md5sums',"$nice md5sum";
+    startprocess $dumpin,'>&DUMPOW',"$nice ".$rstr.$dumpcmd;
+    startprocess '<&DUMPOR','>&TEEOW',"$nice tee p";
+    if ($gz) {
+       startprocess '<&TEEOR','>&GZOW',"$nice gzip -v$gz";
+    }
+    startprocess "<&$bufir",'>&BUFOW',"$nasty writebuffer";
+    startprocess '<&DDERRR','>/dev/null',"$nice tee dderr >&2";
+    startprocess '<&BUFOR','>&DDERRW',"$nasty $ddcmd";
     closepipes();
     endprocesses();
+
+    open DDERR, "dderr" or die $!;
+    defined(read DDERR,$_,1023) or die $!;
+    close DDERR;
+    m/\n(\d+)\+0 records out\n/ or die ">$dderr< ?";
+    push @tapefilesizes, [ $1, $currenttapefilename ];
+    $totalrecords += $1;
+    pboth("total blocks written so far: $totalrecords\n");
+
+    if ($tm eq 'gtar') {
+       execute("$rstr mv -f $fsidfile+new $fsidfile");
+    }  
+    
+    finfsys();
 }
 
 # The backup should now be complete; verify it
@@ -187,25 +268,48 @@ for $tf (@fsys) {
 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 $?;
+runsystem("mt -f $tape rewind");
+runsystem("mt -f $ntape fsf 1");
 
 # Check the md5sums match for each filesystem on the tape
 open S,"this-md5sums" or die $!;
 for $tf (@fsys) {
     printdate();
-    chomp($orgsum= <S>);
+    parsefsys();
+    chomp($orgsum= <S>); $orgsum =~ s/\ +\-?$//;
     $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
-    chomp($csum= `dd if=$ntape ibs=$blocksizebytes | readbuffer | md5sum`);
+    $cmd= "$nasty dd if=$ntape ibs=$blocksizebytes";
+    $cmd .= " | $nasty readbuffer";
+    $cmd .= " | $nice gzip -vd" if $gz;
+    $cmd .= " | $nice md5sum";
+    pboth("  $cmd\n");
+    chomp($csum= `$cmd`);
+    $csum =~ s/\ +\-?$//;
     $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 $!;
 }
 printdate();
-system "mt -f $tape rewind"; $? and die $?;
+runsystem("mt -f $tape rewind");
 
 setstatus "FAILED during cleanup";
 
+$summary= '';
+foreach $tfs (@tapefilesizes) {
+    $summary .= sprintf "    %10d blocks for %s\n", $tfs->[0], $tfs->[1]
+}
+$summary .=
+    sprintf "    %10d blocks total (of %d bytes) plus TAPEID and headers\n",
+    $totalrecords, $blocksizebytes;
+
+pboth("size-summary:\n");
+pboth($summary);
+
+open SS, ">size-summary..new" or die $!;
+print SS $summary or die $!;
+close SS or die $!;
+rename 'size-summary..new',"size-summary.$fsys" or die $!;
+
 # Write to some status files to indicate what the backup system
 # ought to do when next invoked.
 # reset incremental backup count to 1.