chiark / gitweb /
do not placate -Wpointer-sign
[chiark-utils.git] / backup / backuplib.pl
index 279cbd6ec0e782c2355df1693d841aa2794bf497..cbbe6f4619b5612f248eb86cc70289ed6ab773dc 100644 (file)
@@ -5,12 +5,13 @@
 # other UN*X-compatible machines, as used on chiark.greenend.org.uk.
 #
 # chiark backup is:
-#  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
+#  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 2, or (at your option) any later version.
+# 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
 # details.
 #
 # You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# 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.
 
 require IO::File;
 
+$nice='nice ' if !defined $nice;
+
 sub printdate () {
     print scalar(localtime),"\n";
 }
@@ -41,8 +44,7 @@ sub setstatus ($) {
 # used to implement the funky pipeline stuff.
 sub startprocess ($$$) {
     my ($i,$o,$c) = @_;
-    print LOG "  $c\n" or die $!;
-    print "  $c\n" or die $!;
+    pboth("  $c\n");
     defined($p= fork) or die $!;
     if ($p) { $processes{$p}= $c; return; }
     open STDIN,"$i" or die "$c stdin $i: $!";
@@ -52,7 +54,7 @@ sub startprocess ($$$) {
 }
 
 sub rewind_raw () {
-    system "mt -f $tape rewind"; $? and die $?;
+    runsystem("mt -f $tape rewind");
 }
 
 sub readtapeid_raw () {
@@ -64,11 +66,34 @@ sub readtapeid_raw () {
           "| tar -b$blocksize -vvxf - TAPEID";
 }
 
+sub runsystem ($) {
+    pboth("    $_[0]\n");
+    system $_[0];
+    $? and die $?;
+}
+
+sub pboth ($) {
+    my ($str) = @_;
+    print LOG $str or die $!;
+    print $str or die $!;
+}
+
+sub nexttapefile ($) {
+    my ($what) = @_;
+    $currenttapefilenumber++;
+    $currenttapefilename= $what;
+    pboth(sprintf "writing tape file #%d (mt fsf %d): %s\n",
+         $currenttapefilenumber, $currenttapefilenumber-1, $what);
+}
+
 sub writetapeid ($$) {
     open T, ">TAPEID" or die $!;
     print T "$_[0]\n$_[1]\n" or die $!;
     close T or die $!;
 
+    $currenttapefilenumber= 0;
+    nexttapefile('TAPEID');
+
     system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
     system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10";
     $? and die $?;
@@ -82,8 +107,7 @@ sub endprocesses () {
        delete $processes{$p};
        $? && die "error: command gave code $?: $c\n";
     }
-    print LOG "  ok\n" or die $!;
-    print "  ok\n" or die $!;
+    pboth("  ok\n");
 }
 
 sub killprocesses {
@@ -113,6 +137,8 @@ sub readfsysfile ($) {
            $prefix{$1}= $2;
        } elsif (m/^prefix\-df\s+(\w+)\s+(\S.*\S)$/) {
            $prefixdf{$1}= $2;
+       } elsif (m/^snap(?:\=(\w+))?\s+(\w+)\s+(\w+)$/) {
+            push @excldir,$1;
        } elsif (m/^excludedir\s+(\S.*\S)$/) {
             push @excldir,$1;
         } elsif (m/^exclude\s+(\S.*\S)$/) {
@@ -141,37 +167,109 @@ sub readfsys ($) {
 # to be in $tf.
 sub parsefsys () {
     my ($dopts,$dopt);
-    if ($tf =~ m#^(/\S*)\s+(\w+)([,0-9a-z]+)$#) {
-        # Line of form '/file/system   dumptype[,options]'
+    if ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)$#) {
+        # Line of form '[/device:]/file/system dumptype[,options]'
        $atf= $1;
        $tm= $2;
        $dopts= $3;
        $prefix= '<local>';
-       stat $atf or die "stat $atf: $!";
-       -d _ or die "not a dir: $atf";
+       $pcstr= '';
        $rstr= '';
-    } elsif ($tf =~ m#^(/\S*)\s+(\w+)([,0-9a-z]+)\s+(\w+)$#) {
-        # Line of form '/file/system dumptype[,options] prefix'
+    } elsif ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)\s+(\w+)$#) {
+        # Line of form '[/device:]/file/system dumptype[,options] prefix'
         # (used for remote backups)
        $atf= $1;
        $tm= $2;
        $dopts= $3;
        $prefix= $4;
+       $pcstr= "$prefix:";
        defined($prefix{$prefix}) or die "prefix $prefix in $tf ?\n";
        $rstr= $prefix{$prefix}.' ';
     } else {
        die "fsys $tf ?";
     }
+
+    $fsidstr= $pcstr.$atf;
+    $fsidstr =~ s/[,+]/+$&/g;
+    $fsidstr =~ s#/#,#g;
+    $fsidfile= "/var/lib/chiark-backup/incstamp,$fsidstr";
+
+    $dev = $atf =~ s,^(.*)\:,, ? $1 : '';
+
+    if (!length $pcstr) {
+       stat $atf or die "stat $atf: $!";
+       -d _ or die "not a dir: $atf";
+    }
+
     undef %dopt;
     foreach $dopt (split /\,/,$dopts) {
-       if (grep { $dopt eq $_ } qw(gz)) {
+       if (grep { $dopt eq $_ } qw(gz noinc)) {
            $dopt{$dopt}= 'y';
-       } elsif ($dopt =~ m/\=/ && grep { $` eq $_ } qw(gz)) {
+       } elsif (grep { $dopt eq $_ } qw(snap)) {
+           $dopt{$dopt}= $dopt;
+       } elsif ($dopt =~ m/\=/ && grep { $` eq $_ } qw(gz snap)) {
            $dopt{$`}= $';
+       } elsif (length $dopt) {
+           die "unknown option $dopt (in $dopts $tf)";
+       }
+    }
+
+    my ($gzo);
+    foreach $gzo (qw(gz gzi)) {
+       if ($dopt{$gzo} eq 'y') {
+           $$gzo= '1';
+       } elsif ($dopt{$gzo} =~ m/^\d$/) {
+           $$gzo= $dopt{$gzo};
+       } elsif (defined $dopt{$gzo}) {
+           die "$tf bad $gzo";
        } else {
-           die "unknown option $dopt";
+           $$gzo= '';
        }
     }
+
+    if (length $dopt{'snap'}) {
+       length $dev or die "$pcstr:$atf no device but needed for snap";
+    }
+}
+
+sub execute ($) {
+    pboth("  $_[0]\n");
+    system $_[0]; $? and die "$_[0] $?";
+}
+
+sub prepfsys () {
+    $dev_print= $dev;
+    $atf_print= $atf;
+    
+    if (length $dopt{'snap'}) {
+       
+       system('snap-drop'); $? and die $?;
+       
+       $snapscripts= '/etc/chiark-backup/snap';
+       $snapbase= "$rstr $snapscripts/$dopt{'snap'}";
+       $snapargs= "/var/lib/chiark-backup";
+
+       $snapsnap= "$snapbase snap $snapargs $dev $atf";
+       $snapdrop= "$snapbase drop $snapargs";
+
+       open SD, ">snap-drop.new" or die $!;
+       print SD $snapdrop,"\n" or die $!;
+       close SD or die $!;
+       rename "snap-drop.new","snap-drop" or die $!;
+
+       execute($snapsnap);
+
+       $dev_nosnap= $dev;
+       $atf_nosnap= $atf;
+       $dev= "/var/lib/chiark-backup/snap-device";
+       $atf= "/var/lib/chiark-backup/snap-mount";
+    }
+}
+
+sub finfsys () {
+    if (length $dopt{'snap'}) {
+       system('snap-drop'); $? and die $?;
+    }
 }
 
 sub openlog () {