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;
17 use Getopt::Long::Descriptive;
21 BackupPC_ASA_PostArchive_Update
25 # /etc/BackupPC/pc/_search_archive.pl
30 my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
31 $bpc->ConfigRead('_search_archive'); # read our configuration
32 my %Conf = $bpc->Conf();
35 %BackupPC::Search::Conf = %Conf;
37 my ($opt,$usage) = describe_options(
39 [ 'host|h=s@', "import just host(s)" ],
40 [ 'num|n=s@', "import just backup number(s)" ],
41 [ 'check|c', "check archives on disk and sync", { default => 1 } ],
42 [ 'debug|d', "debug", { default => 1 } ],
43 [ 'help', "show help" ],
46 print($usage->text), exit if $opt->help;
52 my $t_fmt = '%Y-%m-%d %H:%M:%S';
54 #warn "## Conf = ",dump( \%Conf );
56 my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
62 return strftime($t_fmt,localtime());
66 my $t = shift || return;
68 my ($ss,$mm,$hh) = gmtime($t);
69 $out .= "${hh}h" if ($hh);
70 $out .= sprintf("%02d:%02d", $mm,$ss);
76 sub get_backup_id($$) {
77 my ($host, $num) = @_;
79 my $key = "$host $num";
80 return $hsn_cache->{$key} if ($hsn_cache->{$key});
82 my $sth = $dbh->prepare(qq{
86 INNER JOIN shares ON backups.shareID=shares.ID
87 INNER JOIN hosts ON backups.hostID = hosts.ID
88 WHERE hosts.name = ? and backups.num = ?
90 $sth->execute($host, $num);
91 my ($id) = $sth->fetchrow_array;
93 $hsn_cache->{"$host $num"} = $id;
95 print STDERR "# $host $num == $id\n" if $opt->debug;
100 sub backup_inc_deleted($) {
101 my $backup_id = shift;
102 my $sth_inc_deleted = $dbh->prepare(qq{
107 $sth_inc_deleted->execute($backup_id);
111 warn "## system_ok @_\n";
112 system(@_) == 0 || die "system @_:$!";
115 my $sth_inc_size = $dbh->prepare(qq{
124 my ($host,$num) = @_;
125 warn "# check_archive $host $num";
129 my $glob = "$Conf{ArchiveDest}/$host.$num.*";
131 my @tar_parts = sort map { s/^\Q$Conf{ArchiveDest}\E\/*//; $_ } glob $glob ;
133 if ( ! @tar_parts ) {
134 warn "ERROR: no files for $glob";
138 print curr_time, " check $host $num";
140 my $md5_path = "$Conf{ArchiveDest}/$host.$num.md5";
141 unlink $md5_path if -s $md5_path == 0; # fix empty
143 if ( ! -e $md5_path ) {
144 system_ok "cd $Conf{ArchiveDest} && /usr/bin/md5sum $host.$num.* > $md5_path";
146 system_ok "cd $Conf{ArchiveDest} && /usr/bin/md5sum -c $md5_path" if $opt->check;
150 foreach ( split(/\n/, read_file "$Conf{ArchiveDest}/$host.$num.md5" ) ) {
151 my ( $md5, $path ) = split(/\s+/,$_);
152 $md5sum->{$path} = $md5;
155 # depending on expected returned value this is used like:
156 # my $uncompress_size = get_gzip_size('/full/path/to.gz');
157 # my ($compress_size, $uncompress_size) = get_gzip_size('/path.gz');
158 sub get_gzip_size($) {
159 my $filename = shift;
160 die "file $filename problem: $!" unless (-r $filename);
162 if ( $filename !~ m/\.gz$/ ) {
166 open(my $gzip, $Conf{GzipPath}." -l $filename |") || die "can't gzip -l $filename: $!";
171 my ($comp, $uncomp) = (0,0);
173 if ($line =~ m/\s+(\d+)\s+(\d+)\s+\d+\.\d+/s) {
180 warn "ERROR can't parse: $line";
186 my ($host, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
187 my $backup_id = get_backup_id($host, $num);
188 my $sth_md5 = $dbh->prepare(qq{
190 id, tar_size, size, md5, items
192 where backup_id = ? and part_nr = ?
195 $sth_md5->execute($backup_id, $part_nr);
197 if (my $row = $sth_md5->fetchrow_hashref) {
199 $row->{tar_size} >= $tar_size &&
200 $row->{size} == $size &&
201 $row->{md5} eq $md5 &&
202 $row->{items} == $items
204 print ", deleting invalid backup_parts $row->{id}";
205 $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
207 print ", inserting new";
208 my $sth_insert = $dbh->prepare(qq{
209 insert into backup_parts (
216 ) values (?,?,?,?,?,?)
219 $sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items);
223 print " [parts: ",join(", ", @tar_parts),"]" if $opt->debug;
229 print " reading" if $opt->debug;
234 foreach my $filename (@tar_parts) {
236 next if $filename eq "$host.$num.md5";
238 print "\n\t- $filename";
240 my $path = "$Conf{ArchiveDest}/$filename";
243 my $size = (stat( $path ))[7] || die "can't stat $path: $!";
245 if ($size > $Conf{ArchiveMediaSize}) {
246 print ", part bigger than media $size > $Conf{ArchiveMediaSize}\n";
250 print ", $size bytes";
254 open(my $fh, "gzip -cd $path |") or die "can't open $path: $!";
256 my $tar = Archive::Tar::Streamed->new($fh);
258 my $tar_size_inarc = 0;
261 while(my $entry = $tar->next) {
262 push @tar_files, $entry->name;
264 $tar_size_inarc += $entry->size;
266 if ($tar_size_inarc > $Conf{MaxArchiveFileSize}) {
267 print ", part $filename is too big $tar_size_inarc > $Conf{MaxArchiveFileSize}\n";
275 print ", $items items";
277 if ($tar_size_inarc == 0 && $items == 0) {
278 print ", EMPTY tar\n";
280 my $backup_id = get_backup_id($host, $share, $num);
281 backup_inc_deleted( $backup_id );
291 my $tar_size = get_gzip_size( $path );
294 # finally, check if backup_parts table in database is valid
297 my $md5 = $md5sum->{$filename} || die "no md5sum for $filename in ",dump($md5sum);
301 check_part($host, $num, $part_nr, $tar_size, $size, $md5, $items);
303 # round increment size to 2k block size
304 $inc_size += int(($size + 2048) / 2048);
307 $sth_inc_size->execute(
310 get_backup_id($host, $num),
314 @tar_files = sort @tar_files;
315 print "\n\t",($#tar_files + 1), " tar files";
317 my $sth = $dbh->prepare(qq{
320 JOIN shares on shares.id = shareid
321 JOIN hosts on hosts.id = shares.hostid
322 WHERE hosts.name = ? and backupnum = ?
324 $sth->execute($host, $num);
326 while( my $row = $sth->fetchrow_hashref ) {
328 my $path = $row->{'path'} || die "no path?";
330 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
331 push @db_files, $path;
334 print " ",($#db_files + 1), " database files, diff";
336 @db_files = sort @db_files;
340 if ($#tar_files != $#db_files) {
344 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
345 while ( $diff->Next() ) {
346 next if $diff->Same();
348 print "< $_\n" for $diff->Items(1);
349 print "> $_\n" for $diff->Items(2);
353 print " ",($same ? 'ok' : 'DIFFERENT'),
354 ", dur: ",fmt_time(time() - $t), "\n";
362 foreach ( 0 .. $#{ $opt->host } ) {
364 my $host = $opt->host->[$_];
365 my $num = $opt->num->[$_];
367 check_archive $host => $num;
373 my $sth = $dbh->prepare( qq{
376 backups.id as backup_id,
378 shares.name as share,
383 count(backup_parts.backup_id) as backup_parts
385 join shares on backups.hostid = shares.hostid
386 and shares.id = backups.shareid
387 join hosts on shares.hostid = hosts.id
388 full outer join backup_parts on backups.id = backup_parts.backup_id
389 where not inc_deleted and backups.size > 0
390 group by backups.id, hosts.name, shares.name, backups.num, backups.date, inc_size, parts, backup_parts.backup_id
391 order by backups.date
396 my $num_backups = $sth->rows;
399 while (my $row = $sth->fetchrow_hashref) {
403 my $tar_file = BackupPC::Search::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
405 # this will return -1 if file doesn't exist
406 my $size = BackupPC::Search::get_tgz_size_by_name($tar_file);
408 print "# host: ".$row->{host}.", share: ".$row->{'share'}.", backup_num:".$row->{num}." size: $size backup.size: ", $row->{inc_size},"\n" if $opt->debug;
410 if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size && $row->{parts} == $row->{backup_parts}) {
412 tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
418 print curr_time, " creating $curr_backup/$num_backups ", $row->{host}, ":", $row->{share}, " #", $row->{num},
419 " ", strftime('%Y-%m-%d', localtime($row->{date})), " -> $tar_file";
425 my $cmd = qq[ $tarIncCreate -h "$row->{host}" -s "$row->{share}" -n $row->{num} -f ];
426 print STDERR "## $cmd\n" if ($opt->debug);
428 if (system($cmd) != 0) {
429 print STDERR " FAILED, marking this backup deleted";
430 backup_inc_deleted( $row->{backup_id} );
434 print ", dur: ",fmt_time(time() - $t), "\n";