--- trunk/bin/BackupPC_tarIncCreate 2005/11/08 20:24:45 234 +++ trunk/bin/BackupPC_tarIncCreate 2005/11/10 15:07:10 235 @@ -74,6 +74,7 @@ use POSIX qw/strftime/; use File::Which; use File::Path; +use File::Slurp; use Data::Dumper; ### FIXME die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); @@ -151,6 +152,30 @@ our $ShareName = $opts{s}; our $view = BackupPC::View->new($bpc, $Host, \@Backups); +# database + +my $dsn = $Conf{SearchDSN}; +my $db_user = $Conf{SearchUser} || ''; + +my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 0} ); + +my $sth_inc_size = $dbh->prepare(qq{ + update backups set + inc_size = ?, + parts = ?, + inc_deleted = false + where id = ? }); +my $sth_backup_parts = $dbh->prepare(qq{ + insert into backup_parts ( + backup_id, + part_nr, + tar_size, + size, + md5, + items + ) values (?,?,?,?,?,?) +}); + # # This constant and the line of code below that uses it are borrowed # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander. @@ -185,11 +210,25 @@ my $tar_path = $tar_dir . '/' . $tar_file . '.tmp'; $tar_path =~ s#//#/#g; -print STDERR "working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d}); +my $sth = $dbh->prepare(qq{ + SELECT + backups.id + FROM backups + JOIN shares on shares.id = shareid + JOIN hosts on hosts.id = shares.hostid + WHERE hosts.name = ? and shares.name = ? and backups.num = ? +}); +$sth->execute($Host, $ShareName, $Num); +my ($backup_id) = $sth->fetchrow_array; +$sth->finish; + +print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d}); + my $fh; my $part = 0; my $no_files = 0; +my $items_in_part = 0; sub new_tar_part { if ($fh) { @@ -203,6 +242,23 @@ TarWrite($fh, undef); close($fh) || die "can't close archive part $part: $!"; + + my $file = $tar_path . '/' . $part; + + my $md5 = read_file( $file . '.md5' ) || die "can't read md5sum file ${file}.md5"; + $md5 =~ s/\s.*$//; + + my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz"; + + $sth_backup_parts->execute( + $backup_id, + $part, + $current_tar_size, + $size, + $md5, + $items_in_part, + ); + } $part++; @@ -233,7 +289,9 @@ open($fh, $cmd) or die "can't open $cmd: $!"; binmode($fh); + $current_tar_size = 0; + $items_in_part = 0; } new_tar_part(); @@ -282,6 +340,12 @@ exit(1); } +$sth_inc_size->finish; +$sth_backup_parts->finish; + +$dbh->commit || die "can't commit changes to database"; +$dbh->disconnect(); + exit(0); ########################################################################### @@ -481,9 +545,6 @@ sub seedCache($$$) { my ($host, $share, $dumpNo) = @_; - my $dsn = $Conf{SearchDSN}; - my $db_user = $Conf{SearchUser} || ''; - print STDERR curr_time(), "getting files for $host:$share increment $dumpNo..." if ($opts{v}); my $sql = q{ SELECT path,size @@ -493,7 +554,6 @@ WHERE hosts.name = ? and shares.name = ? and backupnum = ? }; - my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1} ); my $sth = $dbh->prepare($sql); $sth->execute($host, $share, $dumpNo); my $count = $sth->rows; @@ -504,7 +564,6 @@ } $sth->finish(); - $dbh->disconnect(); return $count; } @@ -550,7 +609,8 @@ new_tar_part(); } - print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d}); + #print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d}); + $items_in_part++; if ( defined($PathRemove) && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) { @@ -623,6 +683,8 @@ TarWrite($fh, \$data); $size += length($data); TarWritePad($fh, $size); + + $items_in_part++; } $f->close; $FileCnt++;