chiark / gitweb /
Merge Peter Maydell's changes.
[chiark-utils.git] / backup / backuplib.pl
1 #
2
3 # Assorted useful functions used by the backup scripts.
4
5 sub printdate () {
6     print scalar(localtime),"\n";
7 }
8
9 # Set status info -- we write the current status to a file 
10 # so if we hang or crash the last thing written to the file
11 # will tell us where we were when things went pear-shaped.
12 sub setstatus ($) {
13     open S, ">this-status.new" or die $!;
14     print S $_[0],"\n" or die $!;
15     close S or die $!;
16     rename "this-status.new","this-status" or die $!;
17 }
18
19 # startprocess, endprocesses, killprocesses are 
20 # used to implement the funky pipeline stuff.
21 sub startprocess ($$$) {
22     my ($i,$o,$c) = @_;
23     print LOG "  $c\n" or die $!;
24     print "  $c\n" or die $!;
25     defined($p= fork) or die $!;
26     if ($p) { $processes{$p}= $c; return; }
27     open STDIN,"$i" or die "$c stdin $i: $!";
28     open STDOUT,"$o" or die "$c stdout $o: $!";
29     &closepipes;
30     exec $c; die "$c: $!";
31 }
32
33 sub endprocesses () {
34     while (keys %processes) {
35         $p= waitpid(-1,0) or die "wait: $!";
36         if (!exists $processes{$p}) { warn "unknown pid exited: $p, code $?\n"; next; }
37         $c= $processes{$p};
38         delete $processes{$p};
39         $? && die "error: command gave code $?: $c\n";
40     }
41     print LOG "  ok\n" or die $!;
42     print "  ok\n" or die $!;
43 }
44
45 sub killprocesses {
46     for $p (keys %processes) {
47         kill 15,$p or warn "kill process $p: $!";
48     }
49     undef %processes;
50 }
51
52 # Read a fsys.foo filesystem group definition file.
53 # Syntax is: empty lines and those beginning with '#' are ignored.
54 # Trailing whitespace is ignored. Lines of the form 'prefix foo bar'
55 # are handled specially, as arex lines 'exclude regexp'; otherwise 
56 # we just shove the line into @fsys and let parsefsys deal with it.
57 sub readfsys ($) {
58     my ($fsnm) = @_;
59     open F, "$etc/fsys.$fsnm" or die "Filesystems $fsnm unknown ($!).\n";
60     for (;;) {
61         $_= <F> or die "unexpected EOF in $etc/fsys.$fsnm\n"; chomp; s/\s*$//;
62         last if m/^end$/;
63         next unless m/\S/;
64         next if m/^\#/;
65         if (m/^prefix\s+(\w+)\s+(\S.*\S)$/) {
66             $prefix{$1}= $2;
67         } elsif (m/^prefix\-df\s+(\w+)\s+(\S.*\S)$/) {
68             $prefixdf{$1}= $2;
69         } elsif (m/^excludedir\s+(\S.*\S)$/) {
70             push @excldir,$1;
71         } elsif (m/^exclude\s+(\S.*\S)$/) {
72             push @excl,$1;
73         } else {
74             push @fsys,$_;
75         }
76     }
77     close F or die $!;
78 }
79
80 # Parse a line from a filesystem definition file. We expect the line
81 # to be in $tf.
82 sub parsefsys () {
83     if ($tf =~ m,^(/\S*)\s+(\w+)$,) {
84         # Line of form '/file/system    dumptype'
85         $atf= $1;
86         $tm= $2;
87         $prefix= '<local>';
88         stat $atf or die "stat $atf: $!";
89         -d _ or die "not a dir: $atf";
90         $rstr= '';
91     } elsif ($tf =~ m,^(/\S*)\s+(\w+)\s+(\w+)$,) {
92         # Line of form '/file/system dumptype prefix'
93         # (used for remote backups, I think)
94         $atf= $1;
95         $tm= $2;
96         $prefix= $3;
97         defined($prefix{$prefix}) or die "prefix $prefix in $tf ?\n";
98         $rstr= $prefix{$prefix}.' ';
99     }
100 }
101
102 sub openlog () {
103     unlink 'log';
104     $u= umask(007);
105     open LOG, ">log" or die $!;
106     umask $u;
107     select(LOG); $|=1; select(STDOUT);
108 }
109
110 $SIG{'__DIE__'}= 'killprocesses';
111
112 1;