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 my $path = abs_path($0);
26 $path =~ s#/[^/]+$#/#;
27 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
29 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
32 foreach my $c (qw/gzip md5sum/) {
33 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
37 getopts("cd", \%opt );
40 my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
46 my $t_fmt = '%Y-%m-%d %H:%M:%S';
48 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
49 my $user = $Conf{SearchUser} || '';
51 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
53 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
55 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
60 my $t = shift || return;
62 my ($ss,$mm,$hh) = gmtime($t);
63 $out .= "${hh}h" if ($hh);
64 $out .= sprintf("%02d:%02d", $mm,$ss);
69 return strftime($t_fmt,localtime());
73 my ($host,$share,$num,$filename) = @_;
76 print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
83 if (-d "$tar_dir/$filename") {
84 print STDERR " multi-part" if ($opt{d});
85 opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
86 @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
89 push @tar_parts, "${filename}.tar.gz";
92 print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
99 foreach my $tarfilename (@tar_parts) {
101 print STDERR " $tarfilename" if ($debug);
103 my $path = "$tar_dir/$tarfilename";
105 $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";
107 print ", creating md5";
108 system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";
111 open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
113 my $tar = Archive::Tar::Streamed->new($fh);
117 while(my $entry = $tar->next) {
118 push @tar_files, $entry->name;
119 $total_size += $entry->size;
122 if ($total_size > $Conf{MaxArchiveFileSize}) {
123 print STDERR " part too big $total_size > $Conf{MaxArchiveFileSize} }}" if ($debug);
126 } elsif ($total_size > $Conf{MaxArchiveSize}) {
127 print STDERR " part bigger than media $total_size > $Conf{MaxArchiveSize} }}" if ($debug);
133 # short-cut and exit;
134 return $same unless($same);
136 @tar_files = sort @tar_files;
137 print STDERR " ",($#tar_files + 1), " files" if ($debug);
139 print STDERR ", database" if ($debug);
141 my $sth = $dbh->prepare(qq{
144 JOIN shares on shares.id = shareid
145 JOIN hosts on hosts.id = shares.hostid
146 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
148 $sth->execute($host, $share, $num);
150 while( my $row = $sth->fetchrow_hashref ) {
152 my $path = $row->{'path'} || die "no path?";
154 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
155 push @db_files, $path;
158 print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
160 @db_files = sort @db_files;
162 if ($#tar_files != $#db_files) {
164 print STDERR " NUMBER" if ($debug);
166 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
167 while ( $diff->Next() ) {
168 next if $diff->Same();
170 print "< $_\n" for $diff->Items(1);
171 print "> $_\n" for $diff->Items(2);
175 print " ",($same ? 'ok' : 'DIFFERENT');
176 print STDERR " }} " if ($debug);
184 my $sth = $dbh->prepare( qq{
187 backups.id as backup_id,
189 shares.name as share,
194 join shares on backups.hostid = shares.hostid
195 and shares.id = backups.shareid
196 join hosts on shares.hostid = hosts.id
197 where not inc_deleted
198 order by backups.date
203 my $num_backups = $sth->rows;
206 while (my $row = $sth->fetchrow_hashref) {
207 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
209 # this will return -1 if file doesn't exist
210 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
212 print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
214 if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size) {
216 tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
222 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
228 my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
229 print STDERR "## $cmd\n" if ($debug);
231 if (system($cmd) != 0) {
232 print STDERR " FAILED";
235 print ", dur: ",fmt_time(time() - $t), "\n";