chiark / gitweb /
783b14d50bf6d298ef0b62400f7ebc53a6793d8b
[chiark-utils.git] / backup / backuplib.pl
1 # backuplib.pl
2 # core common routines
3 #
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.
6 #
7 # chiark backup is:
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>
10 #
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.
14 #
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
18 # details.
19 #
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.
23
24 require IO::File;
25
26 $nice='nice ' if !defined $nice;
27
28 sub printdate () {
29     print scalar(localtime),"\n";
30 }
31
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.
35 sub setstatus ($) {
36     open S, ">this-status.new" or die $!;
37     print S $_[0],"\n" or die $!;
38     close S or die $!;
39     rename "this-status.new","this-status" or die $!;
40 }
41
42 # startprocess, endprocesses, killprocesses are 
43 # used to implement the funky pipeline stuff.
44 sub startprocess ($$$) {
45     my ($i,$o,$c) = @_;
46     print LOG "  $c\n" or die $!;
47     print "  $c\n" or die $!;
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: $!";
52     &closepipes;
53     exec $c; die "$c: $!";
54 }
55
56 sub rewind_raw () {
57     system "mt -f $tape rewind"; $? and die $?;
58 }
59
60 sub readtapeid_raw () {
61     open T, ">>TAPEID" or die $!; close T;
62     unlink 'TAPEID' or die $!;
63     rewind_raw();
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";
67 }
68
69 sub pboth ($) {
70     my ($str) = @_;
71     print LOG $str or die $!;
72     print $str or die $!;
73 }
74
75 sub nexttapefile ($) {
76     my ($what) = @_;
77     $currenttapefilenumber++;
78     $currenttapefilename= $what;
79     pboth(sprintf "writing tape file #%d (mt fsf %d): %s",
80           $currenttapefilenumber, $currenttapefilenumber-1, $what);
81 }
82
83 sub writetapeid ($$) {
84     open T, ">TAPEID" or die $!;
85     print T "$_[0]\n$_[1]\n" or die $!;
86     close T or die $!;
87
88     $currenttapefilenumber= 0;
89     nexttapefile('TAPEID');
90
91     system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
92     system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10";
93     $? and die $?;
94 }
95
96 sub endprocesses () {
97     while (keys %processes) {
98         $p= waitpid(-1,0) or die "wait: $!";
99         if (!exists $processes{$p}) { warn "unknown pid exited: $p, code $?\n"; next; }
100         $c= $processes{$p};
101         delete $processes{$p};
102         $? && die "error: command gave code $?: $c\n";
103     }
104     pboth("  ok\n");
105 }
106
107 sub killprocesses {
108     for $p (keys %processes) {
109         kill 15,$p or warn "kill process $p: $!";
110     }
111     undef %processes;
112 }
113
114 # Read a fsys.foo filesystem group definition file.
115 # Syntax is: empty lines and those beginning with '#' are ignored.
116 # Trailing whitespace is ignored. Lines of the form 'prefix foo bar'
117 # are handled specially, as arex lines 'exclude regexp'; otherwise 
118 # we just shove the line into @fsys and let parsefsys deal with it.
119
120 sub readfsysfile ($) {
121     my ($fn) = @_;
122     my ($fh,$sfn);
123     $fh= new IO::File "$fn", "r" or die "cannot open fsys file $fn ($!).\n";
124     for (;;) {
125         $!=0; $_= <$fh> or die "unexpected EOF in $fn ($!)\n";
126         chomp; s/\s*$//;
127         last if m/^end$/;
128         next unless m/\S/;
129         next if m/^\#/;
130         if (m/^prefix\s+(\w+)\s+(\S.*\S)$/) {
131             $prefix{$1}= $2;
132         } elsif (m/^prefix\-df\s+(\w+)\s+(\S.*\S)$/) {
133             $prefixdf{$1}= $2;
134         } elsif (m/^excludedir\s+(\S.*\S)$/) {
135             push @excldir,$1;
136         } elsif (m/^exclude\s+(\S.*\S)$/) {
137             push @excl,$1;
138         } elsif (m/^include\s+(\S.*\S)$/) {
139             $sfn = $1;
140             $sfn =~ s/^\./fsys./;
141             $sfn = "$etc/$sfn" unless $sfn =~ m,^/,;
142             readfsysfile($sfn);
143         } else {
144             push @fsys,$_;
145         }
146     }
147     close $fh or die $!;
148 }
149
150 sub readfsys ($) {
151     my ($fsnm) = @_;
152     my ($fsf);
153     $fsf= "$etc/fsys.$fsnm";
154     stat $fsf or die "Filesystems $fsnm unknown ($!).\n";
155     readfsysfile($fsf);
156 }
157
158 # Parse a line from a filesystem definition file. We expect the line
159 # to be in $tf.
160 sub parsefsys () {
161     my ($dopts,$dopt);
162     if ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)$#) {
163         # Line of form '[/device:]/file/system  dumptype[,options]'
164         $atf= $1;
165         $tm= $2;
166         $dopts= $3;
167         $prefix= '<local>';
168         $pcstr= '';
169         stat $atf or die "stat $atf: $!";
170         -d _ or die "not a dir: $atf";
171         $rstr= '';
172     } elsif ($tf =~ m#^(/\S*)\s+(\w+)([,=0-9a-z]*)\s+(\w+)$#) {
173         # Line of form '[/device:]/file/system dumptype[,options] prefix'
174         # (used for remote backups)
175         $atf= $1;
176         $tm= $2;
177         $dopts= $3;
178         $prefix= $4;
179         $pcstr= "$prefix:";
180         defined($prefix{$prefix}) or die "prefix $prefix in $tf ?\n";
181         $rstr= $prefix{$prefix}.' ';
182     } else {
183         die "fsys $tf ?";
184     }
185
186     $dev = $atf =~ s,^(.*)\:,, ? $1 : '';
187
188     undef %dopt;
189     foreach $dopt (split /\,/,$dopts) {
190         if (grep { $dopt eq $_ } qw(gz)) {
191             $dopt{$dopt}= 'y';
192         } elsif ($dopt =~ m/\=/ && grep { $` eq $_ } qw(gz)) {
193             $dopt{$`}= $';
194         } elsif (length $dopt) {
195             die "unknown option $dopt (in $dopts $tf)";
196         }
197     }
198
199     my ($gzo);
200     foreach $gzo (qw(gz gzi)) {
201         if ($dopt{$gzo} eq 'y') {
202             $$gzo= '1';
203         } elsif ($dopt{$gzo} =~ m/^\d$/) {
204             $$gzo= $dopt{$gzo};
205         } elsif (defined $dopt{$gzo}) {
206             die "$tf bad $gzo";
207         } else {
208             $$gzo= '';
209         }
210     }
211 }
212
213 sub openlog () {
214     unlink 'log';
215     $u= umask(007);
216     open LOG, ">log" or die $!;
217     umask $u;
218     select(LOG); $|=1; select(STDOUT);
219 }
220
221 $SIG{'__DIE__'}= 'killprocesses';
222
223 1;