1 |
dpavlin |
157 |
#!/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 |
dpavlin |
214 |
use BackupPC::Attrib qw/:all/; |
10 |
dpavlin |
157 |
use Data::Dumper; |
11 |
|
|
use Time::HiRes qw/time/; |
12 |
|
|
use POSIX qw/strftime/; |
13 |
|
|
use Cwd qw/abs_path/; |
14 |
dpavlin |
201 |
use File::Which; |
15 |
dpavlin |
215 |
use Archive::Tar::Streamed; |
16 |
dpavlin |
214 |
use Algorithm::Diff; |
17 |
|
|
use Getopt::Std; |
18 |
dpavlin |
157 |
|
19 |
dpavlin |
230 |
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 |
dpavlin |
218 |
# cludge: minimum .tar.gz size |
26 |
|
|
my $MIN_TAR_SIZE = 80; |
27 |
|
|
|
28 |
dpavlin |
157 |
my $path = abs_path($0); |
29 |
|
|
$path =~ s#/[^/]+$#/#; |
30 |
|
|
my $tarIncCreate = $path .= 'BackupPC_tarIncCreate'; |
31 |
|
|
|
32 |
|
|
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
33 |
|
|
|
34 |
dpavlin |
201 |
my $bin; |
35 |
dpavlin |
253 |
foreach my $c (qw/gzip/) { |
36 |
dpavlin |
201 |
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
37 |
|
|
} |
38 |
|
|
|
39 |
dpavlin |
214 |
my %opt; |
40 |
|
|
getopts("cd", \%opt ); |
41 |
dpavlin |
201 |
|
42 |
dpavlin |
214 |
my $debug = $opt{d}; |
43 |
|
|
my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n"; |
44 |
|
|
|
45 |
dpavlin |
157 |
$|=1; |
46 |
|
|
|
47 |
|
|
my $start_t = time(); |
48 |
|
|
|
49 |
|
|
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
50 |
|
|
|
51 |
|
|
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
52 |
|
|
my $user = $Conf{SearchUser} || ''; |
53 |
|
|
|
54 |
|
|
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
55 |
|
|
|
56 |
|
|
my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}; |
57 |
|
|
|
58 |
|
|
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir); |
59 |
|
|
|
60 |
|
|
#---- subs ---- |
61 |
|
|
|
62 |
|
|
sub fmt_time { |
63 |
|
|
my $t = shift || return; |
64 |
|
|
my $out = ""; |
65 |
|
|
my ($ss,$mm,$hh) = gmtime($t); |
66 |
|
|
$out .= "${hh}h" if ($hh); |
67 |
|
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
68 |
|
|
return $out; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
sub curr_time { |
72 |
|
|
return strftime($t_fmt,localtime()); |
73 |
|
|
} |
74 |
|
|
|
75 |
dpavlin |
214 |
sub tar_check($$$$) { |
76 |
|
|
my ($host,$share,$num,$filename) = @_; |
77 |
|
|
|
78 |
dpavlin |
253 |
return 1; # FIXME |
79 |
|
|
|
80 |
dpavlin |
214 |
if ($debug) { |
81 |
|
|
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
82 |
|
|
} else { |
83 |
|
|
print " check"; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
if (-d $filename) { |
87 |
|
|
print STDERR ", joining"; |
88 |
|
|
tar_join($filename); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
print STDERR ", opening" if ($debug); |
92 |
dpavlin |
215 |
open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!"; |
93 |
|
|
binmode($fh); |
94 |
|
|
my $tar = Archive::Tar::Streamed->new($fh); |
95 |
dpavlin |
214 |
|
96 |
|
|
print STDERR ", tar" if ($debug); |
97 |
dpavlin |
215 |
my @tar_files; |
98 |
|
|
while(my $entry = $tar->next) { |
99 |
|
|
push @tar_files, $entry->name; |
100 |
|
|
} |
101 |
|
|
@tar_files = sort @tar_files; |
102 |
dpavlin |
214 |
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
103 |
|
|
|
104 |
|
|
print STDERR ", database" if ($debug); |
105 |
|
|
|
106 |
|
|
my $sth = $dbh->prepare(qq{ |
107 |
|
|
SELECT path,type |
108 |
|
|
FROM files |
109 |
|
|
JOIN shares on shares.id = shareid |
110 |
|
|
JOIN hosts on hosts.id = shares.hostid |
111 |
|
|
WHERE hosts.name = ? and shares.name = ? and backupnum = ? |
112 |
|
|
}); |
113 |
|
|
$sth->execute($host, $share, $num); |
114 |
|
|
my @db_files; |
115 |
|
|
while( my $row = $sth->fetchrow_hashref ) { |
116 |
|
|
|
117 |
|
|
my $path = $row->{'path'} || die "no path?"; |
118 |
|
|
$path =~ s#^/#./#; |
119 |
|
|
$path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR); |
120 |
|
|
push @db_files, $path; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
print STDERR " ",($#db_files + 1), " files, diff" if ($debug); |
124 |
|
|
|
125 |
|
|
@db_files = sort @db_files; |
126 |
|
|
|
127 |
|
|
my $same = 1; |
128 |
|
|
if ($#tar_files != $#db_files) { |
129 |
|
|
$same = 0; |
130 |
|
|
print STDERR " NUMBER" if ($debug); |
131 |
|
|
} else { |
132 |
|
|
my $diff = Algorithm::Diff->new(\@tar_files, \@db_files); |
133 |
|
|
while ( $diff->Next() ) { |
134 |
|
|
next if $diff->Same(); |
135 |
|
|
$same = 0; |
136 |
|
|
print "< $_\n" for $diff->Items(1); |
137 |
|
|
print "> $_\n" for $diff->Items(2); |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
|
141 |
dpavlin |
216 |
print " ",($same ? 'ok' : 'DIFFERENT'); |
142 |
dpavlin |
214 |
print STDERR " }} " if ($debug); |
143 |
|
|
|
144 |
|
|
return $same; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
|
148 |
dpavlin |
157 |
#----- main |
149 |
|
|
|
150 |
|
|
my $sth = $dbh->prepare( qq{ |
151 |
|
|
|
152 |
|
|
select |
153 |
dpavlin |
158 |
backups.id as backup_id, |
154 |
dpavlin |
157 |
hosts.name as host, |
155 |
|
|
shares.name as share, |
156 |
dpavlin |
192 |
backups.num as num, |
157 |
dpavlin |
196 |
inc_size, |
158 |
|
|
parts |
159 |
dpavlin |
157 |
from backups |
160 |
|
|
join shares on backups.hostid = shares.hostid |
161 |
|
|
and shares.id = backups.shareid |
162 |
|
|
join hosts on shares.hostid = hosts.id |
163 |
dpavlin |
192 |
where not inc_deleted |
164 |
dpavlin |
157 |
order by backups.date |
165 |
|
|
|
166 |
|
|
} ); |
167 |
|
|
|
168 |
dpavlin |
213 |
$sth->execute(); |
169 |
|
|
my $num_backups = $sth->rows; |
170 |
|
|
my $curr_backup = 1; |
171 |
|
|
|
172 |
dpavlin |
157 |
while (my $row = $sth->fetchrow_hashref) { |
173 |
|
|
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
174 |
dpavlin |
192 |
|
175 |
dpavlin |
194 |
# this will return -1 if file doesn't exist |
176 |
|
|
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
177 |
dpavlin |
192 |
|
178 |
dpavlin |
253 |
print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d}); |
179 |
|
|
|
180 |
|
|
if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size && ( $check && tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file") || 1) ) { |
181 |
|
|
next; |
182 |
|
|
} |
183 |
|
|
|
184 |
dpavlin |
213 |
print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
185 |
|
|
$curr_backup++; |
186 |
dpavlin |
157 |
|
187 |
|
|
my $t = time(); |
188 |
|
|
|
189 |
dpavlin |
194 |
# re-create archive? |
190 |
dpavlin |
253 |
my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} }; |
191 |
|
|
print STDERR "## $cmd\n" if ($debug); |
192 |
dpavlin |
157 |
|
193 |
dpavlin |
253 |
if (system($cmd) != 0) { |
194 |
|
|
print STDERR " FAILED"; |
195 |
dpavlin |
194 |
} |
196 |
dpavlin |
157 |
|
197 |
dpavlin |
194 |
print ", dur: ",fmt_time(time() - $t), "\n"; |
198 |
dpavlin |
158 |
|
199 |
|
|
$dbh->commit; |
200 |
|
|
|
201 |
dpavlin |
157 |
} |
202 |
|
|
|
203 |
|
|
undef $sth; |
204 |
|
|
$dbh->disconnect; |