1 #!/usr/local/bin/perl -w
4 use lib "__INSTALLDIR__/lib";
9 use BackupPC::Attrib qw/:all/;
11 use Time::HiRes qw/time/;
12 use POSIX qw/strftime/;
15 use Archive::Tar::Streamed;
19 my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
20 my %Conf = $bpc->Conf();
22 use BackupPC::SearchLib;
23 %BackupPC::SearchLib::Conf = %Conf;
25 # cludge: minimum .tar.gz size
26 my $MIN_TAR_SIZE = 80;
28 my $path = abs_path($0);
29 $path =~ s#/[^/]+$#/#;
30 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
32 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
35 foreach my $c (qw/gzip/) {
36 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
40 getopts("cd", \%opt );
43 my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
49 my $t_fmt = '%Y-%m-%d %H:%M:%S';
51 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
52 my $user = $Conf{SearchUser} || '';
54 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
56 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
58 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
63 my $t = shift || return;
65 my ($ss,$mm,$hh) = gmtime($t);
66 $out .= "${hh}h" if ($hh);
67 $out .= sprintf("%02d:%02d", $mm,$ss);
72 return strftime($t_fmt,localtime());
76 my ($host,$share,$num,$filename) = @_;
81 print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
87 print STDERR ", joining";
91 print STDERR ", opening" if ($debug);
92 open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
94 my $tar = Archive::Tar::Streamed->new($fh);
96 print STDERR ", tar" if ($debug);
98 while(my $entry = $tar->next) {
99 push @tar_files, $entry->name;
101 @tar_files = sort @tar_files;
102 print STDERR " ",($#tar_files + 1), " files" if ($debug);
104 print STDERR ", database" if ($debug);
106 my $sth = $dbh->prepare(qq{
109 JOIN shares on shares.id = shareid
110 JOIN hosts on hosts.id = shares.hostid
111 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
113 $sth->execute($host, $share, $num);
115 while( my $row = $sth->fetchrow_hashref ) {
117 my $path = $row->{'path'} || die "no path?";
119 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
120 push @db_files, $path;
123 print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
125 @db_files = sort @db_files;
128 if ($#tar_files != $#db_files) {
130 print STDERR " NUMBER" if ($debug);
132 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
133 while ( $diff->Next() ) {
134 next if $diff->Same();
136 print "< $_\n" for $diff->Items(1);
137 print "> $_\n" for $diff->Items(2);
141 print " ",($same ? 'ok' : 'DIFFERENT');
142 print STDERR " }} " if ($debug);
150 my $sth = $dbh->prepare( qq{
153 backups.id as backup_id,
155 shares.name as share,
160 join shares on backups.hostid = shares.hostid
161 and shares.id = backups.shareid
162 join hosts on shares.hostid = hosts.id
163 where not inc_deleted
164 order by backups.date
169 my $num_backups = $sth->rows;
172 while (my $row = $sth->fetchrow_hashref) {
173 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
175 # this will return -1 if file doesn't exist
176 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
178 print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
180 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) ) {
184 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
190 my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} };
191 print STDERR "## $cmd\n" if ($debug);
193 if (system($cmd) != 0) {
194 print STDERR " FAILED";
197 print ", dur: ",fmt_time(time() - $t), "\n";