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;
20 my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
21 my %Conf = $bpc->Conf();
23 use BackupPC::SearchLib;
24 %BackupPC::SearchLib::Conf = %Conf;
26 my $path = abs_path($0);
27 $path =~ s#/[^/]+$#/#;
28 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
30 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
33 foreach my $c (qw/gzip md5sum/) {
34 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
38 getopts("cd", \%opt );
41 my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
47 my $t_fmt = '%Y-%m-%d %H:%M:%S';
49 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
50 my $user = $Conf{SearchUser} || '';
52 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
54 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
56 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
61 my $t = shift || return;
63 my ($ss,$mm,$hh) = gmtime($t);
64 $out .= "${hh}h" if ($hh);
65 $out .= sprintf("%02d:%02d", $mm,$ss);
70 return strftime($t_fmt,localtime());
75 sub get_backup_id($$$) {
76 my ($host, $share, $num) = @_;
78 my $key = "$host $share $num";
79 return $hsn_cache->{$key} if ($hsn_cache->{$key});
81 my $sth = $dbh->prepare(qq{
85 INNER JOIN shares ON backups.shareID=shares.ID
86 INNER JOIN hosts ON backups.hostID = hosts.ID
87 where hosts.name = ? and shares.name = ? and backups.num = ?
89 $sth->execute($host, $share, $num);
90 my ($id) = $sth->fetchrow_array;
92 $hsn_cache->{"$host $share $num"} = $id;
94 print STDERR "# $host $share $num == $id\n" if ($opt{d});
100 sub tar_check($$$$) {
101 my ($host,$share,$num,$filename) = @_;
104 print curr_time, " check $host:$share#$num -> $filename";
107 my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
108 my $backup_id = get_backup_id($host, $share, $num);
109 my $sth_md5 = $dbh->prepare(qq{
111 id, tar_size, size, md5, items
113 where backup_id = ? and part_nr = ?
116 $sth_md5->execute($backup_id, $part_nr);
118 if (my $row = $sth_md5->fetchrow_hashref) {
120 $row->{tar_size} >= $tar_size &&
121 $row->{size} == $size &&
122 $row->{md5} eq $md5 &&
123 $row->{items} == $items
125 print ", deleting invalid backup_parts $row->{id}";
126 $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
128 print ", inserting new";
129 my $sth_insert = $dbh->prepare(qq{
130 insert into backup_parts (
137 ) values (?,?,?,?,?,?)
140 $sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items);
146 if (-d "$tar_dir/$filename") {
147 print ", multi-part";
148 opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
149 @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
152 push @tar_parts, "${filename}.tar.gz";
155 print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
162 print " reading" if ($opt{d});
164 foreach my $tarfilename (@tar_parts) {
166 print "\n\t- $tarfilename";
168 my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename";
170 if ($size > $Conf{MaxArchiveSize}) {
171 print STDERR " part bigger than media $size > $Conf{MaxArchiveSize} }}" if ($debug);
176 print ", $size bytes";
178 my $path = "$tar_dir/$tarfilename";
180 open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
182 my $tar = Archive::Tar::Streamed->new($fh);
187 while(my $entry = $tar->next) {
188 push @tar_files, $entry->name;
190 $tar_size += $entry->size;
192 if ($tar_size > $Conf{MaxArchiveFileSize}) {
193 print ", part $tarfilename is too big $tar_size > $Conf{MaxArchiveFileSize}\n";
200 print ", $items items";
203 # check if md5 exists, and if not, create one
206 my $md5_path = $path;
207 $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";
208 if (! -e $md5_path || -z $md5_path) {
209 print ", creating md5";
210 system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";
212 ## FIXME check if existing md5 is valid
215 my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";
218 # extract part number from filename
220 $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);
223 # finally, check if backup_parts table in database is valid
226 check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);
229 # short-cut and exit;
230 return $same unless($same);
232 @tar_files = sort @tar_files;
233 print "\n\t",($#tar_files + 1), " tar files";
235 my $sth = $dbh->prepare(qq{
238 JOIN shares on shares.id = shareid
239 JOIN hosts on hosts.id = shares.hostid
240 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
242 $sth->execute($host, $share, $num);
244 while( my $row = $sth->fetchrow_hashref ) {
246 my $path = $row->{'path'} || die "no path?";
248 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
249 push @db_files, $path;
252 print " ",($#db_files + 1), " database files, diff";
254 @db_files = sort @db_files;
256 if ($#tar_files != $#db_files) {
260 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
261 while ( $diff->Next() ) {
262 next if $diff->Same();
264 print "< $_\n" for $diff->Items(1);
265 print "> $_\n" for $diff->Items(2);
269 print " ",($same ? 'ok' : 'DIFFERENT'),
270 ", dur: ",fmt_time(time() - $t), "\n";
278 my $sth = $dbh->prepare( qq{
281 backups.id as backup_id,
283 shares.name as share,
288 join shares on backups.hostid = shares.hostid
289 and shares.id = backups.shareid
290 join hosts on shares.hostid = hosts.id
291 where not inc_deleted
292 order by backups.date
297 my $num_backups = $sth->rows;
300 while (my $row = $sth->fetchrow_hashref) {
301 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
303 # this will return -1 if file doesn't exist
304 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
306 print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
308 if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size) {
310 tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
316 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
322 my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
323 print STDERR "## $cmd\n" if ($debug);
325 if (system($cmd) != 0) {
326 print STDERR " FAILED";
329 print ", dur: ",fmt_time(time() - $t), "\n";