11 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
12 |
use BackupPC::SearchLib; |
use BackupPC::SearchLib; |
13 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
14 |
|
use File::Which; |
15 |
|
|
16 |
my $path = abs_path($0); |
my $path = abs_path($0); |
17 |
$path =~ s#/[^/]+$#/#; |
$path =~ s#/[^/]+$#/#; |
19 |
|
|
20 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
21 |
|
|
22 |
|
my $bin; |
23 |
|
foreach my $c (qw/gzip split/) { |
24 |
|
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
25 |
|
} |
26 |
|
|
27 |
|
|
28 |
my $debug = 0; |
my $debug = 0; |
29 |
$|=1; |
$|=1; |
30 |
|
|
70 |
backups.id as backup_id, |
backups.id as backup_id, |
71 |
hosts.name as host, |
hosts.name as host, |
72 |
shares.name as share, |
shares.name as share, |
73 |
backups.num as num |
backups.num as num, |
74 |
|
inc_size, |
75 |
|
parts |
76 |
from backups |
from backups |
77 |
join shares on backups.hostid = shares.hostid |
join shares on backups.hostid = shares.hostid |
78 |
and shares.id = backups.shareid |
and shares.id = backups.shareid |
79 |
join hosts on shares.hostid = hosts.id |
join hosts on shares.hostid = hosts.id |
80 |
where inc_size < 0 and not inc_deleted |
where not inc_deleted |
81 |
order by backups.date |
order by backups.date |
82 |
|
|
83 |
} ); |
} ); |
84 |
|
|
85 |
$sth->execute(); |
$sth->execute(); |
86 |
|
|
87 |
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 = ? }); |
88 |
my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? }); |
my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? }); |
89 |
|
|
90 |
%BackupPC::SearchLib::Conf = %Conf; |
%BackupPC::SearchLib::Conf = %Conf; |
91 |
|
|
92 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
93 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
94 |
|
|
95 |
|
# this will return -1 if file doesn't exist |
96 |
|
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
97 |
|
|
98 |
print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
99 |
|
|
100 |
my $t = time(); |
my $t = time(); |
101 |
|
|
102 |
my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file}; |
# re-create archive? |
103 |
print STDERR "## $cmd\n" if ($debug); |
if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) { |
104 |
|
my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | $bin->{'gzip'} -9 > $tar_dir/$tar_file}; |
105 |
|
print STDERR "## $cmd\n" if ($debug); |
106 |
|
|
107 |
|
system($cmd) == 0 or die "failed: $?"; |
108 |
|
|
109 |
|
$size = (stat( "$tar_dir/$tar_file" ))[7]; |
110 |
|
} |
111 |
|
|
112 |
|
if ($size > 45) { |
113 |
|
|
114 |
system($cmd) == 0 or die "failed: $?"; |
my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar"; |
115 |
|
$max_size *= 1024; # convert to bytes |
116 |
|
|
117 |
my $size = (stat( "$tar_dir/$tar_file" ))[7]; |
my $parts = int( ($size + $max_size - 1) / $max_size ); |
118 |
|
|
119 |
printf(" %1.2f MB, dur: %s", ($size / 1024 / 1024), fmt_time(time() - $t)); |
if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) { |
120 |
|
print " join"; |
121 |
|
|
122 |
if ($size > 45) { |
my $in = my $out = "$tar_dir/$tar_file"; |
123 |
$sth_inc_size->execute($size, $row->{'backup_id'}); |
$out .= '.tmp'; |
124 |
|
|
125 |
|
# FIXME I should really order parts manually! |
126 |
|
system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?"; |
127 |
|
} |
128 |
|
|
129 |
|
if ($size > $max_size && ! -d "$tar_dir/$tar_file") { |
130 |
|
print " split/$parts"; |
131 |
|
my $in = my $out = "$tar_dir/$tar_file"; |
132 |
|
$out .= '.tmp'; |
133 |
|
rename $in, $out || die "can't rename $in: $!"; |
134 |
|
mkdir $in || die "can't mkdir $in: $!"; |
135 |
|
|
136 |
|
my $suffix_len = length("$parts"); |
137 |
|
system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?"; |
138 |
|
unlink $out || die "can't unlink $out: $!"; |
139 |
|
} |
140 |
|
|
141 |
|
$sth_inc_size->execute($size, $parts, $row->{'backup_id'}); |
142 |
$sth_inc_deleted->execute(0, $row->{'backup_id'}); |
$sth_inc_deleted->execute(0, $row->{'backup_id'}); |
143 |
|
|
144 |
|
printf(" %1.2f MB", ($size / 1024 / 1024)); |
145 |
|
|
146 |
} else { |
} else { |
147 |
$sth_inc_deleted->execute(1, $row->{'backup_id'}); |
$sth_inc_deleted->execute(1, $row->{'backup_id'}); |
148 |
unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n"; |
unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n"; |
149 |
print " EMPTY"; |
print " EMPTY"; |
150 |
} |
} |
151 |
print "\n"; |
print ", dur: ",fmt_time(time() - $t), "\n"; |
152 |
|
|
153 |
$dbh->commit; |
$dbh->commit; |
154 |
|
|