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/; |
13 |
use BackupPC::SearchLib; |
use BackupPC::SearchLib; |
14 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
15 |
|
use File::Which; |
16 |
|
use Archive::Tar::Streamed; |
17 |
|
use Algorithm::Diff; |
18 |
|
use Getopt::Std; |
19 |
|
|
20 |
my $path = abs_path($0); |
my $path = abs_path($0); |
21 |
$path =~ s#/[^/]+$#/#; |
$path =~ s#/[^/]+$#/#; |
23 |
|
|
24 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
25 |
|
|
26 |
my $debug = 0; |
my $bin; |
27 |
|
foreach my $c (qw/gzip split/) { |
28 |
|
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
29 |
|
} |
30 |
|
|
31 |
|
my %opt; |
32 |
|
getopts("cd", \%opt ); |
33 |
|
|
34 |
|
my $debug = $opt{d}; |
35 |
|
my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n"; |
36 |
|
|
37 |
$|=1; |
$|=1; |
38 |
|
|
39 |
my $start_t = time(); |
my $start_t = time(); |
70 |
return strftime($t_fmt,localtime()); |
return strftime($t_fmt,localtime()); |
71 |
} |
} |
72 |
|
|
73 |
|
sub tar_join($) { |
74 |
|
my $filename = shift; |
75 |
|
|
76 |
|
my $in = my $out = $filename; |
77 |
|
$out .= '.tmp'; |
78 |
|
|
79 |
|
# FIXME I should really order parts manually! |
80 |
|
system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?"; |
81 |
|
|
82 |
|
} |
83 |
|
|
84 |
|
sub tar_check($$$$) { |
85 |
|
my ($host,$share,$num,$filename) = @_; |
86 |
|
|
87 |
|
if ($debug) { |
88 |
|
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
89 |
|
} else { |
90 |
|
print " check"; |
91 |
|
} |
92 |
|
|
93 |
|
if (-d $filename) { |
94 |
|
print STDERR ", joining"; |
95 |
|
tar_join($filename); |
96 |
|
} |
97 |
|
|
98 |
|
print STDERR ", opening" if ($debug); |
99 |
|
open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!"; |
100 |
|
binmode($fh); |
101 |
|
my $tar = Archive::Tar::Streamed->new($fh); |
102 |
|
|
103 |
|
print STDERR ", tar" if ($debug); |
104 |
|
my @tar_files; |
105 |
|
while(my $entry = $tar->next) { |
106 |
|
push @tar_files, $entry->name; |
107 |
|
} |
108 |
|
@tar_files = sort @tar_files; |
109 |
|
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
110 |
|
|
111 |
|
print STDERR ", database" if ($debug); |
112 |
|
|
113 |
|
my $sth = $dbh->prepare(qq{ |
114 |
|
SELECT path,type |
115 |
|
FROM files |
116 |
|
JOIN shares on shares.id = shareid |
117 |
|
JOIN hosts on hosts.id = shares.hostid |
118 |
|
WHERE hosts.name = ? and shares.name = ? and backupnum = ? |
119 |
|
}); |
120 |
|
$sth->execute($host, $share, $num); |
121 |
|
my @db_files; |
122 |
|
while( my $row = $sth->fetchrow_hashref ) { |
123 |
|
|
124 |
|
my $path = $row->{'path'} || die "no path?"; |
125 |
|
$path =~ s#^/#./#; |
126 |
|
$path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR); |
127 |
|
push @db_files, $path; |
128 |
|
} |
129 |
|
|
130 |
|
print STDERR " ",($#db_files + 1), " files, diff" if ($debug); |
131 |
|
|
132 |
|
@db_files = sort @db_files; |
133 |
|
|
134 |
|
my $same = 1; |
135 |
|
if ($#tar_files != $#db_files) { |
136 |
|
$same = 0; |
137 |
|
print STDERR " NUMBER" if ($debug); |
138 |
|
} else { |
139 |
|
my $diff = Algorithm::Diff->new(\@tar_files, \@db_files); |
140 |
|
while ( $diff->Next() ) { |
141 |
|
next if $diff->Same(); |
142 |
|
$same = 0; |
143 |
|
print "< $_\n" for $diff->Items(1); |
144 |
|
print "> $_\n" for $diff->Items(2); |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
print STDERR " ",($same ? 'ok' : 'DIFFERENT'); |
149 |
|
print STDERR " }} " if ($debug); |
150 |
|
|
151 |
|
return $same; |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
#----- main |
#----- main |
156 |
|
|
157 |
my $sth = $dbh->prepare( qq{ |
my $sth = $dbh->prepare( qq{ |
172 |
|
|
173 |
} ); |
} ); |
174 |
|
|
|
$sth->execute(); |
|
|
|
|
175 |
my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? }); |
my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? }); |
176 |
my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? }); |
my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? }); |
177 |
|
|
178 |
%BackupPC::SearchLib::Conf = %Conf; |
%BackupPC::SearchLib::Conf = %Conf; |
179 |
|
|
180 |
|
$sth->execute(); |
181 |
|
my $num_backups = $sth->rows; |
182 |
|
my $curr_backup = 1; |
183 |
|
|
184 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
185 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
186 |
|
|
187 |
# this will return -1 if file doesn't exist |
# this will return -1 if file doesn't exist |
188 |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
189 |
|
|
190 |
print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
191 |
|
$curr_backup++; |
192 |
|
|
193 |
my $t = time(); |
my $t = time(); |
194 |
|
|
195 |
# re-create archive? |
# re-create archive? |
196 |
if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) { |
if ($row->{'inc_size'} == -1 || $size == -1 || |
197 |
my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file}; |
$row->{'inc_size'} != $size || |
198 |
|
$check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file") |
199 |
|
) { |
200 |
|
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}; |
201 |
print STDERR "## $cmd\n" if ($debug); |
print STDERR "## $cmd\n" if ($debug); |
202 |
|
|
203 |
system($cmd) == 0 or die "failed: $?"; |
system($cmd) == 0 or die "failed: $?"; |
204 |
|
|
205 |
|
rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!"; |
206 |
|
|
207 |
$size = (stat( "$tar_dir/$tar_file" ))[7]; |
$size = (stat( "$tar_dir/$tar_file" ))[7]; |
208 |
} |
} |
209 |
|
|
216 |
|
|
217 |
if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) { |
if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) { |
218 |
print " join"; |
print " join"; |
219 |
|
tar_join("$tar_dir/$tar_file"); |
|
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: $?"; |
|
220 |
} |
} |
221 |
|
|
222 |
if ($size > $max_size && ! -d "$tar_dir/$tar_file") { |
if ($size > $max_size && ! -d "$tar_dir/$tar_file") { |
227 |
mkdir $in || die "can't mkdir $in: $!"; |
mkdir $in || die "can't mkdir $in: $!"; |
228 |
|
|
229 |
my $suffix_len = length("$parts"); |
my $suffix_len = length("$parts"); |
230 |
system("split -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?"; |
system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?"; |
231 |
unlink $out || die "can't unlink $out: $!"; |
unlink $out || die "can't unlink $out: $!"; |
232 |
} |
} |
233 |
|
|