chiark / gitweb /
784d302335e3c3653c6e484cab95fed8125281d4
[chiark-utils.git] / backup / full
1 #!/usr/bin/perl
2 # full
3 # Main backup script - does a full dump or execs increm.  Do NOT run directly!
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 BEGIN {
26     $etc= '/etc/chiark-backup';
27     require "$etc/settings.pl";
28     require 'backuplib.pl';
29 }
30
31 $|=1;
32
33 while (@ARGV) {
34     $_= shift @ARGV;
35     if (m/^\-\-no\-reten$/) {
36         $noreten=1;
37     } elsif (m/^\-\-no\-config\-check$/) {
38         $nocheck=1;
39     } else {
40         die "unknown option/argument \`$_'\n";
41     }
42 }
43
44 # Check to see whether the tape.nn and fsys.nn files are sane.
45 # checkallused checks that all the filesystems mounted are in fact
46 # dumped in both full and incremental dumps.
47
48 if (!$nocheck) {
49     setstatus "FAILED configuration check";
50     print "Configuration check ...\n" or die $!;
51     system 'backup-checkallused'; $? and die $?;
52 } else {
53     setstatus "FAILED rewinding";
54     rewind_raw();
55 }
56
57 printdate();
58
59 setstatus "FAILED reading TAPEID";
60 # Try to read the tape ID from the tape into the file TAPEID
61
62 readtapeid_raw();
63
64 setstatus "FAILED during startup";
65
66 # We need some ID; if the tape has one already that takes precedence;
67 # otherwise the user might have set a tape ID that this should be
68 # by creating really-TAPEID.
69 if (open T, "TAPEID") {
70     unlink 'really-TAPEID';
71 } elsif (open T, "really-TAPEID") {
72 } else {
73     die "No TAPEID.\n";
74 }
75
76 # read the ID; it had better be a non-empty string of alphanumeric chars.
77 chomp($tapeid= <T>);
78 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
79 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
80 close T;
81
82 setstatus "FAILED at tape identity check";
83
84 # We don't let the user overwrite the tape used for the last backup.
85 if (open L, "last-tape") {
86     chomp($lasttape= <L>);
87     close L;
88 } else {
89     undef $lasttape;
90 }
91
92 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
93
94 # $tapeid identifies the individual tape; $tapedesc is its current
95 # identity and function, for printing in messages.  You can make these
96 # namespaces the same if you like, or you can make the tape.<tapeid>
97 # files be links to tape.<tapedesc> files.
98 if (defined($tapedesc= readlink "$etc/tape.$tapeid")) {
99     $tapedesc =~ s/^.*\.//;
100     $tapedesc .= "($tapeid)";
101 } else {
102     $tapedesc = $tapeid;
103 }
104
105 # Parse the appropriate tape.nn file.
106 # Format is: empty lines and lines starting '#' are ignored. Trailing
107 # whitespace is ignored. File must end with 'end' on a line by itself.
108 # Either there should be a line 'incremental' to indicate that this is
109 # a tape for incremental backups, or a pair of lines 'filesystems fsg'
110 # and 'next tapeid', indicating that this tape is part of a full 
111 # backup, containing the filesystem group fsg. 
112 undef $fsys;
113 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
114 for (;;) {
115     $_= <D> or die; chomp; s/\s+$//;
116     last if m/^end$/;
117     next unless m/\S/;
118     next if m/^\#/;
119     if (m/^filesystems (\w+)$/) {
120         $fsys= $1;
121     } elsif (m/^next (\w+)$/) {
122         $next= $1;
123     } elsif (m/^incremental$/) {
124         $incremental= 1;
125     } else {
126         die "unknown entry in tape $tapeid at line $.: $_\n";
127     }
128 }
129 close D or die $!;
130
131 # Incremental backups are handled by increm, not us.
132 if ($incremental) {
133     die "incremental tape $tapeid has next or filesystems\n"
134         if defined($next) || defined($fsys);
135     print STDERR "Incremental tape $tapeid.\n\n";
136     setstatus "FAILED during incremental startup";
137     exec "increm",$tapeid,$tapedesc;
138     die $!;
139 }
140
141 # Read the filesystem group definition (file fsys.nnn)
142 readfsys("$fsys");
143 openlog();
144
145 $doing= "dump of $fsys to tape $tapedesc in drive $tape";
146 print LOG "$doing:\n" or die $!;
147
148 if (!$noreten) {
149     setstatus "FAILED retensioning";
150     system "mt -f $tape reten"; $? and die $?;
151 }
152
153 setstatus "FAILED writing tape ID";
154 # First write the tape ID to this tape.
155
156 writetapeid($tapeid,$tapedesc);
157
158 unlink 'this-md5sums';
159
160 print "Doing $doing ...\n" or die $!;
161
162 unlink 'p';
163 system 'mknod p p'; $? and die $?;
164
165 setstatus "FAILED during dump";
166
167 sub closepipes () {
168     close(DUMPOR); close(TEEOR); close(BUFOR); close(FINDOR);
169     close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
170 }
171
172 # work out a find option string that will exclude the required files    
173 # Note that dump pays no attention to exclude options.
174 $exclopt = '';
175 foreach $exc (@excldir) {
176     $exclopt .= "-regex $exc -prune -o ";
177 }
178 foreach $exc (@excl) {
179     $exclopt .= "-regex $exc -o ";
180 }
181
182 # For each filesystem to be put on this tape:
183 for $tf (@fsys) {
184     printdate();
185     pipe(FINDOR,FINDOW) or die $!;
186     pipe(DUMPOR,DUMPOW) or die $!;
187     pipe(TEEOR,TEEOW) or die $!;
188     $bufir='TEEOR';
189
190     if ($dopt{'gz'} eq 'y') {
191         $gz= '3';
192     } elsif ($dopt{'gz'} =~ m/^\d$/) {
193         $gz= $dopt{'gz'};
194     } elsif (defined $dopt{'gz'}) {
195         die "$tf bad gz";
196     } else {
197         $gz= 0;
198     }
199     if ($gz) {
200         $bufir='GZOR';
201         pipe(GZOR,GZOW) or die $!;
202     }
203     
204     pipe(BUFOR,BUFOW) or die $!;
205     parsefsys();
206     
207     # We can back up via dump or cpio or zafio
208     if ($tm eq 'dump') {
209         $dumpcmd= "dump 0bfu $softblocksizekb - $atf";
210         $dumpin= '</dev/null';
211     } elsif ($tm eq 'cpio') {
212         startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
213         $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
214         $dumpin= '<&FINDOR';
215     } elsif ($tm eq 'zafio') {
216         # compress-each-file-then-archive using afio
217         startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf $exclopt -print";
218         # don't use verbose flag as this generates 2MB report emails :->
219         $dumpcmd = "afio -b $softblocksizebytes -Zo -";
220         $dumpin = '<&FINDOR';
221     } else {
222         die "unknown method $tm for $prefix:$atf\n";
223     }
224     # This is a funky way of doing a pipeline which pays attention
225     # to the exit status of all the commands in the pipeline.
226     # It is roughly equivalent to:
227     #    md5sum <p >>this-md5sums
228     #    dump <$dumpin | tee p | writebuffer | dd >/dev/null
229     startprocess '<p','>>this-md5sums','md5sum';
230     startprocess $dumpin,'>&DUMPOW',$rstr.$dumpcmd;
231     startprocess '<&DUMPOR','>&TEEOW','tee p';
232     if ($gz) {
233         startprocess '<&TEEOR','>&GZOW','gzip -v$gz';
234     }
235     startprocess "<&$bufir",'>&BUFOW','writebuffer';
236     startprocess '<&BUFOR','>/dev/null'
237         ,"dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape";
238     closepipes();
239     endprocesses();
240 }
241
242 # The backup should now be complete; verify it
243
244 setstatus "FAILED during check";
245
246 # Rewind the tape and skip the TAPEID record
247 system "mt -f $tape rewind"; $? and die $?;
248 system "mt -f $ntape fsf 1"; $? and die $?;
249
250 # Check the md5sums match for each filesystem on the tape
251 open S,"this-md5sums" or die $!;
252 for $tf (@fsys) {
253     printdate();
254     chomp($orgsum= <S>); $orgsum =~ s/\ +\-?$//;
255     $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
256     chomp($csum= `dd if=$ntape ibs=$blocksizebytes | readbuffer | md5sum`);
257     $csum =~ s/\ +\-?$//;
258     $orgsum eq $csum or die "MISMATCH $tf $csum $orgsum\n";
259     print "checksum ok $csum\t$tf\n" or die $!;
260     print LOG "checksum ok $csum\t$tf\n" or die $!;
261 }
262 printdate();
263 system "mt -f $tape rewind"; $? and die $?;
264
265 setstatus "FAILED during cleanup";
266
267 # Write to some status files to indicate what the backup system
268 # ought to do when next invoked.
269 # reset incremental backup count to 1.
270 open IAN,">increm-advance.new" or die $!;
271 print IAN "1\n" or die $!;
272 close IAN or die $!;
273
274 # Next full backup is whatever the next link in the tape description
275 # file says it ought to be.
276 open TN,">next-full.new" or die $!;
277 print TN "$next\n" or die $!;
278 close TN or die $!;
279
280 unlink 'last-tape','next-full';
281 # We are the last tape to have been backed up
282 rename 'TAPEID','last-tape' or die $!;
283 rename 'this-md5sums',"md5sums.$fsys" or die $!;
284 rename 'log',"log.$fsys" or die $!;
285 rename 'next-full.new',"next-full" or die $!;
286 rename 'increm-advance.new',"increm-advance" or die $!;
287
288 print "$doing completed.\nNext dump tape is $next.\n" or die $!;
289
290 setstatus "Successful: $tapedesc $fsys, next $next";
291 exit 0;