chiark / gitweb /
seems to work with test case: PATH=~ian/things/chiark-utils/cprogs:$PATH ./snaprsync...
[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 openlog();
49
50 if (!$nocheck) {
51     setstatus "FAILED configuration check";
52     print "Configuration check ...\n" or die $!;
53     system 'backup-checkallused'; $? and die $?;
54 } else {
55     setstatus "FAILED rewinding";
56     rewind_raw();
57 }
58
59 printdate();
60
61 setstatus "FAILED reading TAPEID";
62 # Try to read the tape ID from the tape into the file TAPEID
63
64 readtapeid_raw();
65
66 setstatus "FAILED during startup";
67
68 # We need some ID; if the tape has one already that takes precedence;
69 # otherwise the user might have set a tape ID that this should be
70 # by creating really-TAPEID.
71 if (open T, "TAPEID") {
72     unlink 'really-TAPEID';
73 } elsif (open T, "really-TAPEID") {
74 } else {
75     die "No TAPEID.\n";
76 }
77
78 # read the ID; it had better be a non-empty string of alphanumeric chars.
79 chomp($tapeid= <T>);
80 $tapeid =~ m/[^0-9a-zA-Z]/ and die "Bad TAPEID ($&).\n";
81 $tapeid =~ m/[0-9a-zA-Z]/ or die "Empty TAPEID.\n";
82 close T;
83
84 setstatus "FAILED at tape identity check";
85
86 # We don't let the user overwrite the tape used for the last backup.
87 if (open L, "last-tape") {
88     chomp($lasttape= <L>);
89     close L;
90 } else {
91     undef $lasttape;
92 }
93
94 die "Tape $tapeid same as last time.\n" if $tapeid eq $lasttape;
95
96 # $tapeid identifies the individual tape; $tapedesc is its current
97 # identity and function, for printing in messages.  You can make these
98 # namespaces the same if you like, or you can make the tape.<tapeid>
99 # files be links to tape.<tapedesc> files.
100 if (defined($tapedesc= readlink "$etc/tape.$tapeid")) {
101     $tapedesc =~ s/^.*\.//;
102     $tapedesc .= "($tapeid)";
103 } else {
104     $tapedesc = $tapeid;
105 }
106
107 # Parse the appropriate tape.nn file.
108 # Format is: empty lines and lines starting '#' are ignored. Trailing
109 # whitespace is ignored. File must end with 'end' on a line by itself.
110 # Either there should be a line 'incremental' to indicate that this is
111 # a tape for incremental backups, or a pair of lines 'filesystems fsg'
112 # and 'next tapeid', indicating that this tape is part of a full 
113 # backup, containing the filesystem group fsg. 
114 undef $fsys;
115 open D, "$etc/tape.$tapeid" or die "Unknown tape $tapeid ($!).\n";
116 for (;;) {
117     $_= <D> or die; chomp; s/\s+$//;
118     last if m/^end$/;
119     next unless m/\S/;
120     next if m/^\#/;
121     if (m/^filesystems (\w+)$/) {
122         $fsys= $1;
123     } elsif (m/^next (\w+)$/) {
124         $next= $1;
125     } elsif (m/^incremental$/) {
126         $incremental= 1;
127     } else {
128         die "unknown entry in tape $tapeid at line $.: $_\n";
129     }
130 }
131 close D or die $!;
132
133 # Incremental backups are handled by increm, not us.
134 if ($incremental) {
135     die "incremental tape $tapeid has next or filesystems\n"
136         if defined($next) || defined($fsys);
137     print STDERR "Incremental tape $tapeid.\n\n";
138     setstatus "FAILED during incremental startup";
139     exec "increm",$tapeid,$tapedesc;
140     die $!;
141 }
142
143 # Read the filesystem group definition (file fsys.nnn)
144 readfsys("$fsys");
145
146 $doing= "dump of $fsys to tape $tapedesc in drive $tape";
147 print LOG "$doing:\n" or die $!;
148
149 if (!$noreten) {
150     setstatus "FAILED retensioning";
151     runsystem("mt -f $tape reten");
152 }
153
154 setstatus "FAILED writing tape ID";
155 # First write the tape ID to this tape.
156
157 writetapeid($tapeid,$tapedesc);
158
159 unlink 'this-md5sums';
160
161 print "Doing $doing ...\n" or die $!;
162
163 unlink 'p';
164 system 'mknod -m600 p p'; $? and die $?;
165
166 setstatus "FAILED during dump";
167
168 sub closepipes () {
169     close(DUMPOR); close(TEEOR); close(BUFOR); close(FINDOR);
170     close(DUMPOW); close(TEEOW); close(BUFOW); close(FINDOW);
171     close(GZOR); close(GZOW);
172     close(DDERRR); close(DDERRW);
173 }
174
175 # work out a find option string that will exclude the required files    
176 # Note that dump pays no attention to exclude options.
177 $exclopt = '';
178 foreach $exc (@excldir) {
179     $exclopt .= "-regex $exc -prune -o ";
180 }
181 foreach $exc (@excl) {
182     $exclopt .= "-regex $exc -o ";
183 }
184
185 # For each filesystem to be put on this tape:
186 for $tf (@fsys) {
187     printdate();
188     parsefsys();
189     prepfsys();
190
191     pipe(FINDOR,FINDOW) or die $!;
192     pipe(DUMPOR,DUMPOW) or die $!;
193     pipe(TEEOR,TEEOW) or die $!;
194     pipe(TEEOR,TEEOW) or die $!;
195     pipe(BUFOR,BUFOW) or die $!;
196     pipe(DDERRR,DDERRW) or die $!;
197     
198     $bufir='TEEOR';
199     $ddcmd= "dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape 2>&1";
200
201     if ($gz) {
202         $bufir='GZOR';
203         pipe(GZOR,GZOW) or die $!;
204         $ddcmd .= " conv=sync";
205     }
206     
207     nexttapefile("full $prefix:$atf_print");
208
209     # We can back up via dump or cpio or zafio
210     $dumpin= '</dev/null';
211     if ($tm eq 'dump') {
212         $dumplabel= $pcstr.$atf_print.'$';
213         $dumpcmd= "dump 0Lbfu $dumplabel $softblocksizekb - $atf";
214     } elsif ($tm eq 'cpio') {
215         startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf -print0";
216         $dumpcmd= "cpio -Hustar -o0C$softblocksizebytes";
217         $dumpin= '<&FINDOR';
218     } elsif ($tm eq 'zafio') {
219         # compress-each-file-then-archive using afio
220         startprocess '</dev/null','>&FINDOW',$rstr."find $atf -xdev -noleaf $exclopt -print";
221         # don't use verbose flag as this generates 2MB report emails :->
222         $dumpcmd = "afio -b $softblocksizebytes -Zo -";
223         $dumpin = '<&FINDOR';
224     } elsif ($tm eq 'ntfsimage') {
225         $dumpcmd= "ntfsimage -svvf --dirty $dev";
226     } elsif ($tm eq 'gtar') {
227         execute("$rstr touch $fsidfile+new");
228         $dumpcmd= "tar Ccfl $atf - .";
229     } else {
230         die "unknown method $tm for $prefix:$atf_print\n";
231     }
232     # This is a funky way of doing a pipeline which pays attention
233     # to the exit status of all the commands in the pipeline.
234     # It is roughly equivalent to:
235     #    md5sum <p >>this-md5sums
236     #    dump <$dumpin | tee p [| gzip] | writebuffer | dd >/dev/null
237
238     startprocess '<p','>>this-md5sums',"$nice md5sum";
239     startprocess $dumpin,'>&DUMPOW',"$nice ".$rstr.$dumpcmd;
240     startprocess '<&DUMPOR','>&TEEOW',"$nice tee p";
241     if ($gz) {
242         startprocess '<&TEEOR','>&GZOW',"$nice gzip -v$gz";
243     }
244     startprocess "<&$bufir",'>&BUFOW',"$nasty writebuffer";
245     startprocess '<&DDERRR','>/dev/null',"$nice tee dderr >&2";
246     startprocess '<&BUFOR','>&DDERRW',"$nasty $ddcmd";
247     closepipes();
248     endprocesses();
249
250     open DDERR, "dderr" or die $!;
251     defined(read DDERR,$_,1023) or die $!;
252     close DDERR;
253     m/\n(\d+)\+0 records out\n/ or die ">$dderr< ?";
254     push @tapefilesizes, [ $1, $currenttapefilename ];
255     $totalrecords += $1;
256     pboth("total blocks written so far: $totalrecords\n");
257
258     if ($tm eq 'gtar') {
259         execute("$rstr mv -f $fsidfile+new $fsidfile");
260     }   
261     
262     finfsys();
263 }
264
265 # The backup should now be complete; verify it
266
267 setstatus "FAILED during check";
268
269 # Rewind the tape and skip the TAPEID record
270 runsystem("mt -f $tape rewind");
271 runsystem("mt -f $ntape fsf 1");
272
273 # Check the md5sums match for each filesystem on the tape
274 open S,"this-md5sums" or die $!;
275 for $tf (@fsys) {
276     printdate();
277     parsefsys();
278     chomp($orgsum= <S>); $orgsum =~ s/\ +\-?$//;
279     $orgsum =~ m/^[0-9a-fA-F]{32}$/i or die "orgsum \`$orgsum' ?";
280     $cmd= "$nasty dd if=$ntape ibs=$blocksizebytes";
281     $cmd .= " | $nasty readbuffer";
282     $cmd .= " | $nice gzip -vd" if $gz;
283     $cmd .= " | $nice md5sum";
284     pboth("  $cmd\n");
285     chomp($csum= `$cmd`);
286     $csum =~ s/\ +\-?$//;
287     $orgsum eq $csum or die "MISMATCH $tf $csum $orgsum\n";
288     print "checksum ok $csum\t$tf\n" or die $!;
289     print LOG "checksum ok $csum\t$tf\n" or die $!;
290 }
291 printdate();
292 runsystem("mt -f $tape rewind");
293
294 setstatus "FAILED during cleanup";
295
296 $summary= '';
297 foreach $tfs (@tapefilesizes) {
298     $summary .= sprintf "    %10d blocks for %s\n", $tfs->[0], $tfs->[1]
299 }
300 $summary .=
301     sprintf "    %10d blocks total (of %d bytes) plus TAPEID and headers\n",
302     $totalrecords, $blocksizebytes;
303
304 pboth("size-summary:\n");
305 pboth($summary);
306
307 open SS, ">size-summary..new" or die $!;
308 print SS $summary or die $!;
309 close SS or die $!;
310 rename 'size-summary..new',"size-summary.$fsys" or die $!;
311
312 # Write to some status files to indicate what the backup system
313 # ought to do when next invoked.
314 # reset incremental backup count to 1.
315 open IAN,">increm-advance.new" or die $!;
316 print IAN "1\n" or die $!;
317 close IAN or die $!;
318
319 # Next full backup is whatever the next link in the tape description
320 # file says it ought to be.
321 open TN,">next-full.new" or die $!;
322 print TN "$next\n" or die $!;
323 close TN or die $!;
324
325 unlink 'last-tape','next-full';
326 # We are the last tape to have been backed up
327 rename 'TAPEID','last-tape' or die $!;
328 rename 'this-md5sums',"md5sums.$fsys" or die $!;
329 rename 'log',"log.$fsys" or die $!;
330 rename 'next-full.new',"next-full" or die $!;
331 rename 'increm-advance.new',"increm-advance" or die $!;
332
333 print "$doing completed.\nNext dump tape is $next.\n" or die $!;
334
335 setstatus "Successful: $tapedesc $fsys, next $next";
336 exit 0;