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