File: backuplib.pl

package info (click to toggle)
chiark-utils 4.0.0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 784 kB
  • ctags: 262
  • sloc: perl: 2,568; ansic: 864; makefile: 339; sh: 111
file content (229 lines) | stat: -rw-r--r-- 6,217 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# backuplib.pl
# core common routines
#
# 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 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.
#
# 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

require IO::File;

$nice='nice ' if !defined $nice;

sub printdate () {
    print scalar(localtime),"\n";
}

# Set status info -- we write the current status to a file 
# so if we hang or crash the last thing written to the file
# will tell us where we were when things went pear-shaped.
sub setstatus ($) {
    open S, ">this-status.new" or die $!;
    print S $_[0],"\n" or die $!;
    close S or die $!;
    rename "this-status.new","this-status" or die $!;
}

# startprocess, endprocesses, killprocesses are 
# used to implement the funky pipeline stuff.
sub startprocess ($$$) {
    my ($i,$o,$c) = @_;
    print LOG "  $c\n" or die $!;
    print "  $c\n" or die $!;
    defined($p= fork) or die $!;
    if ($p) { $processes{$p}= $c; return; }
    open STDIN,"$i" or die "$c stdin $i: $!";
    open STDOUT,"$o" or die "$c stdout $o: $!";
    &closepipes;
    exec $c; die "$c: $!";
}

sub rewind_raw () {
    runsystem("mt -f $tape rewind");
}

sub readtapeid_raw () {
    open T, ">>TAPEID" or die $!; close T;
    unlink 'TAPEID' or die $!;
    rewind_raw();
    system "mt -f $tape setblk $blocksizebytes"; $? and die $?;
    system "dd if=$tape bs=${blocksize}b count=10 ".
	   "| 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 $?;
}

sub endprocesses () {
    while (keys %processes) {
	$p= waitpid(-1,0) or die "wait: $!";
	if (!exists $processes{$p}) { warn "unknown pid exited: $p, code $?\n"; next; }
	$c= $processes{$p};
	delete $processes{$p};
	$? && die "error: command gave code $?: $c\n";
    }
    pboth("  ok\n");
}

sub killprocesses {
    for $p (keys %processes) {
	kill 15,$p or warn "kill process $p: $!";
    }
    undef %processes;
}

# Read a fsys.foo filesystem group definition file.
# Syntax is: empty lines and those beginning with '#' are ignored.
# Trailing whitespace is ignored. Lines of the form 'prefix foo bar'
# are handled specially, as arex lines 'exclude regexp'; otherwise 
# we just shove the line into @fsys and let parsefsys deal with it.

sub readfsysfile ($) {
    my ($fn) = @_;
    my ($fh,$sfn);
    $fh= new IO::File "$fn", "r" or die "cannot open fsys file $fn ($!).\n";
    for (;;) {
	$!=0; $_= <$fh> or die "unexpected EOF in $fn ($!)\n";
	chomp; s/\s*$//;
	last if m/^end$/;
	next unless m/\S/;
	next if m/^\#/;
	if (m/^prefix\s+(\w+)\s+(\S.*\S)$/) {
	    $prefix{$1}= $2;
	} elsif (m/^prefix\-df\s+(\w+)\s+(\S.*\S)$/) {
	    $prefixdf{$1}= $2;
	} elsif (m/^excludedir\s+(\S.*\S)$/) {
            push @excldir,$1;
        } elsif (m/^exclude\s+(\S.*\S)$/) {
            push @excl,$1;
	} elsif (m/^include\s+(\S.*\S)$/) {
	    $sfn = $1;
	    $sfn =~ s/^\./fsys./;
	    $sfn = "$etc/$sfn" unless $sfn =~ m,^/,;
	    readfsysfile($sfn);
        } else {
	    push @fsys,$_;
	}
    }
    close $fh or die $!;
}

sub readfsys ($) {
    my ($fsnm) = @_;
    my ($fsf);
    $fsf= "$etc/fsys.$fsnm";
    stat $fsf or die "Filesystems $fsnm unknown ($!).\n";
    readfsysfile($fsf);
}

# Parse a line from a filesystem definition file. We expect the line
# to be in $tf.
sub parsefsys () {
    my ($dopts,$dopt);
    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>';
	$pcstr= '';
	stat $atf or die "stat $atf: $!";
	-d _ or die "not a dir: $atf";
	$rstr= '';
    } 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 ?";
    }

    $dev = $atf =~ s,^(.*)\:,, ? $1 : '';

    undef %dopt;
    foreach $dopt (split /\,/,$dopts) {
	if (grep { $dopt eq $_ } qw(gz)) {
	    $dopt{$dopt}= 'y';
	} elsif ($dopt =~ m/\=/ && grep { $` eq $_ } qw(gz)) {
	    $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 {
	    $$gzo= '';
	}
    }
}

sub openlog () {
    unlink 'log';
    $u= umask(007);
    open LOG, ">log" or die $!;
    umask $u;
    select(LOG); $|=1; select(STDOUT);
}

$SIG{'__DIE__'}= 'killprocesses';

1;