1 #!/usr/local/bin/perl -w
4 use lib "/usr/local/BackupPC/lib";
9 use BackupPC::Attrib qw/:all/;
10 use Data::Dump qw(dump);
11 use Time::HiRes qw/time/;
12 use POSIX qw/strftime/;
14 use Archive::Tar::Streamed;
21 BackupPC_ASA_PostArchive_Update
25 # /etc/BackupPC/pc/dvd_tar.pl
30 my $debug = $ENV{DEBUG} || 1;
31 my $check = $ENV{CHECK} || 1;
34 my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
35 my %Conf = $bpc->Conf();
36 warn "## ARGV=",dump @ARGV;
43 if ( m/(\w+)=(.+)/ ) {
47 if ( $name =~ m/List/ ) {
48 push @{ $args->{$name} }, $v;
54 warn "args = ",dump($args);
58 %BackupPC::Search::Conf = %Conf;
60 my $path = abs_path($0);
61 $path =~ s{/[^/]+$}{/}; # FIXME remove?
67 my $t_fmt = '%Y-%m-%d %H:%M:%S';
69 warn "## Conf = ",dump( \%Conf );
71 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
72 my $user = $Conf{SearchUser} || '';
74 my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
80 return strftime($t_fmt,localtime());
84 my $t = shift || return;
86 my ($ss,$mm,$hh) = gmtime($t);
87 $out .= "${hh}h" if ($hh);
88 $out .= sprintf("%02d:%02d", $mm,$ss);
94 sub get_backup_id($$) {
95 my ($host, $num) = @_;
97 my $key = "$host $num";
98 return $hsn_cache->{$key} if ($hsn_cache->{$key});
100 my $sth = $dbh->prepare(qq{
104 INNER JOIN shares ON backups.shareID=shares.ID
105 INNER JOIN hosts ON backups.hostID = hosts.ID
106 WHERE hosts.name = ? and backups.num = ?
108 $sth->execute($host, $num);
109 my ($id) = $sth->fetchrow_array;
111 $hsn_cache->{"$host $num"} = $id;
113 print STDERR "# $host $num == $id\n" if $debug;
118 sub backup_inc_deleted($) {
119 my $backup_id = shift;
120 my $sth_inc_deleted = $dbh->prepare(qq{
125 $sth_inc_deleted->execute($backup_id);
129 warn "## system_ok @_\n";
130 system(@_) == 0 || die "system @_:$!";
133 my $sth_inc_size = $dbh->prepare(qq{
142 my ($host,$num) = @_;
147 sort map { s/^\Q$Conf{ArchiveDest}\E\/*//; $_ }
148 glob "$Conf{ArchiveDest}/$host.$num.*"
151 return unless @tar_parts;
153 print curr_time, " check $host $num";
155 my $md5_path = "$Conf{ArchiveDest}/$host.$num.md5";
156 unlink $md5_path if -s $md5_path == 0; # fix empty
158 if ( ! -e $md5_path ) {
159 system_ok "cd $Conf{ArchiveDest} && /usr/bin/md5sum $host.$num.* > $md5_path";
161 system_ok "cd $Conf{ArchiveDest} && /usr/bin/md5sum -c $md5_path" if $check;
165 foreach ( split(/\n/, read_file "$Conf{ArchiveDest}/$host.$num.md5" ) ) {
166 my ( $md5, $path ) = split(/\s+/,$_);
167 $md5sum->{$path} = $md5;
170 # depending on expected returned value this is used like:
171 # my $uncompress_size = get_gzip_size('/full/path/to.gz');
172 # my ($compress_size, $uncompress_size) = get_gzip_size('/path.gz');
173 sub get_gzip_size($) {
174 my $filename = shift;
175 die "file $filename problem: $!" unless (-r $filename);
177 if ( $filename !~ m/\.gz$/ ) {
181 open(my $gzip, $Conf{GzipPath}." -l $filename |") || die "can't gzip -l $filename: $!";
184 $line = <$gzip> if ($line =~ /^\s+compressed/);
186 my ($comp, $uncomp) = (0,0);
188 if ($line =~ m/^\s+(\d+)\s+(\d+)\s+\d+\.\d+/) {
195 die "can't find size in line: $line";
200 my ($host, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
201 my $backup_id = get_backup_id($host, $num);
202 my $sth_md5 = $dbh->prepare(qq{
204 id, tar_size, size, md5, items
206 where backup_id = ? and part_nr = ?
209 $sth_md5->execute($backup_id, $part_nr);
211 if (my $row = $sth_md5->fetchrow_hashref) {
213 $row->{tar_size} >= $tar_size &&
214 $row->{size} == $size &&
215 $row->{md5} eq $md5 &&
216 $row->{items} == $items
218 print ", deleting invalid backup_parts $row->{id}";
219 $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
221 print ", inserting new";
222 my $sth_insert = $dbh->prepare(qq{
223 insert into backup_parts (
230 ) values (?,?,?,?,?,?)
233 $sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items);
237 print " [parts: ",join(", ", @tar_parts),"]" if $debug;
243 print " reading" if $debug;
248 foreach my $filename (@tar_parts) {
250 next if $filename eq "$host.$num.md5";
252 print "\n\t- $filename";
254 my $path = "$Conf{ArchiveDest}/$filename";
257 my $size = (stat( $path ))[7] || die "can't stat $path: $!";
259 if ($size > $Conf{MaxArchiveSize}) {
260 print ", part bigger than media $size > $Conf{MaxArchiveSize}\n";
264 print ", $size bytes";
268 open(my $fh, "gzip -cd $path |") or die "can't open $path: $!";
270 my $tar = Archive::Tar::Streamed->new($fh);
272 my $tar_size_inarc = 0;
275 while(my $entry = $tar->next) {
276 push @tar_files, $entry->name;
278 $tar_size_inarc += $entry->size;
280 if ($tar_size_inarc > $Conf{MaxArchiveFileSize}) {
281 print ", part $filename is too big $tar_size_inarc > $Conf{MaxArchiveFileSize}\n";
289 print ", $items items";
291 if ($tar_size_inarc == 0 && $items == 0) {
292 print ", EMPTY tar\n";
294 my $backup_id = get_backup_id($host, $share, $num);
295 backup_inc_deleted( $backup_id );
305 my $tar_size = get_gzip_size( $path );
308 # finally, check if backup_parts table in database is valid
311 my $md5 = $md5sum->{$filename} || die "no md5sum for $filename in ",dump($md5sum);
315 check_part($host, $num, $part_nr, $tar_size, $size, $md5, $items);
317 # round increment size to 2k block size
318 $inc_size += int(($size + 2048) / 2048);
321 $sth_inc_size->execute(
324 get_backup_id($host, $num),
328 @tar_files = sort @tar_files;
329 print "\n\t",($#tar_files + 1), " tar files";
331 my $sth = $dbh->prepare(qq{
334 JOIN shares on shares.id = shareid
335 JOIN hosts on hosts.id = shares.hostid
336 WHERE hosts.name = ? and backupnum = ?
338 $sth->execute($host, $num);
340 while( my $row = $sth->fetchrow_hashref ) {
342 my $path = $row->{'path'} || die "no path?";
344 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
345 push @db_files, $path;
348 print " ",($#db_files + 1), " database files, diff";
350 @db_files = sort @db_files;
354 if ($#tar_files != $#db_files) {
358 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
359 while ( $diff->Next() ) {
360 next if $diff->Same();
362 print "< $_\n" for $diff->Items(1);
363 print "> $_\n" for $diff->Items(2);
367 print " ",($same ? 'ok' : 'DIFFERENT'),
368 ", dur: ",fmt_time(time() - $t), "\n";
376 foreach ( 0 .. $#{ $args->{HostList} } ) {
378 my $host = $args->{'HostList'}->[$_];
379 my $num = $args->{'BackupList'}->[$_];
381 check_archive $host => $num;
387 my $sth = $dbh->prepare( qq{
390 backups.id as backup_id,
392 shares.name as share,
397 count(backup_parts.backup_id) as backup_parts
399 join shares on backups.hostid = shares.hostid
400 and shares.id = backups.shareid
401 join hosts on shares.hostid = hosts.id
402 full outer join backup_parts on backups.id = backup_parts.backup_id
403 where not inc_deleted and backups.size > 0
404 group by backups.id, hosts.name, shares.name, backups.num, backups.date, inc_size, parts, backup_parts.backup_id
405 order by backups.date
410 my $num_backups = $sth->rows;
413 while (my $row = $sth->fetchrow_hashref) {
417 my $tar_file = BackupPC::Search::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
419 # this will return -1 if file doesn't exist
420 my $size = BackupPC::Search::get_tgz_size_by_name($tar_file);
422 print "# host: ".$row->{host}.", share: ".$row->{'share'}.", backup_num:".$row->{num}." size: $size backup.size: ", $row->{inc_size},"\n" if $debug;
424 if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size && $row->{parts} == $row->{backup_parts}) {
426 tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
432 print curr_time, " creating $curr_backup/$num_backups ", $row->{host}, ":", $row->{share}, " #", $row->{num},
433 " ", strftime('%Y-%m-%d', localtime($row->{date})), " -> $tar_file";
439 my $cmd = qq[ $tarIncCreate -h "$row->{host}" -s "$row->{share}" -n $row->{num} -f ];
440 print STDERR "## $cmd\n" if ($debug);
442 if (system($cmd) != 0) {
443 print STDERR " FAILED, marking this backup deleted";
444 backup_inc_deleted( $row->{backup_id} );
448 print ", dur: ",fmt_time(time() - $t), "\n";