#!/usr/local/bin/perl -w use strict; 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 Cwd qw/abs_path/; use File::Which; use Archive::Tar::Streamed; use Algorithm::Diff; use Getopt::Std; my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib"; my %Conf = $bpc->Conf(); use BackupPC::SearchLib; %BackupPC::SearchLib::Conf = %Conf; # cludge: minimum .tar.gz size my $MIN_TAR_SIZE = 80; my $path = abs_path($0); $path =~ s#/[^/]+$#/#; my $tarIncCreate = $path .= 'BackupPC_tarIncCreate'; die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); my $bin; foreach my $c (qw/gzip/) { $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"; $|=1; my $start_t = time(); my $t_fmt = '%Y-%m-%d %H:%M:%S'; my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; my $user = $Conf{SearchUser} || ''; my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}; die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir); #---- subs ---- sub fmt_time { my $t = shift || return; my $out = ""; my ($ss,$mm,$hh) = gmtime($t); $out .= "${hh}h" if ($hh); $out .= sprintf("%02d:%02d", $mm,$ss); return $out; } sub curr_time { return strftime($t_fmt,localtime()); } sub tar_check($$$$) { my ($host,$share,$num,$filename) = @_; return 1; # FIXME if ($debug) { print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; } else { print " check"; } if (-d $filename) { print STDERR ", joining"; tar_join($filename); } print STDERR ", opening" if ($debug); open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!"; binmode($fh); my $tar = Archive::Tar::Streamed->new($fh); print STDERR ", tar" if ($debug); my @tar_files; while(my $entry = $tar->next) { push @tar_files, $entry->name; } @tar_files = sort @tar_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 " ",($same ? 'ok' : 'DIFFERENT'); print STDERR " }} " if ($debug); return $same; } #----- main my $sth = $dbh->prepare( qq{ select backups.id as backup_id, hosts.name as host, shares.name as share, backups.num as num, inc_size, parts from backups join shares on backups.hostid = shares.hostid and shares.id = backups.shareid join hosts on shares.hostid = hosts.id where not inc_deleted order by backups.date } ); $sth->execute(); my $num_backups = $sth->rows; my $curr_backup = 1; while (my $row = $sth->fetchrow_hashref) { my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); # this will return -1 if file doesn't exist my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d}); if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size && ( $check && tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file") || 1) ) { next; } print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; $curr_backup++; my $t = time(); # re-create archive? my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} }; print STDERR "## $cmd\n" if ($debug); if (system($cmd) != 0) { print STDERR " FAILED"; } print ", dur: ",fmt_time(time() - $t), "\n"; $dbh->commit; } undef $sth; $dbh->disconnect;