r8609@llin: dpavlin | 2005-10-16 14:32:54 +0200
authordpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Sun, 16 Oct 2005 12:33:05 +0000 (12:33 +0000)
committerdpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Sun, 16 Oct 2005 12:33:05 +0000 (12:33 +0000)
 added command-line options: -d for debugging and -c to check tar archives
 on disk against database, moved join in tar_join()

git-svn-id: svn+ssh://llin/home/dpavlin/private/svn/BackupPC/trunk@214 8392b6e1-25fa-0310-8288-cc32f8e212ea

bin/BackupPC_incPartsUpdate

index 9995cde..de2895a 100755 (executable)
@@ -6,12 +6,16 @@ use lib "__INSTALLDIR__/lib";
 use DBI;
 use BackupPC::Lib;
 use BackupPC::View;
+use BackupPC::Attrib qw/:all/;
 use Data::Dumper;
 use Time::HiRes qw/time/;
 use POSIX qw/strftime/;
 use BackupPC::SearchLib;
 use Cwd qw/abs_path/;
 use File::Which;
+use Archive::Tar;
+use Algorithm::Diff;
+use Getopt::Std;
 
 my $path = abs_path($0);
 $path =~ s#/[^/]+$#/#;
@@ -24,8 +28,12 @@ foreach my $c (qw/gzip split/) {
        $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
 }
 
+my %opt;
+getopts("cd", \%opt );
+
+my $debug = $opt{d};
+my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
 
-my $debug = 0;
 $|=1;
 
 my $start_t = time();
@@ -62,6 +70,85 @@ sub curr_time {
        return strftime($t_fmt,localtime());
 }
 
+sub tar_join($) {
+       my $filename = shift;
+
+       my $in = my $out = $filename;
+       $out .= '.tmp';
+
+       # FIXME I should really order parts manually!
+       system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
+
+}
+
+sub tar_check($$$$) {
+       my ($host,$share,$num,$filename) = @_;
+
+       if ($debug) {
+               print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
+       } else {
+               print " check";
+       }
+
+       if (-d $filename) {
+               print STDERR ", joining";
+               tar_join($filename);
+       }
+
+       my $tar = Archive::Tar->new;
+       my $comp = 0;
+       $comp = 1 if ($filename =~ m/\.(gz|tgz)$/);
+       print STDERR ", opening" if ($debug);
+       $tar->read($filename, $comp) or die "can't open $filename: $!";
+
+       print STDERR ", tar" if ($debug);
+       my @tar_files = sort $tar->list_files();
+       print STDERR " ",($#tar_files + 1), " files" if ($debug);
+
+       print STDERR ", database" if ($debug);
+
+       my $sth = $dbh->prepare(qq{
+               SELECT path,type
+               FROM files
+               JOIN shares on shares.id = shareid
+               JOIN hosts on hosts.id = shares.hostid
+               WHERE hosts.name = ? and shares.name = ? and backupnum = ?
+       });
+       $sth->execute($host, $share, $num);
+       my @db_files;
+       while( my $row = $sth->fetchrow_hashref ) {
+
+               my $path = $row->{'path'} || die "no path?";
+               $path =~ s#^/#./#;
+               $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
+               push @db_files, $path;
+       }
+
+       print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
+
+       @db_files = sort @db_files;
+
+       my $same = 1;
+       if ($#tar_files != $#db_files) {
+               $same = 0;
+               print STDERR " NUMBER" if ($debug);
+       } else {
+               my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
+               while ( $diff->Next() ) {
+                       next if $diff->Same();
+                       $same = 0;
+                       print "< $_\n" for $diff->Items(1);
+                       print "> $_\n" for $diff->Items(2);
+               }
+       }
+
+       print STDERR " ",($same ? 'ok' : 'DIFFERENT');
+       print STDERR " }} " if ($debug);
+
+       return $same;
+}
+
+
 #----- main
 
 my $sth = $dbh->prepare( qq{
@@ -103,7 +190,10 @@ while (my $row = $sth->fetchrow_hashref) {
        my $t = time();
 
        # re-create archive?
-       if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) {
+       if ($row->{'inc_size'} == -1 || $size == -1 ||
+               $row->{'inc_size'} != $size ||
+               $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")
+       ) {
                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};
                print STDERR "## $cmd\n" if ($debug);
 
@@ -123,12 +213,7 @@ while (my $row = $sth->fetchrow_hashref) {
 
                if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
                        print " join";
-
-                       my $in = my $out = "$tar_dir/$tar_file";
-                       $out .= '.tmp';
-
-                       # FIXME I should really order parts manually!
-                       system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
+                       tar_join("$tar_dir/$tar_file");
                }
 
                if ($size > $max_size && ! -d "$tar_dir/$tar_file") {