chiark / gitweb /
From Peter Maydell (/u2/pmaydell/iwjbackup/) as per <E156bwm-0005xq-00@watchdragon...
[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             next;
68         }
69         if (m/^excludedir\s+(\S.*\S)$/) {
70             push @excldir,$1;
71             next;
72         }
73         if (m/^exclude\s+(\S.*\S)$/) {
74             push @excl,$1;
75             next;
76         }
77         push @fsys,$_;
78     }
79     close F or die $!;
80 }
81
82 # Parse a line from a filesystem definition file. We expect the line
83 # to be in $tf.
84 sub parsefsys () {
85     if ($tf =~ m,^(/\S*)\s+(\w+)$,) {
86         # Line of form '/file/system    dumptype'
87         $atf= $1;
88         $tm= $2;
89         $prefix= '<local>';
90         stat $atf or die "stat $atf: $!";
91         -d _ or die "not a dir: $atf";
92         $rstr= '';
93     } elsif ($tf =~ m,^(/\S*)\s+(\w+)\s+(\w+)$,) {
94         # Line of form '/file/system dumptype prefix'
95         # (used for remote backups, I think)
96         $atf= $1;
97         $tm= $2;
98         $prefix= $3;
99         defined($prefix{$prefix}) or die "prefix $prefix in $tf ?\n";
100         $rstr= $prefix{$prefix}.' ';
101     }
102 }
103
104 sub openlog () {
105     unlink 'log';
106     $u= umask(007);
107     open LOG, ">log" or die $!;
108     umask $u;
109     select(LOG); $|=1; select(STDOUT);
110 }
111
112 $SIG{'__DIE__'}= 'killprocesses';
113
114 1;