17 |
my $debug = 0; |
my $debug = 0; |
18 |
$|=1; |
$|=1; |
19 |
|
|
20 |
|
my $start_t = time(); |
21 |
|
|
22 |
my $pidfile = new File::Pid; |
my $pidfile = new File::Pid; |
23 |
|
|
24 |
if (my $pid = $pidfile->running ) { |
if (my $pid = $pidfile->running ) { |
38 |
my $TopDir = $bpc->TopDir(); |
my $TopDir = $bpc->TopDir(); |
39 |
my $beenThere = {}; |
my $beenThere = {}; |
40 |
|
|
41 |
my $dsn = "dbi:SQLite:dbname=$TopDir/$Conf{SearchDB}"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
42 |
my $user = ''; |
my $user = $Conf{SearchUser} || ''; |
|
|
|
|
# DEBUG option! |
|
|
($dsn,$user) = qw/dbi:Pg:dbname=backuppc dpavlin/; |
|
43 |
|
|
44 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
45 |
|
|
65 |
my $index = shift || return; |
my $index = shift || return; |
66 |
my ($table,$col,$unique) = split(/_/, $index); |
my ($table,$col,$unique) = split(/_/, $index); |
67 |
$unique ||= ''; |
$unique ||= ''; |
68 |
|
$index =~ s/,/_/g; |
69 |
$dbh->do(qq{ create $unique index $index on $table($col) }); |
$dbh->do(qq{ create $unique index $index on $table($col) }); |
70 |
} |
} |
71 |
|
|
95 |
num INTEGER NOT NULL, |
num INTEGER NOT NULL, |
96 |
date integer NOT NULL, |
date integer NOT NULL, |
97 |
type CHAR(4) not null, |
type CHAR(4) not null, |
98 |
PRIMARY KEY(hostID, num) |
shareID integer not null references shares(id), |
99 |
|
PRIMARY KEY(hostID, num, shareID) |
100 |
); |
); |
101 |
}); |
}); |
102 |
|
|
103 |
do_index('backups_num_unique'); |
#do_index('backups_hostid,num_unique'); |
104 |
|
|
105 |
$dbh->do(qq{ |
$dbh->do(qq{ |
106 |
create table dvds ( |
create table dvds ( |
115 |
create table files ( |
create table files ( |
116 |
ID SERIAL PRIMARY KEY, |
ID SERIAL PRIMARY KEY, |
117 |
shareID INTEGER NOT NULL references shares(id), |
shareID INTEGER NOT NULL references shares(id), |
118 |
backupNum INTEGER NOT NULL references backups(num), |
backupNum INTEGER NOT NULL, |
119 |
name VARCHAR(255) NOT NULL, |
name VARCHAR(255) NOT NULL, |
120 |
path VARCHAR(255) NOT NULL, |
path VARCHAR(255) NOT NULL, |
|
fullpath VARCHAR(255) NOT NULL, |
|
121 |
date integer NOT NULL, |
date integer NOT NULL, |
122 |
type INTEGER NOT NULL, |
type INTEGER NOT NULL, |
123 |
size INTEGER NOT NULL, |
size INTEGER NOT NULL, |
156 |
} |
} |
157 |
print " done...\n"; |
print " done...\n"; |
158 |
|
|
159 |
eval { $dbh->commit; }; |
$dbh->commit; |
160 |
} |
} |
161 |
|
|
162 |
if ($opt{v}) { |
if ($opt{v}) { |
181 |
SELECT ID FROM hosts WHERE name=? |
SELECT ID FROM hosts WHERE name=? |
182 |
}); |
}); |
183 |
|
|
184 |
$sth->{backups_broj} = $dbh->prepare(qq{ |
$sth->{backups_count} = $dbh->prepare(qq{ |
185 |
SELECT COUNT(*) |
SELECT COUNT(*) |
186 |
FROM backups |
FROM backups |
187 |
WHERE hostID=? AND num=? |
WHERE hostID=? AND num=? AND shareid=? |
188 |
}); |
}); |
189 |
|
|
190 |
$sth->{insert_backups} = $dbh->prepare(qq{ |
$sth->{insert_backups} = $dbh->prepare(qq{ |
191 |
INSERT INTO backups (hostID, num, date, type) |
INSERT INTO backups (hostID, num, date, type, shareid) |
192 |
VALUES (?,?,?,?) |
VALUES (?,?,?,?,?) |
193 |
}); |
}); |
194 |
|
|
195 |
$sth->{insert_files} = $dbh->prepare(qq{ |
$sth->{insert_files} = $dbh->prepare(qq{ |
196 |
INSERT INTO files |
INSERT INTO files |
197 |
(shareID, backupNum, name, path, fullpath, date, type, size) |
(shareID, backupNum, name, path, date, type, size) |
198 |
VALUES (?,?,?,?,?,?,?,?) |
VALUES (?,?,?,?,?,?,?) |
199 |
}); |
}); |
200 |
|
|
201 |
|
sub fmt_time { |
202 |
|
my $t = shift || return; |
203 |
|
my $out = ""; |
204 |
|
my ($ss,$mm,$hh) = gmtime($t); |
205 |
|
$out .= "${hh}h" if ($hh); |
206 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
207 |
|
return $out; |
208 |
|
} |
209 |
|
|
210 |
foreach my $host_key (keys %{$hosts}) { |
foreach my $host_key (keys %{$hosts}) { |
211 |
|
|
212 |
my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key"; |
my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key"; |
241 |
print $hosts->{$host_key}->{'host'}, |
print $hosts->{$host_key}->{'host'}, |
242 |
"\t#$backupNum\t", $backup->{type} || '?', " ", |
"\t#$backupNum\t", $backup->{type} || '?', " ", |
243 |
$backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?', |
$backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?', |
244 |
" files\n"; |
" files (date: ", |
245 |
|
strftime($t_fmt,localtime($backup->{startTime})), |
246 |
$sth->{backups_broj}->execute($hostID, $backupNum); |
" dur: ", |
247 |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
fmt_time($backup->{endTime} - $backup->{startTime}), |
248 |
next if ($broj > 0); |
")\n"; |
|
|
|
|
$sth->{insert_backups}->execute( |
|
|
$hostID, |
|
|
$backupNum, |
|
|
$backup->{'endTime'}, |
|
|
$backup->{'type'} |
|
|
); |
|
|
$dbh->commit(); |
|
249 |
|
|
250 |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
251 |
foreach my $share ($files->shareList($backupNum)) { |
foreach my $share ($files->shareList($backupNum)) { |
252 |
|
|
253 |
my $t = time(); |
my $t = time(); |
254 |
|
|
|
print strftime($t_fmt,localtime())," ", $share; |
|
255 |
$shareID = getShareID($share, $hostID, $hostname); |
$shareID = getShareID($share, $hostID, $hostname); |
256 |
|
|
257 |
|
$sth->{backups_count}->execute($hostID, $backupNum, $shareID); |
258 |
|
my ($count) = $sth->{backups_count}->fetchrow_array(); |
259 |
|
# skip if allready in database! |
260 |
|
next if ($count > 0); |
261 |
|
|
262 |
|
# dump some log |
263 |
|
print strftime($t_fmt,localtime())," ", $share; |
264 |
|
|
265 |
my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
266 |
printf(" %d/%d files %d/%d dirs [%.2f/s]\n", |
|
267 |
$nf, $f, $nd, $d, |
$sth->{insert_backups}->execute( |
268 |
( ($f+$d) / ((time() - $t) || 1) ) |
$hostID, |
269 |
|
$backupNum, |
270 |
|
$backup->{'endTime'}, |
271 |
|
$backup->{'type'}, |
272 |
|
$shareID |
273 |
); |
); |
274 |
|
|
275 |
|
print " commit"; |
276 |
$dbh->commit(); |
$dbh->commit(); |
277 |
|
|
278 |
|
my $dur = (time() - $t) || 1; |
279 |
|
printf(" %d/%d files %d/%d dirs [%.2f/s dur: %s]\n", |
280 |
|
$nf, $f, $nd, $d, |
281 |
|
( ($f+$d) / $dur ), |
282 |
|
fmt_time($dur) |
283 |
|
); |
284 |
} |
} |
285 |
|
|
286 |
} |
} |
289 |
$dbh->commit(); |
$dbh->commit(); |
290 |
$dbh->disconnect(); |
$dbh->disconnect(); |
291 |
|
|
292 |
|
print "total duration: ",fmt_time(time() - $start_t),"\n"; |
293 |
|
|
294 |
$pidfile->remove; |
$pidfile->remove; |
295 |
|
|
296 |
sub getShareID() { |
sub getShareID() { |
374 |
$backupNum, |
$backupNum, |
375 |
$path_key, |
$path_key, |
376 |
$filesInBackup->{$path_key}->{'relPath'}, |
$filesInBackup->{$path_key}->{'relPath'}, |
|
$filesInBackup->{$path_key}->{'fullPath'}, |
|
|
# $filesInBackup->{$path_key}->{'sharePathM'}, |
|
377 |
$filesInBackup->{$path_key}->{'mtime'}, |
$filesInBackup->{$path_key}->{'mtime'}, |
378 |
$filesInBackup->{$path_key}->{'type'}, |
$filesInBackup->{$path_key}->{'type'}, |
379 |
$filesInBackup->{$path_key}->{'size'} |
$filesInBackup->{$path_key}->{'size'} |