4 # This file is part of chiark backup, a system for backing up GNU/Linux and
5 # other UN*X-compatible machines, as used on chiark.greenend.org.uk.
8 # Copyright (C) 1997-1998,2000-2001,2007
9 # Ian Jackson <ian@chiark.greenend.org.uk>
10 # Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
12 # This is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 3, or (at your option) any later version.
16 # This is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, consult the Free Software Foundation's
23 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
27 $nice='nice ' if !defined $nice;
30 print scalar(localtime),"\n";
33 # Set status info -- we write the current status to a file
34 # so if we hang or crash the last thing written to the file
35 # will tell us where we were when things went pear-shaped.
37 open S, ">this-status.new" or die $!;
38 print S $_[0],"\n" or die $!;
40 rename "this-status.new","this-status" or die $!;
43 # startprocess, endprocesses, killprocesses are
44 # used to implement the funky pipeline stuff.
45 sub startprocess ($$$) {
48 defined($p= fork) or die $!;
49 if ($p) { $processes{$p}= $c; return; }
50 open STDIN,"$i" or die "$c stdin $i: $!";
51 open STDOUT,"$o" or die "$c stdout $o: $!";
53 exec $c; die "$c: $!";
57 runsystem("mt -f $tape rewind");
60 sub readtapeid_raw () {
61 open T, ">>TAPEID" or die $!; close T;
62 unlink 'TAPEID' or die $!;
64 system "mt -f $tape setblk $blocksizebytes"; $? and die $?;
65 system "dd if=$tape bs=${blocksize}b count=10 ".
66 "| tar -b$blocksize -vvxf - TAPEID";
77 print LOG $str or die $!;
81 sub nexttapefile ($) {
83 $currenttapefilenumber++;
84 $currenttapefilename= $what;
85 pboth(sprintf "writing tape file #%d (mt fsf %d): %s\n",
86 $currenttapefilenumber, $currenttapefilenumber-1, $what);
89 sub writetapeid ($$) {
90 open T, ">TAPEID" or die $!;
91 print T "$_[0]\n$_[1]\n" or die $!;
94 $currenttapefilenumber= 0;
95 nexttapefile('TAPEID');
97 system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
98 system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10";
102 sub endprocesses () {
103 while (keys %processes) {
104 $p= waitpid(-1,0) or die "wait: $!";
105 if (!exists $processes{$p}) { warn "unknown pid exited: $p, code $?\n"; next; }
107 delete $processes{$p};
108 $? && die "error: command gave code $?: $c\n";
114 for $p (keys %processes) {
115 kill 15,$p or warn "kill process $p: $!";
120 # Read a fsys.foo filesystem group definition file.
121 # Syntax is: empty lines and those beginning with '#' are ignored.
122 # Trailing whitespace is ignored. Lines of the form 'prefix foo bar'
123 # are handled specially, as arex lines 'exclude regexp'; otherwise
124 # we just shove the line into @fsys and let parsefsys deal with it.
126 sub readfsysfile ($) {
129 $fh= new IO::File "$fn", "r" or die "cannot open fsys file $fn ($!).\n";
131 $!=0; $_= <$fh> or die "unexpected EOF in $fn ($!)\n";
136 if (m/^prefix\s+(\w+)\s+(\S.*\S)$/) {
138 } elsif (m/^prefix\-df\s+(\w+)\s+(\S.*\S)$/) {
140 } elsif (m/^snap(?:\=(\w+))?\s+(\w+)\s+(\w+)$/) {
142 } elsif (m/^excludedir\s+(\S.*\S)$/) {
144 } elsif (m/^exclude\s+(\S.*\S)$/) {
146 } elsif (m/^include\s+(\S.*\S)$/) {
148 $sfn =~ s/^\./fsys./;
149 $sfn = "$etc/$sfn" unless $sfn =~ m,^/,;
161 $fsf= "$etc/fsys.$fsnm";
162 stat $fsf or die "Filesystems $fsnm unknown ($!).\n";
166 # Parse a line from a filesystem definition file. We expect the line
170 if ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)$#) {
171 # Line of form '[/device:]/file/system dumptype[,options]'
178 } elsif ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)\s+(\w+)$#) {
179 # Line of form '[/device:]/file/system dumptype[,options] prefix'
180 # (used for remote backups)
186 defined($prefix{$prefix}) or die "prefix $prefix in $tf ?\n";
187 $rstr= $prefix{$prefix}.' ';
192 $fsidstr= $pcstr.$atf;
193 $fsidstr =~ s/[,+]/+$&/g;
195 $fsidfile= "/var/lib/chiark-backup/incstamp,$fsidstr";
197 $dev = $atf =~ s,^(.*)\:,, ? $1 : '';
199 if (!length $pcstr) {
200 stat $atf or die "stat $atf: $!";
201 -d _ or die "not a dir: $atf";
205 foreach $dopt (split /\,/,$dopts) {
206 if (grep { $dopt eq $_ } qw(gz noinc)) {
208 } elsif (grep { $dopt eq $_ } qw(snap)) {
210 } elsif ($dopt =~ m/\=/ && grep { $` eq $_ } qw(gz snap)) {
212 } elsif (length $dopt) {
213 die "unknown option $dopt (in $dopts $tf)";
218 foreach $gzo (qw(gz gzi)) {
219 if ($dopt{$gzo} eq 'y') {
221 } elsif ($dopt{$gzo} =~ m/^\d$/) {
223 } elsif (defined $dopt{$gzo}) {
230 if (length $dopt{'snap'}) {
231 length $dev or die "$pcstr:$atf no device but needed for snap";
237 system $_[0]; $? and die "$_[0] $?";
244 if (length $dopt{'snap'}) {
246 system('snap-drop'); $? and die $?;
248 $snapscripts= '/etc/chiark-backup/snap';
249 $snapbase= "$rstr $snapscripts/$dopt{'snap'}";
250 $snapargs= "/var/lib/chiark-backup";
252 $snapsnap= "$snapbase snap $snapargs $dev $atf";
253 $snapdrop= "$snapbase drop $snapargs";
255 open SD, ">snap-drop.new" or die $!;
256 print SD $snapdrop,"\n" or die $!;
258 rename "snap-drop.new","snap-drop" or die $!;
264 $dev= "/var/lib/chiark-backup/snap-device";
265 $atf= "/var/lib/chiark-backup/snap-mount";
270 if (length $dopt{'snap'}) {
271 system('snap-drop'); $? and die $?;
278 open LOG, ">log" or die $!;
280 select(LOG); $|=1; select(STDOUT);
283 $SIG{'__DIE__'}= 'killprocesses';