r8682@llin: dpavlin | 2005-10-24 18:41:06 +0200
[BackupPC.git] / bin / BackupPC_incPartsUpdate
1 #!/usr/local/bin/perl -w
2
3 use strict;
4 use lib "__INSTALLDIR__/lib";
5
6 use DBI;
7 use BackupPC::Lib;
8 use BackupPC::View;
9 use BackupPC::Attrib qw/:all/;
10 use Data::Dumper;
11 use Time::HiRes qw/time/;
12 use POSIX qw/strftime/;
13 use BackupPC::SearchLib;
14 use Cwd qw/abs_path/;
15 use File::Which;
16 use Archive::Tar::Streamed;
17 use Algorithm::Diff;
18 use Getopt::Std;
19
20 # cludge: minimum .tar.gz size
21 my $MIN_TAR_SIZE = 80;
22
23 my $path = abs_path($0);
24 $path =~ s#/[^/]+$#/#;
25 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
26
27 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
28
29 my $bin;
30 foreach my $c (qw/gzip split/) {
31         $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
32 }
33
34 my %opt;
35 getopts("cd", \%opt );
36
37 my $debug = $opt{d};
38 my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
39
40 $|=1;
41
42 my $start_t = time();
43
44 my $t_fmt = '%Y-%m-%d %H:%M:%S';
45
46 my $hosts;
47 my $bpc = BackupPC::Lib->new || die;
48 my %Conf = $bpc->Conf();
49 my $TopDir = $bpc->TopDir();
50 my $beenThere = {};
51
52 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
53 my $user = $Conf{SearchUser} || '';
54
55 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
56
57 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
58
59 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
60
61 #---- subs ----
62
63 sub fmt_time {
64         my $t = shift || return;
65         my $out = "";
66         my ($ss,$mm,$hh) = gmtime($t);
67         $out .= "${hh}h" if ($hh);
68         $out .= sprintf("%02d:%02d", $mm,$ss);
69         return $out;
70 }
71
72 sub curr_time {
73         return strftime($t_fmt,localtime());
74 }
75
76 sub tar_join($) {
77         my $filename = shift;
78
79         my $in = my $out = $filename;
80         $out .= '.tmp';
81
82         # FIXME I should really order parts manually!
83         system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
84
85 }
86
87 sub tar_check($$$$) {
88         my ($host,$share,$num,$filename) = @_;
89
90         if ($debug) {
91                 print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
92         } else {
93                 print " check";
94         }
95
96         if (-d $filename) {
97                 print STDERR ", joining";
98                 tar_join($filename);
99         }
100
101         print STDERR ", opening" if ($debug);
102         open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
103         binmode($fh);
104         my $tar = Archive::Tar::Streamed->new($fh);
105
106         print STDERR ", tar" if ($debug);
107         my @tar_files;
108         while(my $entry = $tar->next) {
109                 push @tar_files, $entry->name;
110         }
111         @tar_files = sort @tar_files;
112         print STDERR " ",($#tar_files + 1), " files" if ($debug);
113
114         print STDERR ", database" if ($debug);
115
116         my $sth = $dbh->prepare(qq{
117                 SELECT path,type
118                 FROM files
119                 JOIN shares on shares.id = shareid
120                 JOIN hosts on hosts.id = shares.hostid
121                 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
122         });
123         $sth->execute($host, $share, $num);
124         my @db_files;
125         while( my $row = $sth->fetchrow_hashref ) {
126
127                 my $path = $row->{'path'} || die "no path?";
128                 $path =~ s#^/#./#;
129                 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
130                 push @db_files, $path;
131         }
132
133         print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
134
135         @db_files = sort @db_files;
136
137         my $same = 1;
138         if ($#tar_files != $#db_files) {
139                 $same = 0;
140                 print STDERR " NUMBER" if ($debug);
141         } else {
142                 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
143                 while ( $diff->Next() ) {
144                         next if $diff->Same();
145                         $same = 0;
146                         print "< $_\n" for $diff->Items(1);
147                         print "> $_\n" for $diff->Items(2);
148                 }
149         }
150
151         print " ",($same ? 'ok' : 'DIFFERENT');
152         print STDERR " }} " if ($debug);
153
154         return $same;
155 }
156
157
158 #----- main
159
160 my $sth = $dbh->prepare( qq{
161         
162 select
163         backups.id as backup_id,
164         hosts.name as host,
165         shares.name as share,
166         backups.num as num,
167         inc_size,
168         parts
169 from backups
170         join shares on backups.hostid = shares.hostid
171                 and shares.id = backups.shareid
172         join hosts on shares.hostid = hosts.id
173 where not inc_deleted
174 order by backups.date
175
176 } );
177
178 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });
179 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
180
181 %BackupPC::SearchLib::Conf = %Conf;
182
183 $sth->execute();
184 my $num_backups = $sth->rows;
185 my $curr_backup = 1;
186
187 while (my $row = $sth->fetchrow_hashref) {
188         my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
189
190         # this will return -1 if file doesn't exist
191         my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
192
193         print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
194         $curr_backup++;
195
196         my $t = time();
197
198         # re-create archive?
199         if ($row->{'inc_size'} == -1 || $size == -1 ||
200                 $row->{'inc_size'} != $size ||
201                 $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")
202         ) {
203                 my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | $bin->{'gzip'} $Conf{GzipLevel} > ${tar_dir}/${tar_file}.tmp};
204                 print STDERR "## $cmd\n" if ($debug);
205
206                 system($cmd) == 0 or die "failed: $?";
207
208                 rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
209
210                 $size = (stat( "$tar_dir/$tar_file" ))[7];
211         }
212
213         if ($size > $MIN_TAR_SIZE) {
214
215                 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
216                 $max_size *= 1024;      # convert to bytes
217
218                 # maximum file size on ISO image is 4Gb
219                 # this will require Linux kernel 2.6.8 or newer
220                 my $max_iso_file_size = 2^32 - 2048;
221                 if ( $max_size > $max_iso_file_size ) {
222                         $max_size = $max_iso_file_size;
223                 }
224
225                 my $parts = int( ($size + $max_size - 1) / $max_size );
226
227                 if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
228                         print " join";
229                         tar_join("$tar_dir/$tar_file");
230                 }
231
232                 if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
233                         print " split/$parts";
234                         my $in = my $out = "$tar_dir/$tar_file";
235                         $out .= '.tmp';
236                         rename $in, $out || die "can't rename $in: $!";
237                         mkdir $in || die "can't mkdir $in: $!";
238
239                         my $suffix_len = length("$parts");
240                         system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
241                         unlink $out || die "can't unlink $out: $!";
242                 }
243
244                 $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
245                 $sth_inc_deleted->execute(0, $row->{'backup_id'});
246
247                 printf(" %1.2f MB", ($size / 1024 / 1024));
248
249         } else {
250                 $sth_inc_deleted->execute(1, $row->{'backup_id'});
251                 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
252                 print " EMPTY";
253         }
254         print ", dur: ",fmt_time(time() - $t), "\n";
255
256         $dbh->commit;
257
258 }
259
260 undef $sth;
261 $dbh->disconnect;