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