chiark / gitweb /
@@ -6,8 +6,11 @@
[chiark-utils.git] / backup / increm
1 #!/usr/bin/perl
2 # increm
3 # Do an incremental backup; ONLY for invocation by full !
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 # We are invoked by full if the tape description file says that it is
26 # for an incremental backup.  We expect two commandline argument which
27 # is the ID string of the tape to use, and the description (which
28 # includes ID and function).
29
30 BEGIN {
31     $etc= '/etc/chiark-backup';
32     require "$etc/settings.pl";
33     require 'backuplib.pl';
34 }
35
36 $|=1;
37
38 @ARGV==2 or die;
39 ($tapeid,$tapedesc)= @ARGV;
40
41 print "Running incremental onto $tapedesc ...\n" or die $!;
42
43 # Just check that we weren't passed a bogus ID (the tape description
44 # file for incrementals is just 'incremental\nend\n')
45 open T,"$etc/tape.$tapeid" or die "Tape $tapeid not found: $!\n";
46 close T;
47
48 # This is only used for the 'next FULL backup is X' message at the end.
49 open NF,"next-full" or die $!;
50 chomp($next= <NF>);
51 close NF or die $!;
52
53 setstatus "FAILED during incremental";
54
55 # Read the number of the incremental involved
56 # (ie, (how many files are already on the tape) - 1)
57 open A,"increm-advance" or die $!;
58 chomp($advance= <A>);
59 close A or die $!;
60
61 # better be a decimal number
62 $advance =~ m/^\d+$/ or die "$advance ?";
63
64 # Rewind the tape and if we are the first incremental on the tape then
65 # write the TAPEID record, otherwise skip forward to the correct point.
66 # (full will already have checked that this is the right tape before
67 # it invoked us, so no need to read the existing TAPEID record first.)
68 system "mt -f $ntape rewind"; $? and die $?;
69 if ($advance == 1) {
70     writetapeid($tapeid,$tapedesc);
71 } else {
72     system "mt -f $ntape fsf $advance"; $? and die $?;
73     $currenttapefilenumber= $advance+1;
74 }
75
76 # Get a list of all filesystems
77 readfsys('all');
78 openlog();
79
80 sub closepipes () {
81     close(DUMPOR); close(BUFOR);
82     close(DUMPOW); close(BUFOW);
83 }
84
85 setstatus "PROBLEMS during incremental dump";
86
87 for $tf (@fsys) {
88
89     parsefsys();
90     
91     $bufir='DUMPOR';
92     $ddcmd= "$nasty dd ibs=$softblocksizebytes obs=$blocksizebytes of=$ntape";
93
94     pipe(DUMPOR,DUMPOW) or die $!;
95     pipe(BUFOR,BUFOW) or die $!;
96
97     $gz= $gzi if length $gzi;
98     if ($gz) {
99         $bufir='GZOR';
100         pipe(GZOR,GZOW) or die $!;
101         $ddcmd .= " conv=sync";
102     }
103
104     if ($tm eq 'dump') {
105         $dumpcmd= "dump 1bfu $softblocksizekb - $atf";
106     } else {
107         print "Not dumping $atf ($prefix) - not \`dump'.\n" or die $!;
108         print LOG "Not dumping $atf ($prefix) - not \`dump'.\n" or die $!;
109         next;
110     }
111
112     nexttapefile("inc $prefix:$atf");
113     
114     # Same trick as full uses to do a pipeline whilst keeping track
115     # of all exit statuses:
116     #   dump </dev/null | writebuffer | dd >/dev/null
117     startprocess '</dev/null','>&DUMPOW',"$nice ".$rstr.$dumpcmd;
118     if ($gz) {
119         startprocess '<&DUMPOR','>&GZOW',"$nice gzip -v$gz";
120     }
121     startprocess "<&$bufir",'>&BUFOW',"$nasty writebuffer";
122     startprocess '<&BUFOR','>/dev/null',"$nasty $ddcmd";
123     closepipes();
124     endprocesses();
125     # advance is a file counter, so it needs to be updated for each 
126     # dump we do to tape.
127     $advance++;
128 }
129
130 # Rewind the tape, and increment the counter of incremental backups.
131 system "mt -f $tape rewind"; $? and die $?;
132 open IAN,">increm-advance.new" or die $!;
133 print IAN "$advance\n" or die $!;
134 close IAN or die $!;
135 rename 'increm-advance.new','increm-advance' or die $!;
136
137 print LOG "Next FULL dump tape is $next\n" or die $!;
138 print "Next FULL dump tape is $next\n" or die $!;
139
140 setstatus "INCREMENTAL successful: $tapedesc, next full is $next";
141 exit 0;