15 |
use Archive::Tar::Streamed; |
use Archive::Tar::Streamed; |
16 |
use Algorithm::Diff; |
use Algorithm::Diff; |
17 |
use Getopt::Std; |
use Getopt::Std; |
18 |
|
use File::Slurp; |
19 |
|
|
20 |
my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib"; |
my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib"; |
21 |
my %Conf = $bpc->Conf(); |
my %Conf = $bpc->Conf(); |
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($$$$) { |
sub tar_check($$$$) { |
101 |
my ($host,$share,$num,$filename) = @_; |
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) { |
if ($debug) { |
142 |
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
143 |
} else { |
} else { |
160 |
my $same = 1; |
my $same = 1; |
161 |
my @tar_files; |
my @tar_files; |
162 |
|
|
163 |
|
my $backup_part; |
164 |
|
|
165 |
print " reading"; |
print " reading"; |
166 |
|
|
167 |
foreach my $tarfilename (@tar_parts) { |
foreach my $tarfilename (@tar_parts) { |
169 |
print STDERR " $tarfilename" if ($debug); |
print STDERR " $tarfilename" if ($debug); |
170 |
|
|
171 |
my $path = "$tar_dir/$tarfilename"; |
my $path = "$tar_dir/$tarfilename"; |
172 |
my $md5 = $path; |
my $md5_path = $path; |
173 |
$md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5"; |
$md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path"; |
174 |
if (! -e $md5) { |
if (! -e $md5_path) { |
175 |
print ", creating md5"; |
print ", creating md5"; |
176 |
system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!"; |
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 |
|
|
181 |
|
my $part_nr = 1; |
182 |
|
$part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#); |
183 |
|
|
184 |
|
my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename"; |
185 |
|
|
186 |
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
187 |
binmode($fh); |
binmode($fh); |
188 |
my $tar = Archive::Tar::Streamed->new($fh); |
my $tar = Archive::Tar::Streamed->new($fh); |
189 |
|
|
190 |
my $total_size = 0; |
my $tar_size = 0; |
191 |
|
my $items = 0; |
192 |
|
|
193 |
while(my $entry = $tar->next) { |
while(my $entry = $tar->next) { |
194 |
push @tar_files, $entry->name; |
push @tar_files, $entry->name; |
195 |
$total_size += $entry->size; |
$items++; |
196 |
|
$tar_size += $entry->size; |
197 |
} |
} |
198 |
|
|
199 |
if ($total_size > $Conf{MaxArchiveFileSize}) { |
if ($tar_size > $Conf{MaxArchiveFileSize}) { |
200 |
print STDERR " part too big $total_size > $Conf{MaxArchiveFileSize} }}" if ($debug); |
print STDERR " part too big $tar_size > $Conf{MaxArchiveFileSize} }}" if ($debug); |
201 |
$same = 0; |
$same = 0; |
202 |
last; |
last; |
203 |
} elsif ($total_size > $Conf{MaxArchiveSize}) { |
} elsif ($size > $Conf{MaxArchiveSize}) { |
204 |
print STDERR " part bigger than media $total_size > $Conf{MaxArchiveSize} }}" if ($debug); |
print STDERR " part bigger than media $size > $Conf{MaxArchiveSize} }}" if ($debug); |
205 |
$same = 0; |
$same = 0; |
206 |
last; |
last; |
207 |
} |
} |
208 |
|
|
209 |
|
check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items); |
210 |
} |
} |
211 |
|
|
212 |
# short-cut and exit; |
# short-cut and exit; |