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