1 |
#!/usr/local/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use lib "__INSTALLDIR__/lib"; |
5 |
|
6 |
use DBI; |
7 |
use BackupPC::Lib; |
8 |
use BackupPC::View; |
9 |
use BackupPC::Attrib qw/:all/; |
10 |
use Data::Dumper; |
11 |
use Time::HiRes qw/time/; |
12 |
use POSIX qw/strftime/; |
13 |
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); |
27 |
$path =~ s#/[^/]+$#/#; |
28 |
my $tarIncCreate = $path .= 'BackupPC_tarIncCreate'; |
29 |
|
30 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
31 |
|
32 |
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; |
44 |
|
45 |
my $start_t = time(); |
46 |
|
47 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
48 |
|
49 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
50 |
my $user = $Conf{SearchUser} || ''; |
51 |
|
52 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
53 |
|
54 |
my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}; |
55 |
|
56 |
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir); |
57 |
|
58 |
#---- subs ---- |
59 |
|
60 |
sub fmt_time { |
61 |
my $t = shift || return; |
62 |
my $out = ""; |
63 |
my ($ss,$mm,$hh) = gmtime($t); |
64 |
$out .= "${hh}h" if ($hh); |
65 |
$out .= sprintf("%02d:%02d", $mm,$ss); |
66 |
return $out; |
67 |
} |
68 |
|
69 |
sub curr_time { |
70 |
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 |
my $t = time(); |
104 |
print curr_time, " check $host:$share#$num -> $filename"; |
105 |
|
106 |
sub check_part { |
107 |
my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_; |
108 |
my $backup_id = get_backup_id($host, $share, $num); |
109 |
my $sth_md5 = $dbh->prepare(qq{ |
110 |
select |
111 |
id, tar_size, size, md5, items |
112 |
from backup_parts |
113 |
where backup_id = ? and part_nr = ? |
114 |
}); |
115 |
|
116 |
$sth_md5->execute($backup_id, $part_nr); |
117 |
|
118 |
if (my $row = $sth_md5->fetchrow_hashref) { |
119 |
return if ( |
120 |
$row->{tar_size} >= $tar_size && |
121 |
$row->{size} == $size && |
122 |
$row->{md5} eq $md5 && |
123 |
$row->{items} == $items |
124 |
); |
125 |
print ", deleting invalid backup_parts $row->{id}"; |
126 |
$dbh->do(qq{ delete from backup_parts where id = $row->{id} }); |
127 |
} |
128 |
print ", inserting new"; |
129 |
my $sth_insert = $dbh->prepare(qq{ |
130 |
insert into backup_parts ( |
131 |
backup_id, |
132 |
part_nr, |
133 |
tar_size, |
134 |
size, |
135 |
md5, |
136 |
items |
137 |
) values (?,?,?,?,?,?) |
138 |
}); |
139 |
|
140 |
$sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items); |
141 |
$dbh->commit; |
142 |
} |
143 |
|
144 |
my @tar_parts; |
145 |
|
146 |
if (-d "$tar_dir/$filename") { |
147 |
print ", multi-part"; |
148 |
opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!"; |
149 |
@tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir); |
150 |
closedir($dir); |
151 |
} else { |
152 |
push @tar_parts, "${filename}.tar.gz"; |
153 |
} |
154 |
|
155 |
print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d}); |
156 |
|
157 |
my $same = 1; |
158 |
my @tar_files; |
159 |
|
160 |
my $backup_part; |
161 |
|
162 |
print " reading" if ($opt{d}); |
163 |
|
164 |
foreach my $tarfilename (@tar_parts) { |
165 |
|
166 |
print "\n\t- $tarfilename"; |
167 |
|
168 |
my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename"; |
169 |
|
170 |
if ($size > $Conf{MaxArchiveSize}) { |
171 |
print ", part bigger than media $size > $Conf{MaxArchiveSize}\n"; |
172 |
return 0; |
173 |
} |
174 |
|
175 |
print ", $size bytes"; |
176 |
|
177 |
my $path = "$tar_dir/$tarfilename"; |
178 |
|
179 |
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
180 |
binmode($fh); |
181 |
my $tar = Archive::Tar::Streamed->new($fh); |
182 |
|
183 |
my $tar_size = 0; |
184 |
my $items = 0; |
185 |
|
186 |
while(my $entry = $tar->next) { |
187 |
push @tar_files, $entry->name; |
188 |
$items++; |
189 |
$tar_size += $entry->size; |
190 |
|
191 |
if ($tar_size > $Conf{MaxArchiveFileSize}) { |
192 |
print ", part $tarfilename is too big $tar_size > $Conf{MaxArchiveFileSize}\n"; |
193 |
return 0; |
194 |
} |
195 |
|
196 |
} |
197 |
|
198 |
print ", $items items"; |
199 |
|
200 |
# |
201 |
# check if md5 exists, and if not, create one |
202 |
# |
203 |
|
204 |
my $md5_path = $path; |
205 |
$md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path"; |
206 |
if (! -e $md5_path || -z $md5_path) { |
207 |
print ", creating md5"; |
208 |
system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!"; |
209 |
} else { |
210 |
## FIXME check if existing md5 is valid |
211 |
} |
212 |
|
213 |
my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!"; |
214 |
$md5 =~ s#\s.*$##; |
215 |
|
216 |
# extract part number from filename |
217 |
my $part_nr = 1; |
218 |
$part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#); |
219 |
|
220 |
# |
221 |
# finally, check if backup_parts table in database is valid |
222 |
# |
223 |
|
224 |
check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items); |
225 |
} |
226 |
|
227 |
# short-cut and exit; |
228 |
return $same unless($same); |
229 |
|
230 |
@tar_files = sort @tar_files; |
231 |
print "\n\t",($#tar_files + 1), " tar files"; |
232 |
|
233 |
my $sth = $dbh->prepare(qq{ |
234 |
SELECT path,type |
235 |
FROM files |
236 |
JOIN shares on shares.id = shareid |
237 |
JOIN hosts on hosts.id = shares.hostid |
238 |
WHERE hosts.name = ? and shares.name = ? and backupnum = ? |
239 |
}); |
240 |
$sth->execute($host, $share, $num); |
241 |
my @db_files; |
242 |
while( my $row = $sth->fetchrow_hashref ) { |
243 |
|
244 |
my $path = $row->{'path'} || die "no path?"; |
245 |
$path =~ s#^/#./#; |
246 |
$path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR); |
247 |
push @db_files, $path; |
248 |
} |
249 |
|
250 |
print " ",($#db_files + 1), " database files, diff"; |
251 |
|
252 |
@db_files = sort @db_files; |
253 |
|
254 |
if ($#tar_files != $#db_files) { |
255 |
$same = 0; |
256 |
print " NUMBER"; |
257 |
} else { |
258 |
my $diff = Algorithm::Diff->new(\@tar_files, \@db_files); |
259 |
while ( $diff->Next() ) { |
260 |
next if $diff->Same(); |
261 |
$same = 0; |
262 |
print "< $_\n" for $diff->Items(1); |
263 |
print "> $_\n" for $diff->Items(2); |
264 |
} |
265 |
} |
266 |
|
267 |
print " ",($same ? 'ok' : 'DIFFERENT'), |
268 |
", dur: ",fmt_time(time() - $t), "\n"; |
269 |
|
270 |
return $same; |
271 |
} |
272 |
|
273 |
|
274 |
#----- main |
275 |
|
276 |
my $sth = $dbh->prepare( qq{ |
277 |
|
278 |
select |
279 |
backups.id as backup_id, |
280 |
hosts.name as host, |
281 |
shares.name as share, |
282 |
backups.num as num, |
283 |
inc_size, |
284 |
parts |
285 |
from backups |
286 |
join shares on backups.hostid = shares.hostid |
287 |
and shares.id = backups.shareid |
288 |
join hosts on shares.hostid = hosts.id |
289 |
where not inc_deleted |
290 |
order by backups.date |
291 |
|
292 |
} ); |
293 |
|
294 |
$sth->execute(); |
295 |
my $num_backups = $sth->rows; |
296 |
my $curr_backup = 1; |
297 |
|
298 |
while (my $row = $sth->fetchrow_hashref) { |
299 |
|
300 |
$curr_backup++; |
301 |
|
302 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
303 |
|
304 |
# this will return -1 if file doesn't exist |
305 |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
306 |
|
307 |
print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d}); |
308 |
|
309 |
if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size) { |
310 |
if ($check) { |
311 |
tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next; |
312 |
} else { |
313 |
next; |
314 |
} |
315 |
} |
316 |
|
317 |
print curr_time, " creating $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
318 |
|
319 |
my $t = time(); |
320 |
|
321 |
# re-create archive? |
322 |
my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f }; |
323 |
print STDERR "## $cmd\n" if ($debug); |
324 |
|
325 |
if (system($cmd) != 0) { |
326 |
print STDERR " FAILED"; |
327 |
} |
328 |
|
329 |
print ", dur: ",fmt_time(time() - $t), "\n"; |
330 |
|
331 |
$dbh->commit; |
332 |
|
333 |
} |
334 |
|
335 |
undef $sth; |
336 |
$dbh->disconnect; |