chiark / gitweb /
Merge Peter Maydell's changes.
[chiark-utils.git] / backup / increm
1 #!/usr/bin/perl
2
3 # Do an incremental backup. We are invoked by full if the tape 
4 # description file says that it is for an incremental backup.
5 # We expect two commandline argument which is the ID string
6 # of the tape to use, and the description (which includes ID
7 # and function).
8
9 BEGIN {
10     $etc= '/etc/backup';
11     require "$etc/settings.pl";
12     require 'backuplib.pl';
13 }
14
15 $|=1;
16
17 @ARGV==2 or die;
18 ($tapeid,$tapedesc)= @ARGV;
19
20 print "Running incremental onto $tapedesc ...\n" or die $!;
21
22 # Just check that we weren't passed a bogus ID (the tape description
23 # file for incrementals is just 'incremental\nend\n')
24 open T,"$etc/tape.$tapeid" or die "Tape $tapeid not found: $!\n";
25 close T;
26
27 # This is only used for the 'next FULL backup is X' message at the end.
28 open NF,"next-full" or die $!;
29 chomp($next= <NF>);
30 close NF or die $!;
31
32 setstatus "FAILED during incremental";
33
34 # Read the number of the incremental involved
35 # (ie, (how many files are already on the tape) - 1)
36 open A,"increm-advance" or die $!;
37 chomp($advance= <A>);
38 close A or die $!;
39
40 # better be a decimal number
41 $advance =~ m/^\d+$/ or die "$advance ?";
42
43 # Rewind the tape and if we are the first incremental on the tape then
44 # write the TAPEID record, otherwise skip forward to the correct point.
45 # (full will already have checked that this is the right tape before
46 # it invoked us, so no need to read the existing TAPEID record first.)
47 system "mt -f $ntape rewind"; $? and die $?;
48 if ($advance == 1) {
49     open TI,">TAPEID" or die $!;
50     print TI "$tapeid\n$tapedesc\n" or die $!;
51     close TI or die $!;
52
53     system "tar -b$blocksize -vvcf TAPEID.tar TAPEID"; $? and die $?;
54     system "dd if=TAPEID.tar of=$ntape bs=${blocksize}b count=10"; $? and die $?;
55 } else {
56     system "mt -f $ntape fsf $advance"; $? and die $?;
57 }
58
59 # Get a list of all filesystems
60 readfsys('all');
61 openlog();
62
63 sub closepipes () {
64     close(DUMPOR); close(BUFOR);
65     close(DUMPOW); close(BUFOW);
66 }
67
68 setstatus "PROBLEMS during incremental dump";
69
70 for $tf (@fsys) {
71
72     pipe(DUMPOR,DUMPOW) or die $!;
73     pipe(BUFOR,BUFOW) or die $!;
74     parsefsys();
75     if ($tm ne 'dump') {
76         print "Not dumping $atf ($prefix) - not \`dump'.\n" or die $!;
77         print LOG "Not dumping $atf ($prefix) - not \`dump'.\n" or die $!;
78         next;
79     }
80     # Same trick as full uses to do a pipeline whilst keeping track
81     # of all exit statuses:
82     #   dump </dev/null | writebuffer | dd >/dev/null
83     startprocess '</dev/null','>&DUMPOW',$rstr."dump 1bfu $softblocksizekb - $atf";
84     startprocess '<&DUMPOR','>&BUFOW','writebuffer';
85     startprocess '<&BUFOR','>/dev/null'
86         ,"dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape";
87     closepipes();
88     endprocesses();
89     # advance is a file counter, so it needs to be updated for each 
90     # dump we do to tape.
91     $advance++;
92 }
93
94 # Rewind the tape, and increment the counter of incremental backups.
95 system "mt -f $tape rewind"; $? and die $?;
96 open IAN,">increm-advance.new" or die $!;
97 print IAN "$advance\n" or die $!;
98 close IAN or die $!;
99 rename 'increm-advance.new','increm-advance' or die $!;
100
101 print LOG "Next FULL dump tape is $next\n" or die $!;
102 print "Next FULL dump tape is $next\n" or die $!;
103
104 setstatus "INCREMENTAL successful: $tapedesc, next full is $next";
105 exit 0;