chiark / gitweb /
8fb446831687dca891a236d3519ab70d5e72e9c4
[chiark-utils.git] / backup / checkallused
1 #!/usr/bin/perl
2
3 # Read all the configuration files and check that all filesystems
4 # are either backed up in both full and incremental dumps or 
5 # listed as exceptions.
6
7 BEGIN {
8     $etc= '/etc/backup';
9     require "$etc/settings.pl";
10     require 'backuplib.pl';
11 }
12
13 $|=1;
14
15 open X,'last-tape' or die $!;
16 chomp($tape= <X>);
17 close X or die $!;
18
19 while (!defined $tapedone{$tape}) {
20     open X,"$etc/tape.$tape" or die "$tape $!";
21     $fsg=''; $next='';
22     for (;;) {
23         $_= <X> or die $1; chomp; s/\s*$//;
24         last if m/^end$/;
25         next unless m/\S/;
26         next if m/^\#/;
27         if (m/^filesystems (\w+)$/) { $fsg= $1; }
28         elsif (m/^next (\w+)$/) { $next=$1; }
29         else { die "$tape $_ ?"; }
30     }
31     length $fsg or die "$tape $!";
32     length $next or die "$tape $!";
33     push @{$fsgdone{$fsg}}, $tape;
34     $tapedone{$tape}=1;
35     $tape= $next;
36 }
37
38 for $fsg (sort keys %fsgdone) {
39     print "filesystem group $fsg: ".join(' ',@{$fsgdone{$fsg}}).":\n ";
40     @fsys= ();
41     readfsys($fsg);
42     for $tf (@fsys) {
43         parsefsys();
44         $pstr= $prefix ne '<local>' ? "$prefix:$atf" : $atf;
45         &e("dumped twice ($backed{$pstr}, $fsg): $pstr")
46             if defined $backed{$pstr};
47         $backed{$pstr}= $fsg;
48         print " $pstr";
49     }
50     print "\n";
51 }
52
53 print "incremental group:\n ";
54 @fsys= ();
55 readfsys('all');
56 for $tf (@fsys) {
57     parsefsys();
58     $pstr= $prefix ne '<local>' ? "$prefix:$atf" : $atf;
59     $incrd{$pstr}= $fsg;
60     print " $pstr";
61 }
62 print "\n";
63
64 for $pfx ('', sort keys %prefix) {
65     $rstr= length($pfx) ? $prefix{$pfx}.' ' : '';
66     $dfstr= exists($prefixdf{$pfx}) ? $prefixdf{$pfx} :
67         'df --no-sync -xiso9660 -xnfs -xproc';
68     $cmd= "$rstr $dfstr";
69     open X, "$cmd |" or die $!;
70     $_= <X>; m/^Filesystem/ or die "$cmd => $_ ?";
71     $ppstr= length($pfx) ? $pfx : '<local>';
72     $pstr= length($pfx) ? "$pfx:" : '';
73     print "mount points: $ppstr:";
74     while (<X>) {
75         chomp;
76         next if m,^procfs\s,;
77         m,^/dev/(\S+)\s.*\s(/\S*)\s*$, or die "$_ ?";
78         ($dev,$mp) = ($1,$2);
79         $mounted{"$pstr$mp"}="$pstr$dev"; print " $1-$2";
80         if (defined($backto= $backed{"$pstr$mp"})) {
81             if (m,^/dev/\S+\s+\d+\s+(\d+)\s,) {
82                 $usedkb{$backto} += $1;
83             } else {
84                 $usedkb{$backto} += 0;
85                 $unkkb{$backto} .= " + $pstr$mp";
86             }
87         }
88     }
89     print "\n";
90     $!=0; close(X); $? and die "$? $!";
91 }
92
93 foreach $fsg (keys %usedkb) {
94     print "filesystem group $fsg: $usedkb{$fsg} 1K-blocks$unkkb{$fsg}\n";
95 }
96
97 # We check that all mounted filesystems are dumped and all
98 # filesystems to be dumped are mounted. The expected-diffs
99 # config file allows us to make exceptions.
100 # eg: 
101 # #expect disk2 to be mounted but not dumped
102 # !/disk2
103 # # CD may or may not be mounted but should not be dumped in either case
104 # ?/cdrom
105
106 open Z,"$etc/expected-diffs" or die $!;
107 for (;;) {
108     $_= <Z> or die; chomp; s/\s*$//;
109     last if m/^end$/;
110     next unless m/^\S/;
111     next if m/^\#/;
112     if (s/^\?//) {
113         print "non-permanent filesystem expected not to be dumped: $_\n";
114         if (defined($mounted{$_})) {
115             delete $mounted{$_};
116         }
117     } elsif (s/^\!//) {
118         &e("expected not to be dumped, but not a mount point: $_")
119             unless defined($mounted{$_});
120         print "filesystem expected not to be dumped: $_\n";
121         delete $mounted{$_};
122     } else {
123         &e("non-filesystem expected to be dumped is mounted: $_ on $mounted{$_}")
124             if defined($mounted{$_});
125         $mounted{$_}= 'expected-diffs';
126         print "non-filesystem expected to be dumped: $_\n";
127     }
128 }
129     
130 for $fs (sort keys %backed) { length($mounted{$fs}) || &e("dumped ($backed{$fs}), not a mount point: $fs"); }
131 for $fs (sort keys %incrd) { length($mounted{$fs}) || &e("increm'd ($incrd{$fs}), not a mount point: $fs"); }
132 for $fs (sort keys %mounted) { length($backed{$fs}) || &e("mount point ($mounted{$fs}), not dumped: $fs"); }
133 for $fs (sort keys %mounted) { length($incrd{$fs}) || &e("mount point ($mounted{$fs}), not increm'd: $fs"); }
134
135 $emsg.= "configuration ok\n" unless $e;
136 print STDERR $emsg;
137 exit($e);
138
139 sub e { $emsg.="** @_\n"; $e=1; }