6 |
use DBI; |
use DBI; |
7 |
use BackupPC::Lib; |
use BackupPC::Lib; |
8 |
use BackupPC::View; |
use BackupPC::View; |
9 |
|
use BackupPC::Attrib qw/:all/; |
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::Streamed; |
16 |
|
use Algorithm::Diff; |
17 |
|
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 |
|
# cludge: minimum .tar.gz size |
26 |
|
my $MIN_TAR_SIZE = 80; |
27 |
|
|
28 |
my $path = abs_path($0); |
my $path = abs_path($0); |
29 |
$path =~ s#/[^/]+$#/#; |
$path =~ s#/[^/]+$#/#; |
32 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
33 |
|
|
34 |
my $bin; |
my $bin; |
35 |
foreach my $c (qw/gzip split/) { |
foreach my $c (qw/gzip md5sum/) { |
36 |
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
37 |
} |
} |
38 |
|
|
39 |
|
my %opt; |
40 |
|
getopts("cd", \%opt ); |
41 |
|
|
42 |
|
my $debug = $opt{d}; |
43 |
|
my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n"; |
44 |
|
|
|
my $debug = 0; |
|
45 |
$|=1; |
$|=1; |
46 |
|
|
47 |
my $start_t = time(); |
my $start_t = time(); |
48 |
|
|
49 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
50 |
|
|
|
my $hosts; |
|
|
my $bpc = BackupPC::Lib->new || die; |
|
|
my %Conf = $bpc->Conf(); |
|
|
my $TopDir = $bpc->TopDir(); |
|
|
my $beenThere = {}; |
|
|
|
|
51 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
52 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
53 |
|
|
72 |
return strftime($t_fmt,localtime()); |
return strftime($t_fmt,localtime()); |
73 |
} |
} |
74 |
|
|
75 |
|
sub tar_check($$$$) { |
76 |
|
my ($host,$share,$num,$filename) = @_; |
77 |
|
|
78 |
|
if ($debug) { |
79 |
|
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
80 |
|
} else { |
81 |
|
print " check"; |
82 |
|
} |
83 |
|
|
84 |
|
my @tar_parts; |
85 |
|
|
86 |
|
if (-d "$tar_dir/$filename") { |
87 |
|
print STDERR " multi-part" if ($opt{d}); |
88 |
|
opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!"; |
89 |
|
@tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir); |
90 |
|
closedir($dir); |
91 |
|
} else { |
92 |
|
push @tar_parts, "${filename}.tar.gz"; |
93 |
|
} |
94 |
|
|
95 |
|
my @tar_files; |
96 |
|
|
97 |
|
print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d}); |
98 |
|
|
99 |
|
print " reading"; |
100 |
|
|
101 |
|
foreach my $tarfilename (@tar_parts) { |
102 |
|
|
103 |
|
print STDERR " $tarfilename" if ($debug); |
104 |
|
|
105 |
|
my $path = "$tar_dir/$tarfilename"; |
106 |
|
my $md5 = $path; |
107 |
|
$md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5"; |
108 |
|
if (! -e $md5) { |
109 |
|
print ", creating md5"; |
110 |
|
system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!"; |
111 |
|
} |
112 |
|
|
113 |
|
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
114 |
|
binmode($fh); |
115 |
|
my $tar = Archive::Tar::Streamed->new($fh); |
116 |
|
|
117 |
|
while(my $entry = $tar->next) { |
118 |
|
push @tar_files, $entry->name; |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
@tar_files = sort @tar_files; |
123 |
|
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
124 |
|
|
125 |
|
print STDERR ", database" if ($debug); |
126 |
|
|
127 |
|
my $sth = $dbh->prepare(qq{ |
128 |
|
SELECT path,type |
129 |
|
FROM files |
130 |
|
JOIN shares on shares.id = shareid |
131 |
|
JOIN hosts on hosts.id = shares.hostid |
132 |
|
WHERE hosts.name = ? and shares.name = ? and backupnum = ? |
133 |
|
}); |
134 |
|
$sth->execute($host, $share, $num); |
135 |
|
my @db_files; |
136 |
|
while( my $row = $sth->fetchrow_hashref ) { |
137 |
|
|
138 |
|
my $path = $row->{'path'} || die "no path?"; |
139 |
|
$path =~ s#^/#./#; |
140 |
|
$path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR); |
141 |
|
push @db_files, $path; |
142 |
|
} |
143 |
|
|
144 |
|
print STDERR " ",($#db_files + 1), " files, diff" if ($debug); |
145 |
|
|
146 |
|
@db_files = sort @db_files; |
147 |
|
|
148 |
|
my $same = 1; |
149 |
|
if ($#tar_files != $#db_files) { |
150 |
|
$same = 0; |
151 |
|
print STDERR " NUMBER" if ($debug); |
152 |
|
} else { |
153 |
|
my $diff = Algorithm::Diff->new(\@tar_files, \@db_files); |
154 |
|
while ( $diff->Next() ) { |
155 |
|
next if $diff->Same(); |
156 |
|
$same = 0; |
157 |
|
print "< $_\n" for $diff->Items(1); |
158 |
|
print "> $_\n" for $diff->Items(2); |
159 |
|
} |
160 |
|
} |
161 |
|
|
162 |
|
print " ",($same ? 'ok' : 'DIFFERENT'); |
163 |
|
print STDERR " }} " if ($debug); |
164 |
|
|
165 |
|
return $same; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
#----- main |
#----- main |
170 |
|
|
171 |
my $sth = $dbh->prepare( qq{ |
my $sth = $dbh->prepare( qq{ |
187 |
} ); |
} ); |
188 |
|
|
189 |
$sth->execute(); |
$sth->execute(); |
190 |
|
my $num_backups = $sth->rows; |
191 |
my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? }); |
my $curr_backup = 1; |
|
my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? }); |
|
|
|
|
|
%BackupPC::SearchLib::Conf = %Conf; |
|
192 |
|
|
193 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
194 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
196 |
# this will return -1 if file doesn't exist |
# this will return -1 if file doesn't exist |
197 |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
198 |
|
|
199 |
print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d}); |
|
|
|
|
my $t = time(); |
|
200 |
|
|
201 |
# re-create archive? |
if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size && ( $check && tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) || 1) ) { |
202 |
if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) { |
next; |
|
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}; |
|
|
print STDERR "## $cmd\n" if ($debug); |
|
|
|
|
|
system($cmd) == 0 or die "failed: $?"; |
|
|
|
|
|
$size = (stat( "$tar_dir/$tar_file" ))[7]; |
|
203 |
} |
} |
204 |
|
|
205 |
if ($size > 45) { |
print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
206 |
|
$curr_backup++; |
|
my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar"; |
|
|
$max_size *= 1024; # convert to bytes |
|
207 |
|
|
208 |
my $parts = int( ($size + $max_size - 1) / $max_size ); |
my $t = time(); |
|
|
|
|
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: $!"; |
|
|
|
|
|
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'}); |
|
209 |
|
|
210 |
printf(" %1.2f MB", ($size / 1024 / 1024)); |
# re-create archive? |
211 |
|
my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f }; |
212 |
|
print STDERR "## $cmd\n" if ($debug); |
213 |
|
|
214 |
} else { |
if (system($cmd) != 0) { |
215 |
$sth_inc_deleted->execute(1, $row->{'backup_id'}); |
print STDERR " FAILED"; |
|
unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n"; |
|
|
print " EMPTY"; |
|
216 |
} |
} |
217 |
|
|
218 |
print ", dur: ",fmt_time(time() - $t), "\n"; |
print ", dur: ",fmt_time(time() - $t), "\n"; |
219 |
|
|
220 |
$dbh->commit; |
$dbh->commit; |