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