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