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; |
15 |
|
use Archive::Tar::Streamed; |
16 |
|
use Algorithm::Diff; |
17 |
|
use Getopt::Std; |
18 |
|
use File::Slurp; |
19 |
|
|
20 |
|
my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib"; |
21 |
|
my %Conf = $bpc->Conf(); |
22 |
|
|
23 |
|
use BackupPC::SearchLib; |
24 |
|
%BackupPC::SearchLib::Conf = %Conf; |
25 |
|
|
26 |
my $path = abs_path($0); |
my $path = abs_path($0); |
27 |
$path =~ s#/[^/]+$#/#; |
$path =~ s#/[^/]+$#/#; |
29 |
|
|
30 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
31 |
|
|
32 |
my $debug = 0; |
my $bin; |
33 |
|
foreach my $c (qw/gzip md5sum/) { |
34 |
|
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
35 |
|
} |
36 |
|
|
37 |
|
my %opt; |
38 |
|
getopts("cd", \%opt ); |
39 |
|
|
40 |
|
my $debug = $opt{d}; |
41 |
|
my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n"; |
42 |
|
|
43 |
$|=1; |
$|=1; |
44 |
|
|
45 |
my $start_t = time(); |
my $start_t = time(); |
46 |
|
|
47 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
48 |
|
|
|
my $hosts; |
|
|
my $bpc = BackupPC::Lib->new || die; |
|
|
my %Conf = $bpc->Conf(); |
|
|
my $TopDir = $bpc->TopDir(); |
|
|
my $beenThere = {}; |
|
|
|
|
49 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
50 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
51 |
|
|
70 |
return strftime($t_fmt,localtime()); |
return strftime($t_fmt,localtime()); |
71 |
} |
} |
72 |
|
|
73 |
|
my $hsn_cache; |
74 |
|
|
75 |
|
sub get_backup_id($$$) { |
76 |
|
my ($host, $share, $num) = @_; |
77 |
|
|
78 |
|
my $key = "$host $share $num"; |
79 |
|
return $hsn_cache->{$key} if ($hsn_cache->{$key}); |
80 |
|
|
81 |
|
my $sth = $dbh->prepare(qq{ |
82 |
|
SELECT |
83 |
|
backups.id |
84 |
|
FROM backups |
85 |
|
INNER JOIN shares ON backups.shareID=shares.ID |
86 |
|
INNER JOIN hosts ON backups.hostID = hosts.ID |
87 |
|
where hosts.name = ? and shares.name = ? and backups.num = ? |
88 |
|
}); |
89 |
|
$sth->execute($host, $share, $num); |
90 |
|
my ($id) = $sth->fetchrow_array; |
91 |
|
|
92 |
|
$hsn_cache->{"$host $share $num"} = $id; |
93 |
|
|
94 |
|
print STDERR "# $host $share $num == $id\n" if ($opt{d}); |
95 |
|
|
96 |
|
return $id; |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
sub tar_check($$$$) { |
101 |
|
my ($host,$share,$num,$filename) = @_; |
102 |
|
|
103 |
|
sub check_part { |
104 |
|
my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_; |
105 |
|
my $backup_id = get_backup_id($host, $share, $num); |
106 |
|
my $sth_md5 = $dbh->prepare(qq{ |
107 |
|
select |
108 |
|
id, tar_size, size, md5, items |
109 |
|
from backup_parts |
110 |
|
where backup_id = ? and part_nr = ? |
111 |
|
}); |
112 |
|
|
113 |
|
$sth_md5->execute($backup_id, $part_nr); |
114 |
|
|
115 |
|
if (my $row = $sth_md5->fetchrow_hashref) { |
116 |
|
return if ( |
117 |
|
$row->{tar_size} >= $tar_size && |
118 |
|
$row->{size} == $size && |
119 |
|
$row->{md5} eq $md5 && |
120 |
|
$row->{items} == $items |
121 |
|
); |
122 |
|
print STDERR "# deleting invalid row $row->{id}\n" if ($opt{d}); |
123 |
|
$dbh->do(qq{ delete from backup_parts where id = $row->{id} }); |
124 |
|
} |
125 |
|
print STDERR "# inserting new backup_part row\n"; |
126 |
|
my $sth_insert = $dbh->prepare(qq{ |
127 |
|
insert into backup_parts ( |
128 |
|
backup_id, |
129 |
|
part_nr, |
130 |
|
tar_size, |
131 |
|
size, |
132 |
|
md5, |
133 |
|
items |
134 |
|
) values (?,?,?,?,?,?) |
135 |
|
}); |
136 |
|
|
137 |
|
$sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items); |
138 |
|
$dbh->commit; |
139 |
|
} |
140 |
|
|
141 |
|
if ($debug) { |
142 |
|
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
143 |
|
} else { |
144 |
|
print " check"; |
145 |
|
} |
146 |
|
|
147 |
|
my @tar_parts; |
148 |
|
|
149 |
|
if (-d "$tar_dir/$filename") { |
150 |
|
print STDERR " multi-part" if ($opt{d}); |
151 |
|
opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!"; |
152 |
|
@tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir); |
153 |
|
closedir($dir); |
154 |
|
} else { |
155 |
|
push @tar_parts, "${filename}.tar.gz"; |
156 |
|
} |
157 |
|
|
158 |
|
print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d}); |
159 |
|
|
160 |
|
my $same = 1; |
161 |
|
my @tar_files; |
162 |
|
|
163 |
|
my $backup_part; |
164 |
|
|
165 |
|
print " reading"; |
166 |
|
|
167 |
|
foreach my $tarfilename (@tar_parts) { |
168 |
|
|
169 |
|
print STDERR " $tarfilename" if ($debug); |
170 |
|
|
171 |
|
my $path = "$tar_dir/$tarfilename"; |
172 |
|
my $md5_path = $path; |
173 |
|
$md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path"; |
174 |
|
if (! -e $md5_path || -z $md5_path) { |
175 |
|
print ", creating md5"; |
176 |
|
system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!"; |
177 |
|
} |
178 |
|
|
179 |
|
my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!"; |
180 |
|
$md5 =~ s#\s.*$##; |
181 |
|
|
182 |
|
my $part_nr = 1; |
183 |
|
$part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#); |
184 |
|
|
185 |
|
my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename"; |
186 |
|
|
187 |
|
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
188 |
|
binmode($fh); |
189 |
|
my $tar = Archive::Tar::Streamed->new($fh); |
190 |
|
|
191 |
|
my $tar_size = 0; |
192 |
|
my $items = 0; |
193 |
|
|
194 |
|
while(my $entry = $tar->next) { |
195 |
|
push @tar_files, $entry->name; |
196 |
|
$items++; |
197 |
|
$tar_size += $entry->size; |
198 |
|
} |
199 |
|
|
200 |
|
if ($tar_size > $Conf{MaxArchiveFileSize}) { |
201 |
|
print STDERR " part too big $tar_size > $Conf{MaxArchiveFileSize} }}" if ($debug); |
202 |
|
$same = 0; |
203 |
|
last; |
204 |
|
} elsif ($size > $Conf{MaxArchiveSize}) { |
205 |
|
print STDERR " part bigger than media $size > $Conf{MaxArchiveSize} }}" if ($debug); |
206 |
|
$same = 0; |
207 |
|
last; |
208 |
|
} |
209 |
|
|
210 |
|
check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items); |
211 |
|
} |
212 |
|
|
213 |
|
# short-cut and exit; |
214 |
|
return $same unless($same); |
215 |
|
|
216 |
|
@tar_files = sort @tar_files; |
217 |
|
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
218 |
|
|
219 |
|
print STDERR ", database" if ($debug); |
220 |
|
|
221 |
|
my $sth = $dbh->prepare(qq{ |
222 |
|
SELECT path,type |
223 |
|
FROM files |
224 |
|
JOIN shares on shares.id = shareid |
225 |
|
JOIN hosts on hosts.id = shares.hostid |
226 |
|
WHERE hosts.name = ? and shares.name = ? and backupnum = ? |
227 |
|
}); |
228 |
|
$sth->execute($host, $share, $num); |
229 |
|
my @db_files; |
230 |
|
while( my $row = $sth->fetchrow_hashref ) { |
231 |
|
|
232 |
|
my $path = $row->{'path'} || die "no path?"; |
233 |
|
$path =~ s#^/#./#; |
234 |
|
$path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR); |
235 |
|
push @db_files, $path; |
236 |
|
} |
237 |
|
|
238 |
|
print STDERR " ",($#db_files + 1), " files, diff" if ($debug); |
239 |
|
|
240 |
|
@db_files = sort @db_files; |
241 |
|
|
242 |
|
if ($#tar_files != $#db_files) { |
243 |
|
$same = 0; |
244 |
|
print STDERR " NUMBER" if ($debug); |
245 |
|
} else { |
246 |
|
my $diff = Algorithm::Diff->new(\@tar_files, \@db_files); |
247 |
|
while ( $diff->Next() ) { |
248 |
|
next if $diff->Same(); |
249 |
|
$same = 0; |
250 |
|
print "< $_\n" for $diff->Items(1); |
251 |
|
print "> $_\n" for $diff->Items(2); |
252 |
|
} |
253 |
|
} |
254 |
|
|
255 |
|
print " ",($same ? 'ok' : 'DIFFERENT'); |
256 |
|
print STDERR " }} " if ($debug); |
257 |
|
|
258 |
|
return $same; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
#----- main |
#----- main |
263 |
|
|
264 |
my $sth = $dbh->prepare( qq{ |
my $sth = $dbh->prepare( qq{ |
267 |
backups.id as backup_id, |
backups.id as backup_id, |
268 |
hosts.name as host, |
hosts.name as host, |
269 |
shares.name as share, |
shares.name as share, |
270 |
backups.num as num |
backups.num as num, |
271 |
|
inc_size, |
272 |
|
parts |
273 |
from backups |
from backups |
274 |
join shares on backups.hostid = shares.hostid |
join shares on backups.hostid = shares.hostid |
275 |
and shares.id = backups.shareid |
and shares.id = backups.shareid |
276 |
join hosts on shares.hostid = hosts.id |
join hosts on shares.hostid = hosts.id |
277 |
where inc_size < 0 and not inc_deleted |
where not inc_deleted |
278 |
order by backups.date |
order by backups.date |
279 |
|
|
280 |
} ); |
} ); |
281 |
|
|
282 |
$sth->execute(); |
$sth->execute(); |
283 |
|
my $num_backups = $sth->rows; |
284 |
my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ? where id = ? }); |
my $curr_backup = 1; |
|
my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? }); |
|
|
|
|
|
%BackupPC::SearchLib::Conf = %Conf; |
|
285 |
|
|
286 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
287 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
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 "; |
|
288 |
|
|
289 |
my $t = time(); |
# this will return -1 if file doesn't exist |
290 |
|
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
291 |
|
|
292 |
my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file}; |
print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d}); |
|
print STDERR "## $cmd\n" if ($debug); |
|
293 |
|
|
294 |
system($cmd) == 0 or die "failed: $?"; |
if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size) { |
295 |
|
if ($check) { |
296 |
|
tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next; |
297 |
|
} else { |
298 |
|
next; |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
my $size = (stat( "$tar_dir/$tar_file" ))[7]; |
print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
303 |
|
$curr_backup++; |
304 |
|
|
305 |
print " dur: ",fmt_time(time() - $t)," $size bytes"; |
my $t = time(); |
306 |
|
|
307 |
if ($size > 45) { |
# re-create archive? |
308 |
$sth_inc_size->execute($size, $row->{'backup_id'}); |
my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f }; |
309 |
$sth_inc_deleted->execute(0, $row->{'backup_id'}); |
print STDERR "## $cmd\n" if ($debug); |
310 |
} else { |
|
311 |
$sth_inc_deleted->execute(1, $row->{'backup_id'}); |
if (system($cmd) != 0) { |
312 |
unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n"; |
print STDERR " FAILED"; |
|
print " EMPTY"; |
|
313 |
} |
} |
314 |
print "\n"; |
|
315 |
|
print ", dur: ",fmt_time(time() - $t), "\n"; |
316 |
|
|
317 |
$dbh->commit; |
$dbh->commit; |
318 |
|
|