chiark / gitweb /
c4856d052ff2f399684d0d0878a125924e74e7f3
[chiark-utils.git] / backup / checkallused
1 #!/usr/bin/perl
2 # checkallused
3 # check that the configuration is sane and backs up everything it should
4 #
5 # This file is part of chiark backup, a system for backing up GNU/Linux and
6 # other UN*X-compatible machines, as used on chiark.greenend.org.uk.
7 #
8 # chiark backup is:
9 #  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
10 #  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
11 #
12 # This is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2, or (at your option) any later version.
15 #
16 # This is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19 # details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write to the Free Software Foundation, Inc.,
23 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 # All filesystems must either be backed up in both full and
26 # incremental dumps or listed as exceptions.
27
28 BEGIN {
29     $etc= '/etc/chiark-backup';
30     require "$etc/settings.pl";
31     require 'backuplib.pl';
32 }
33
34 $|=1;
35
36 open X,'last-tape' or die $!;
37 chomp($tape= <X>);
38 close X or die $!;
39
40 while (!defined $tapedone{$tape}) {
41     open X,"$etc/tape.$tape" or die "$tape $!";
42     $fsg=''; $next='';
43     for (;;) {
44         $_= <X> or die $1; chomp; s/\s*$//;
45         last if m/^end$/;
46         next unless m/\S/;
47         next if m/^\#/;
48         if (m/^filesystems (\w+)$/) { $fsg= $1; }
49         elsif (m/^next (\w+)$/) { $next=$1; }
50         else { die "$tape $_ ?"; }
51     }
52     length $fsg or die "$tape $!";
53     length $next or die "$tape $!";
54     push @{$fsgdone{$fsg}}, $tape;
55     $tapedone{$tape}=1;
56     $tape= $next;
57 }
58
59 sub checkdevspec ($$$) {
60     my ($atf,$devspec,$why) = @_;
61     print STDERR "checkdevspec >$atf|$devspec|$why<\n";
62     push @{ $devspec{$atf}{$devspec} }, $why;
63 }
64
65 for $fsg (sort keys %fsgdone) {
66     print "filesystem group $fsg: ".join(' ',@{$fsgdone{$fsg}}).":\n ";
67     @fsys= ();
68     readfsys($fsg);
69     for $tf (@fsys) {
70         parsefsys();
71         $pstr= "$pcstr$atf";
72         &e("dumped twice ($backed{$pstr}, $fsg): $pstr")
73             if defined $backed{$pstr};
74         $backed{$pstr}= $fsg;
75         checkdevspec($pstr,"$pcstr$dev","filesystem group $fsg")
76             if length $dev;
77         print " $pstr";
78     }
79     print "\n";
80 }
81
82 print "incremental group:\n ";
83 @fsys= ();
84 readfsys('all');
85 for $tf (@fsys) {
86     parsefsys();
87     $pstr= "$pcstr$atf";
88     $incrd{$pstr}= $fsg;
89     checkdevspec($pstr,"$pcstr$dev","incremental group") if length $dev;
90     print " $pstr";
91 }
92 print "\n";
93
94 for $pfx ('', sort keys %prefix) {
95     $rstr= length($pfx) ? $prefix{$pfx}.' ' : '';
96     $dfstr= exists($prefixdf{$pfx}) ? $prefixdf{$pfx} :
97         'df --no-sync -xiso9660 -xnfs -xproc';
98     $cmd= "$rstr $dfstr";
99     open X, "$cmd |" or die $!;
100     $_= <X>; m/^Filesystem/ or die "$cmd => $_ ?";
101     $prefix= length($pfx) ? $pfx : '<local>';
102     $pcstr= length($pfx) ? "$pfx:" : '';
103     print "mount points: $prefix:";
104     while (<X>) {
105         chomp;
106         next if m,^procfs\s,;
107         m,^/dev/(\S+)\s.*\s(/\S*)\s*$, or die "$_ ?";
108         ($dev,$mp) = ($1,$2);
109         checkdevspec("$pcstr$mp","$pcstr/dev/$dev","df");
110         $mounted{"$pcstr$mp"}="$pcstr$dev"; print " $1-$2";
111         if (defined($backto= $backed{"$pcstr$mp"})) {
112             if (m,^/dev/\S+\s+\d+\s+(\d+)\s,) {
113                 $usedkb{$backto} += $1;
114             } else {
115                 $usedkb{$backto} += 0;
116                 $unkkb{$backto} .= " + $prefix:$mp";
117             }
118         }
119     }
120     print "\n";
121     $!=0; close(X); $? and die "$? $!";
122 }
123
124 foreach $fsg (keys %usedkb) {
125     print "filesystem group $fsg: $usedkb{$fsg} 1K-blocks$unkkb{$fsg}\n";
126 }
127
128 foreach $dsk (keys %devspec) {
129     if (keys %{ $devspec{$dsk} } != 1) {
130         foreach $devspec (keys %{ $devspec{$dsk} }) {
131             &e("inconsistent devices for $dsk: $devspec (".
132                join(', ', @{ $devspec{$dsk}{$devspec} }).")");
133         }
134     }
135 }
136
137 # We check that all mounted filesystems are dumped and all
138 # filesystems to be dumped are mounted. The expected-diffs
139 # config file allows us to make exceptions.
140 # eg: 
141 # #expect disk2 to be mounted but not dumped
142 # !/disk2
143 # # CD may or may not be mounted but should not be dumped in either case
144 # ?/cdrom
145
146 open Z,"$etc/expected-diffs" or die $!;
147 for (;;) {
148     $_= <Z> or die; chomp; s/\s*$//;
149     last if m/^end$/;
150     next unless m/^\S/;
151     next if m/^\#/;
152     if (s/^\?//) {
153         print "non-permanent filesystem expected not to be dumped: $_\n";
154         if (defined($mounted{$_})) {
155             delete $mounted{$_};
156         }
157     } elsif (s/^\!//) {
158         &e("expected not to be dumped, but not a mount point: $_")
159             unless defined($mounted{$_});
160         print "filesystem expected not to be dumped: $_\n";
161         delete $mounted{$_};
162     } else {
163         &e("non-filesystem expected to be dumped is mounted: $_ on $mounted{$_}")
164             if defined($mounted{$_});
165         $mounted{$_}= 'expected-diffs';
166         print "non-filesystem expected to be dumped: $_\n";
167     }
168 }
169     
170 for $fs (sort keys %backed) { length($mounted{$fs}) || &e("dumped ($backed{$fs}), not a mount point: $fs"); }
171 for $fs (sort keys %incrd) { length($mounted{$fs}) || &e("increm'd ($incrd{$fs}), not a mount point: $fs"); }
172 for $fs (sort keys %mounted) { length($backed{$fs}) || &e("mount point ($mounted{$fs}), not dumped: $fs"); }
173 for $fs (sort keys %mounted) { length($incrd{$fs}) || &e("mount point ($mounted{$fs}), not increm'd: $fs"); }
174
175 $emsg.= "configuration ok\n" unless $e;
176 print STDERR $emsg;
177 exit($e);
178
179 sub e { $emsg.="** @_\n"; $e=1; }