10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
12 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
|
use BackupPC::SearchLib; |
|
13 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
14 |
use File::Which; |
use File::Which; |
15 |
use Archive::Tar; |
use Archive::Tar::Streamed; |
16 |
use Algorithm::Diff; |
use Algorithm::Diff; |
17 |
use Getopt::Std; |
use Getopt::Std; |
18 |
|
|
19 |
|
my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib"; |
20 |
|
my %Conf = $bpc->Conf(); |
21 |
|
|
22 |
|
use BackupPC::SearchLib; |
23 |
|
%BackupPC::SearchLib::Conf = %Conf; |
24 |
|
|
25 |
my $path = abs_path($0); |
my $path = abs_path($0); |
26 |
$path =~ s#/[^/]+$#/#; |
$path =~ s#/[^/]+$#/#; |
27 |
my $tarIncCreate = $path .= 'BackupPC_tarIncCreate'; |
my $tarIncCreate = $path .= 'BackupPC_tarIncCreate'; |
29 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
30 |
|
|
31 |
my $bin; |
my $bin; |
32 |
foreach my $c (qw/gzip split/) { |
foreach my $c (qw/gzip md5sum/) { |
33 |
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
34 |
} |
} |
35 |
|
|
45 |
|
|
46 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
47 |
|
|
|
my $hosts; |
|
|
my $bpc = BackupPC::Lib->new || die; |
|
|
my %Conf = $bpc->Conf(); |
|
|
my $TopDir = $bpc->TopDir(); |
|
|
my $beenThere = {}; |
|
|
|
|
48 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
49 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
50 |
|
|
69 |
return strftime($t_fmt,localtime()); |
return strftime($t_fmt,localtime()); |
70 |
} |
} |
71 |
|
|
|
sub tar_join($) { |
|
|
my $filename = shift; |
|
|
|
|
|
my $in = my $out = $filename; |
|
|
$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: $?"; |
|
|
|
|
|
} |
|
|
|
|
72 |
sub tar_check($$$$) { |
sub tar_check($$$$) { |
73 |
my ($host,$share,$num,$filename) = @_; |
my ($host,$share,$num,$filename) = @_; |
74 |
|
|
78 |
print " check"; |
print " check"; |
79 |
} |
} |
80 |
|
|
81 |
if (-d $filename) { |
my @tar_parts; |
82 |
print STDERR ", joining"; |
|
83 |
tar_join($filename); |
if (-d "$tar_dir/$filename") { |
84 |
|
print STDERR " multi-part" if ($opt{d}); |
85 |
|
opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!"; |
86 |
|
@tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir); |
87 |
|
closedir($dir); |
88 |
|
} else { |
89 |
|
push @tar_parts, "${filename}.tar.gz"; |
90 |
} |
} |
91 |
|
|
92 |
my $tar = Archive::Tar->new; |
print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d}); |
93 |
my $comp = 0; |
|
94 |
$comp = 1 if ($filename =~ m/\.(gz|tgz)$/); |
my $same = 1; |
95 |
print STDERR ", opening" if ($debug); |
my @tar_files; |
96 |
$tar->read($filename, $comp) or die "can't open $filename: $!"; |
|
97 |
|
print " reading"; |
98 |
|
|
99 |
|
foreach my $tarfilename (@tar_parts) { |
100 |
|
|
101 |
|
print STDERR " $tarfilename" if ($debug); |
102 |
|
|
103 |
print STDERR ", tar" if ($debug); |
my $path = "$tar_dir/$tarfilename"; |
104 |
my @tar_files = sort $tar->list_files(); |
my $md5 = $path; |
105 |
|
$md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5"; |
106 |
|
if (! -e $md5) { |
107 |
|
print ", creating md5"; |
108 |
|
system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!"; |
109 |
|
} |
110 |
|
|
111 |
|
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
112 |
|
binmode($fh); |
113 |
|
my $tar = Archive::Tar::Streamed->new($fh); |
114 |
|
|
115 |
|
my $total_size = 0; |
116 |
|
|
117 |
|
while(my $entry = $tar->next) { |
118 |
|
push @tar_files, $entry->name; |
119 |
|
$total_size += $entry->size; |
120 |
|
} |
121 |
|
|
122 |
|
if ($total_size > $Conf{MaxArchiveFileSize}) { |
123 |
|
print STDERR " part too big $total_size > $Conf{MaxArchiveFileSize} }}" if ($debug); |
124 |
|
$same = 0; |
125 |
|
last; |
126 |
|
} elsif ($total_size > $Conf{MaxArchiveSize}) { |
127 |
|
print STDERR " part bigger than media $total_size > $Conf{MaxArchiveSize} }}" if ($debug); |
128 |
|
$same = 0; |
129 |
|
last; |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
|
# short-cut and exit; |
134 |
|
return $same unless($same); |
135 |
|
|
136 |
|
@tar_files = sort @tar_files; |
137 |
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
138 |
|
|
139 |
print STDERR ", database" if ($debug); |
print STDERR ", database" if ($debug); |
159 |
|
|
160 |
@db_files = sort @db_files; |
@db_files = sort @db_files; |
161 |
|
|
|
my $same = 1; |
|
162 |
if ($#tar_files != $#db_files) { |
if ($#tar_files != $#db_files) { |
163 |
$same = 0; |
$same = 0; |
164 |
print STDERR " NUMBER" if ($debug); |
print STDERR " NUMBER" if ($debug); |
172 |
} |
} |
173 |
} |
} |
174 |
|
|
175 |
print STDERR " ",($same ? 'ok' : 'DIFFERENT'); |
print " ",($same ? 'ok' : 'DIFFERENT'); |
176 |
print STDERR " }} " if ($debug); |
print STDERR " }} " if ($debug); |
177 |
|
|
178 |
return $same; |
return $same; |
199 |
|
|
200 |
} ); |
} ); |
201 |
|
|
|
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; |
|
|
|
|
202 |
$sth->execute(); |
$sth->execute(); |
203 |
my $num_backups = $sth->rows; |
my $num_backups = $sth->rows; |
204 |
my $curr_backup = 1; |
my $curr_backup = 1; |
209 |
# this will return -1 if file doesn't exist |
# this will return -1 if file doesn't exist |
210 |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
211 |
|
|
212 |
|
print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d}); |
213 |
|
|
214 |
|
if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size) { |
215 |
|
if ($check) { |
216 |
|
tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next; |
217 |
|
} else { |
218 |
|
next; |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
223 |
$curr_backup++; |
$curr_backup++; |
224 |
|
|
225 |
my $t = time(); |
my $t = time(); |
226 |
|
|
227 |
# re-create archive? |
# re-create archive? |
228 |
if ($row->{'inc_size'} == -1 || $size == -1 || |
my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f }; |
229 |
$row->{'inc_size'} != $size || |
print STDERR "## $cmd\n" if ($debug); |
|
$check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file") |
|
|
) { |
|
|
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}.tmp}; |
|
|
print STDERR "## $cmd\n" if ($debug); |
|
|
|
|
|
system($cmd) == 0 or die "failed: $?"; |
|
230 |
|
|
231 |
rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!"; |
if (system($cmd) != 0) { |
232 |
|
print STDERR " FAILED"; |
|
$size = (stat( "$tar_dir/$tar_file" ))[7]; |
|
233 |
} |
} |
234 |
|
|
|
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"; |
|
|
tar_join("$tar_dir/$tar_file"); |
|
|
} |
|
|
|
|
|
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: $!"; |
|
|
|
|
|
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: $!"; |
|
|
} |
|
|
|
|
|
$sth_inc_size->execute($size, $parts, $row->{'backup_id'}); |
|
|
$sth_inc_deleted->execute(0, $row->{'backup_id'}); |
|
|
|
|
|
printf(" %1.2f MB", ($size / 1024 / 1024)); |
|
|
|
|
|
} 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"; |
|
|
} |
|
235 |
print ", dur: ",fmt_time(time() - $t), "\n"; |
print ", dur: ",fmt_time(time() - $t), "\n"; |
236 |
|
|
237 |
$dbh->commit; |
$dbh->commit; |