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 Ian Jackson <ian@chiark.greenend.org.uk>
9 # Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
11 # This is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2, or (at your option) any later version.
15 # This is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 $nice='nice ' if !defined $nice;
29 print scalar(localtime),"\n";
32 # Set status info -- we write the current status to a file
33 # so if we hang or crash the last thing written to the file
34 # will tell us where we were when things went pear-shaped.
36 open S, ">this-status.new" or die $!;
37 print S $_[0],"\n" or die $!;
39 rename "this-status.new","this-status" or die $!;
42 # startprocess, endprocesses, killprocesses are
43 # used to implement the funky pipeline stuff.
44 sub startprocess ($$$) {
47 defined($p= fork) or die $!;
48 if ($p) { $processes{$p}= $c; return; }
49 open STDIN,"$i" or die "$c stdin $i: $!";
50 open STDOUT,"$o" or die "$c stdout $o: $!";
52 exec $c; die "$c: $!";
56 runsystem("mt -f $tape rewind");
59 sub readtapeid_raw () {
60 open T, ">>TAPEID" or die $!; close T;
61 unlink 'TAPEID' or die $!;
63 system "mt -f $tape setblk $blocksizebytes"; $? and die $?;
64 system "dd if=$tape bs=${blocksize}b count=10 ".
65 "| tar -b$blocksize -vvxf - TAPEID";
76 print LOG $str or die $!;
80 sub nexttapefile ($) {
82 $currenttapefilenumber++;
83 $currenttapefilename= $what;
84 pboth(sprintf "writing tape file #%d (mt fsf %d): %s\n",
85 $currenttapefilenumber, $currenttapefilenumber-1, $what);
88 sub writetapeid ($$) {
89 open T, ">TAPEID" or die $!;
90 print T "$_[0]\n$_[1]\n" or die $!;
93 $currenttapefilenumber= 0;
94 nexttapefile('TAPEID');
96 system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
97 system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10";
101 sub endprocesses () {
102 while (keys %processes) {
103 $p= waitpid(-1,0) or die "wait: $!";
104 if (!exists $processes{$p}) { warn "unknown pid exited: $p, code $?\n"; next; }
106 delete $processes{$p};
107 $? && die "error: command gave code $?: $c\n";
113 for $p (keys %processes) {
114 kill 15,$p or warn "kill process $p: $!";
119 # Read a fsys.foo filesystem group definition file.
120 # Syntax is: empty lines and those beginning with '#' are ignored.
121 # Trailing whitespace is ignored. Lines of the form 'prefix foo bar'
122 # are handled specially, as arex lines 'exclude regexp'; otherwise
123 # we just shove the line into @fsys and let parsefsys deal with it.
125 sub readfsysfile ($) {
128 $fh= new IO::File "$fn", "r" or die "cannot open fsys file $fn ($!).\n";
130 $!=0; $_= <$fh> or die "unexpected EOF in $fn ($!)\n";
135 if (m/^prefix\s+(\w+)\s+(\S.*\S)$/) {
137 } elsif (m/^prefix\-df\s+(\w+)\s+(\S.*\S)$/) {
139 } elsif (m/^snap(?:\=(\w+))?\s+(\w+)\s+(\w+)$/) {
141 } elsif (m/^excludedir\s+(\S.*\S)$/) {
143 } elsif (m/^exclude\s+(\S.*\S)$/) {
145 } elsif (m/^include\s+(\S.*\S)$/) {
147 $sfn =~ s/^\./fsys./;
148 $sfn = "$etc/$sfn" unless $sfn =~ m,^/,;
160 $fsf= "$etc/fsys.$fsnm";
161 stat $fsf or die "Filesystems $fsnm unknown ($!).\n";
165 # Parse a line from a filesystem definition file. We expect the line
169 if ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)$#) {
170 # Line of form '[/device:]/file/system dumptype[,options]'
177 } elsif ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)\s+(\w+)$#) {
178 # Line of form '[/device:]/file/system dumptype[,options] prefix'
179 # (used for remote backups)
185 defined($prefix{$prefix}) or die "prefix $prefix in $tf ?\n";
186 $rstr= $prefix{$prefix}.' ';
191 $fsidstr= $pcstr.$atf;
192 $fsidstr =~ s/[,+]/+$&/g;
194 $fsidfile= "/var/lib/chiark-backup/incstamp,$fsidstr";
196 $dev = $atf =~ s,^(.*)\:,, ? $1 : '';
198 if (!length $pcstr) {
199 stat $atf or die "stat $atf: $!";
200 -d _ or die "not a dir: $atf";
204 foreach $dopt (split /\,/,$dopts) {
205 if (grep { $dopt eq $_ } qw(gz)) {
207 } elsif (grep { $dopt eq $_ } qw(snap)) {
209 } elsif ($dopt =~ m/\=/ && grep { $` eq $_ } qw(gz snap)) {
211 } elsif (length $dopt) {
212 die "unknown option $dopt (in $dopts $tf)";
217 foreach $gzo (qw(gz gzi)) {
218 if ($dopt{$gzo} eq 'y') {
220 } elsif ($dopt{$gzo} =~ m/^\d$/) {
222 } elsif (defined $dopt{$gzo}) {
229 if (length $dopt{'snap'}) {
230 length $dev or die "$pcstr:$atf no device but needed for snap";
236 system $_[0]; $? and die "$_[0] $?";
243 if (length $dopt{'snap'}) {
245 system('snap-drop'); $? and die $?;
247 $snapscripts= '/etc/chiark-backup/snap';
248 $snapbase= "$rstr $snapscripts/$dopt{'snap'}";
249 $snapargs= "/var/lib/chiark-backup";
251 $snapsnap= "$snapbase snap $snapargs $dev $atf";
252 $snapdrop= "$snapbase drop $snapargs";
254 open SD, ">snap-drop.new" or die $!;
255 print SD $snapdrop,"\n" or die $!;
257 rename "snap-drop.new","snap-drop" or die $!;
263 $dev= "/var/lib/chiark-backup/snap-device";
264 $atf= "/var/lib/chiark-backup/snap-mount";
269 if (length $dopt{'snap'}) {
270 system('snap-drop'); $? and die $?;
277 open LOG, ">log" or die $!;
279 select(LOG); $|=1; select(STDOUT);
282 $SIG{'__DIE__'}= 'killprocesses';