use BackupPC::View;
use Data::Dumper;
use Time::HiRes qw/time/;
-use File::Pid;
use POSIX qw/strftime/;
use BackupPC::SearchLib;
use Cwd qw/abs_path/;
+use File::Which;
my $path = abs_path($0);
$path =~ s#/[^/]+$#/#;
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
-my $debug = 1;
+my $bin;
+foreach my $c (qw/gzip split/) {
+ $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
+}
+
+
+my $debug = 0;
$|=1;
my $start_t = time();
backups.id as backup_id,
hosts.name as host,
shares.name as share,
- backups.num as num
+ backups.num as num,
+ inc_size,
+ parts
from backups
join shares on backups.hostid = shares.hostid
and shares.id = backups.shareid
join hosts on shares.hostid = hosts.id
-where inc_size < 0 and not inc_deleted
+where not inc_deleted
order by backups.date
} );
$sth->execute();
-my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ? where id = ? });
+my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });
+my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
%BackupPC::SearchLib::Conf = %Conf;
while (my $row = $sth->fetchrow_hashref) {
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
- print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
+
+ # this will return -1 if file doesn't exist
+ my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
+
+ print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
my $t = time();
- my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};
- print STDERR "## $cmd\n" if ($debug);
+ # re-create archive?
+ if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) {
+ my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | $bin->{'gzip'} $Conf{GzipLevel} > $tar_dir/$tar_file};
+ print STDERR "## $cmd\n" if ($debug);
+
+ system($cmd) == 0 or die "failed: $?";
+
+ $size = (stat( "$tar_dir/$tar_file" ))[7];
+ }
+
+ if ($size > 45) {
+
+ my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
+ $max_size *= 1024; # convert to bytes
+
+ my $parts = int( ($size + $max_size - 1) / $max_size );
+
+ if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
+ print " join";
+
+ my $in = my $out = "$tar_dir/$tar_file";
+ $out .= '.tmp';
+
+ # FIXME I should really order parts manually!
+ system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
+ }
+
+ if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
+ print " split/$parts";
+ my $in = my $out = "$tar_dir/$tar_file";
+ $out .= '.tmp';
+ rename $in, $out || die "can't rename $in: $!";
+ mkdir $in || die "can't mkdir $in: $!";
- system($cmd) == 0 or die "failed: $?";
+ my $suffix_len = length("$parts");
+ system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
+ unlink $out || die "can't unlink $out: $!";
+ }
- my $size = (stat( "$tar_dir/$tar_file" ))[7];
+ $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
+ $sth_inc_deleted->execute(0, $row->{'backup_id'});
- print fmt_time(time() - $t)," $size bytes\n";
+ printf(" %1.2f MB", ($size / 1024 / 1024));
- $sth_inc_size->execute($size, $row->{'backup_id'});
+ } else {
+ $sth_inc_deleted->execute(1, $row->{'backup_id'});
+ unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
+ print " EMPTY";
+ }
+ print ", dur: ",fmt_time(time() - $t), "\n";
$dbh->commit;