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;
23 BackupPC_incPartsUpdate
27 Create C<.tar.gz> increments on disk calling C<BackupPC_tarIncCreate>.
29 Following options are supported (but all are optional):
35 Update parts for just single C<hostname>
39 Force check for tar archives which exist on disk
50 getopts("cdh:", \%opt );
53 my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
55 my $pid_path = abs_path($0);
56 $pid_path =~ s/\W+/_/g;
58 my $pidfile = new File::Pid({
59 file => "/tmp/$pid_path",
62 if (my $pid = $pidfile->running ) {
63 die "$0 already running: $pid\n";
64 } elsif ($pidfile->pid ne $$) {
66 $pidfile = new File::Pid;
69 print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";
72 my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
73 my %Conf = $bpc->Conf();
75 use BackupPC::SearchLib;
76 %BackupPC::SearchLib::Conf = %Conf;
78 my $path = abs_path($0);
79 $path =~ s#/[^/]+$#/#;
80 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
82 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
85 foreach my $c (qw/gzip md5sum/) {
86 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
93 my $t_fmt = '%Y-%m-%d %H:%M:%S';
95 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
96 my $user = $Conf{SearchUser} || '';
98 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
100 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
102 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
107 my $t = shift || return;
109 my ($ss,$mm,$hh) = gmtime($t);
110 $out .= "${hh}h" if ($hh);
111 $out .= sprintf("%02d:%02d", $mm,$ss);
116 return strftime($t_fmt,localtime());
121 sub get_backup_id($$$) {
122 my ($host, $share, $num) = @_;
124 my $key = "$host $share $num";
125 return $hsn_cache->{$key} if ($hsn_cache->{$key});
127 my $sth = $dbh->prepare(qq{
131 INNER JOIN shares ON backups.shareID=shares.ID
132 INNER JOIN hosts ON backups.hostID = hosts.ID
133 WHERE hosts.name = ? and shares.name = ? and backups.num = ?
135 $sth->execute($host, $share, $num);
136 my ($id) = $sth->fetchrow_array;
138 $hsn_cache->{"$host $share $num"} = $id;
140 print STDERR "# $host $share $num == $id\n" if ($opt{d});
145 sub backup_inc_deleted($) {
146 my $backup_id = shift;
147 my $sth_inc_deleted = $dbh->prepare(qq{
152 $sth_inc_deleted->execute($backup_id);
155 sub tar_check($$$$) {
156 my ($host,$share,$num,$filename) = @_;
159 print curr_time, " check $host:$share#$num -> $filename";
161 # depending on expected returned value this is used like:
162 # my $uncompress_size = get_gzip_size('/full/path/to.gz');
163 # my ($compress_size, $uncompress_size) = get_gzip_size('/path.gz');
164 sub get_gzip_size($) {
165 my $filename = shift;
166 die "file $filename problem: $!" unless (-r $filename);
167 open(my $gzip, $bin->{gzip}." -l $filename |") || die "can't gzip -l $filename: $!";
170 $line = <$gzip> if ($line =~ /^\s+compressed/);
172 my ($comp, $uncomp) = (0,0);
174 if ($line =~ m/^\s+(\d+)\s+(\d+)\s+\d+\.\d+/) {
181 die "can't find size in line: $line";
186 my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
187 my $backup_id = get_backup_id($host, $share, $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);
225 if (-d "$tar_dir/$filename") {
226 print ", multi-part";
227 opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
228 @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
231 push @tar_parts, "${filename}.tar.gz";
234 print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
241 print " reading" if ($opt{d});
243 foreach my $tarfilename (@tar_parts) {
245 print "\n\t- $tarfilename";
247 my $path = "$tar_dir/$tarfilename";
249 my $size = (stat( $path ))[7] || die "can't stat $path: $!";
251 if ($size > $Conf{MaxArchiveSize}) {
252 print ", part bigger than media $size > $Conf{MaxArchiveSize}\n";
256 print ", $size bytes";
259 open(my $fh, "gzip -cd $path |") or die "can't open $path: $!";
261 my $tar = Archive::Tar::Streamed->new($fh);
263 my $tar_size_inarc = 0;
266 while(my $entry = $tar->next) {
267 push @tar_files, $entry->name;
269 $tar_size_inarc += $entry->size;
271 if ($tar_size_inarc > $Conf{MaxArchiveFileSize}) {
272 print ", part $tarfilename is too big $tar_size_inarc > $Conf{MaxArchiveFileSize}\n";
280 print ", $items items";
282 if ($tar_size_inarc == 0 && $items == 0) {
283 print ", EMPTY tar\n";
285 my $backup_id = get_backup_id($host, $share, $num);
286 backup_inc_deleted( $backup_id );
293 my $tar_size = get_gzip_size( $path );
295 # real tar size is bigger because of padding
296 if ($tar_size_inarc > $tar_size) {
297 print ", size of files in tar ($tar_size_inarc) bigger than whole tar ($tar_size)!\n";
302 # check if md5 exists, and if not, create one
305 my $md5_path = $path;
306 $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";
307 if (! -e $md5_path || -z $md5_path) {
308 print ", creating md5";
309 system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";
311 ## FIXME check if existing md5 is valid
314 my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";
317 # extract part number from filename
319 $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);
322 # finally, check if backup_parts table in database is valid
325 check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);
328 # short-cut and exit;
329 return $same unless($same);
331 @tar_files = sort @tar_files;
332 print "\n\t",($#tar_files + 1), " tar files";
334 my $sth = $dbh->prepare(qq{
337 JOIN shares on shares.id = shareid
338 JOIN hosts on hosts.id = shares.hostid
339 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
341 $sth->execute($host, $share, $num);
343 while( my $row = $sth->fetchrow_hashref ) {
345 my $path = $row->{'path'} || die "no path?";
347 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
348 push @db_files, $path;
351 print " ",($#db_files + 1), " database files, diff";
353 @db_files = sort @db_files;
355 if ($#tar_files != $#db_files) {
359 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
360 while ( $diff->Next() ) {
361 next if $diff->Same();
363 print "< $_\n" for $diff->Items(1);
364 print "> $_\n" for $diff->Items(2);
368 print " ",($same ? 'ok' : 'DIFFERENT'),
369 ", dur: ",fmt_time(time() - $t), "\n";
377 my $sth = $dbh->prepare( qq{
380 backups.id as backup_id,
382 shares.name as share,
387 count(backup_parts.backup_id) as backup_parts
389 join shares on backups.hostid = shares.hostid
390 and shares.id = backups.shareid
391 join hosts on shares.hostid = hosts.id
392 full outer join backup_parts on backups.id = backup_parts.backup_id
393 where not inc_deleted and backups.size > 0
394 group by backups.id, hosts.name, shares.name, backups.num, backups.date, inc_size, parts, backup_parts.backup_id
395 order by backups.date
400 my $num_backups = $sth->rows;
404 warn "making increments just for host $opt{h}\n";
407 while (my $row = $sth->fetchrow_hashref) {
409 if ($opt{h} && $row->{host} ne $opt{h}) {
410 warn "skipped $row->{host}\n" if ($debug);
416 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
418 # this will return -1 if file doesn't exist
419 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
421 print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
423 if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size && $row->{parts} == $row->{backup_parts}) {
425 tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
431 print curr_time, " creating $curr_backup/$num_backups ", $row->{host}, ":", $row->{share}, " #", $row->{num},
432 " ", strftime('%Y-%m-%d', localtime($row->{date})), " -> $tar_file";
437 my $cmd = qq[ $tarIncCreate -h "$row->{host}" -s "$row->{share}" -n $row->{num} -f ];
438 print STDERR "## $cmd\n" if ($debug);
440 if (system($cmd) != 0) {
441 print STDERR " FAILED, marking this backup deleted";
442 backup_inc_deleted( $row->{backup_id} );
445 print ", dur: ",fmt_time(time() - $t), "\n";