1 |
dpavlin |
193 |
#!/usr/bin/perl |
2 |
dpavlin |
174 |
|
3 |
|
|
use strict; |
4 |
dpavlin |
193 |
use lib "__INSTALLDIR__/lib"; |
5 |
dpavlin |
174 |
|
6 |
|
|
use DBI; |
7 |
|
|
use BackupPC::Lib; |
8 |
|
|
use BackupPC::SearchLib; |
9 |
|
|
use Time::HiRes qw/time/; |
10 |
|
|
use POSIX qw/strftime/; |
11 |
|
|
use Term::Menus; |
12 |
dpavlin |
199 |
use File::Which; |
13 |
dpavlin |
174 |
|
14 |
|
|
use Data::Dumper; |
15 |
|
|
|
16 |
|
|
my $debug = 0; |
17 |
|
|
$|=1; |
18 |
|
|
|
19 |
dpavlin |
199 |
my $cdr_opts = 'dev=/dev/hdc blank=fast -v -eject'; |
20 |
|
|
|
21 |
|
|
my $bin; |
22 |
|
|
foreach my $c (qw/mkisofs cdrecord eject/) { |
23 |
|
|
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
24 |
|
|
} |
25 |
|
|
|
26 |
dpavlin |
174 |
my $start_t = time(); |
27 |
|
|
|
28 |
|
|
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
29 |
|
|
|
30 |
|
|
# don't check for user |
31 |
|
|
my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; |
32 |
|
|
my %Conf = $bpc->Conf(); |
33 |
|
|
%BackupPC::SearchLib::Conf = %Conf; |
34 |
|
|
|
35 |
|
|
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
36 |
|
|
my $user = $Conf{SearchUser} || ''; |
37 |
|
|
|
38 |
|
|
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 }); |
39 |
|
|
|
40 |
|
|
my $tar_dir = $Conf{InstallDir}.'/'; |
41 |
|
|
$tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration"; |
42 |
|
|
|
43 |
dpavlin |
176 |
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir); |
44 |
dpavlin |
174 |
|
45 |
|
|
my $iso_dir = $Conf{InstallDir}.'/'; |
46 |
|
|
$iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration"; |
47 |
|
|
die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir); |
48 |
|
|
|
49 |
|
|
#---- subs ---- |
50 |
|
|
|
51 |
|
|
sub fmt_time { |
52 |
|
|
my $t = shift || return; |
53 |
|
|
my $out = ""; |
54 |
|
|
my ($ss,$mm,$hh) = gmtime($t); |
55 |
|
|
$out .= "${hh}h" if ($hh); |
56 |
|
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
57 |
|
|
return $out; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
sub curr_time { |
61 |
|
|
return strftime($t_fmt,localtime()); |
62 |
|
|
} |
63 |
|
|
|
64 |
dpavlin |
188 |
sub dumpArchive2XML($$$) |
65 |
|
|
{ |
66 |
|
|
my ($dbh, $dvd_nr, $filename) = @_; |
67 |
|
|
my %archive; |
68 |
|
|
my $output = new IO::File(">$filename"); |
69 |
|
|
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
70 |
|
|
|
71 |
dpavlin |
189 |
$writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"'); |
72 |
|
|
|
73 |
dpavlin |
188 |
my $files_sql = q{ |
74 |
|
|
SELECT |
75 |
|
|
files.path AS path, |
76 |
|
|
files.date AS date, |
77 |
|
|
files.type AS type, |
78 |
|
|
files.size AS size |
79 |
|
|
FROM files, backups, shares |
80 |
|
|
WHERE files.backupnum=backups.num AND |
81 |
|
|
files.shareid=shares.id AND |
82 |
|
|
shares.hostid=backups.hostid AND |
83 |
|
|
backups.id=? |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
|
my $backups_sql = q{ |
87 |
|
|
SELECT |
88 |
|
|
backups.id AS backup_id, |
89 |
|
|
hosts.name AS host, |
90 |
|
|
backups.num AS num, |
91 |
|
|
backups.date AS date, |
92 |
|
|
shares.name AS share, |
93 |
|
|
backups.size AS backup_size, |
94 |
|
|
backups.inc_size AS compress_size |
95 |
|
|
FROM backups, archive_backup, hosts, shares |
96 |
|
|
WHERE archive_backup.backup_id = backups.id |
97 |
|
|
AND hosts.id=backups.hostid |
98 |
|
|
AND shares.id=backups.shareid |
99 |
|
|
AND archive_backup.archive_id = ? |
100 |
|
|
}; |
101 |
|
|
|
102 |
|
|
my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?"); |
103 |
|
|
$sth->execute($dvd_nr); |
104 |
|
|
my $row = $sth->fetchrow_hashref(); |
105 |
|
|
|
106 |
|
|
my $row_h; |
107 |
|
|
foreach my $hide (qw/id note/) { |
108 |
dpavlin |
199 |
$row_h->{$hide} = $row->{$hide} || ''; |
109 |
dpavlin |
188 |
delete $row->{$hide}; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
$writer->startTag("archive", %{$row}); |
113 |
|
|
|
114 |
|
|
$writer->startTag("note"); |
115 |
|
|
$writer->characters( $row_h->{'note'} ); |
116 |
|
|
$writer->endTag("note"); |
117 |
|
|
|
118 |
|
|
my $sth_backups = $dbh->prepare( $backups_sql ); |
119 |
|
|
$sth_backups->execute( $row_h->{'id'} ); |
120 |
|
|
|
121 |
|
|
while (my $row = $sth_backups->fetchrow_hashref()) { |
122 |
|
|
|
123 |
|
|
my $sth_files = $dbh->prepare( $files_sql); |
124 |
|
|
$sth_files->execute($row->{'backup_id'}); |
125 |
|
|
|
126 |
|
|
delete($row->{'backup_id'}); |
127 |
dpavlin |
189 |
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
128 |
dpavlin |
188 |
|
129 |
|
|
$writer->startTag( "backup", %{$row} ); |
130 |
|
|
|
131 |
|
|
while (my $row = $sth_files->fetchrow_hashref()) { |
132 |
|
|
# convert filetype to string |
133 |
|
|
$row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'}); |
134 |
dpavlin |
189 |
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
135 |
dpavlin |
188 |
my $path = $row->{'path'}; delete $row->{'path'}; |
136 |
|
|
|
137 |
|
|
$writer->startTag("file", %{$row}); |
138 |
|
|
$writer->characters( $path ); |
139 |
|
|
$writer->endTag("file"); |
140 |
|
|
} |
141 |
|
|
$writer->endTag("backup"); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
$writer->endTag("archive"); |
145 |
|
|
$writer->end(); |
146 |
|
|
|
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
dpavlin |
174 |
#----- main |
152 |
|
|
|
153 |
|
|
my $sth = $dbh->prepare( qq{ |
154 |
|
|
select |
155 |
|
|
id,dvd_nr,total_size,note,username, |
156 |
|
|
archive.date as date, |
157 |
|
|
count(archive_burned.archive_id) as copies |
158 |
|
|
from archive |
159 |
|
|
left outer join archive_burned on archive.id=archive_burned.archive_id |
160 |
|
|
group by id,dvd_nr,total_size,note,username,archive.date |
161 |
|
|
order by date asc |
162 |
|
|
} ); |
163 |
|
|
|
164 |
|
|
$sth->execute(); |
165 |
|
|
|
166 |
|
|
sub fmt_archive($) { |
167 |
|
|
my $row = shift || die; |
168 |
|
|
|
169 |
|
|
$row->{'date'} =~ s/\.\d+$//; |
170 |
|
|
$row->{'copies'} =~ s/^\s*0+\s*$/no/; |
171 |
|
|
$row->{'total_size'} /= (1024*1024); |
172 |
|
|
|
173 |
|
|
my $copies = 'copies'; |
174 |
|
|
$copies = 'copy' if ($row->{'copies'} == 1); |
175 |
|
|
|
176 |
|
|
return |
177 |
|
|
sprintf("%d by %s on %s, %s %s [%.2f Mb]", |
178 |
|
|
$row->{'dvd_nr'}, |
179 |
|
|
$row->{'username'}, |
180 |
|
|
$row->{'date'}, |
181 |
|
|
$row->{'copies'}, $copies, |
182 |
|
|
$row->{'total_size'}, |
183 |
|
|
); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
my @burned; |
187 |
|
|
my @not_burned; |
188 |
|
|
|
189 |
|
|
while (my $row = $sth->fetchrow_hashref) { |
190 |
|
|
if ($row->{'copies'}) { |
191 |
|
|
push @burned, fmt_archive($row); |
192 |
|
|
} else { |
193 |
|
|
push @not_burned, fmt_archive($row); |
194 |
|
|
} |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
my %Menu_archive = ( |
198 |
|
|
Label => 'Menu_archive', |
199 |
|
|
|
200 |
|
|
Item_1 => { |
201 |
|
|
Text => 'DVD #]Convey[', |
202 |
|
|
Convey => [ @not_burned ], |
203 |
|
|
Default => '*', |
204 |
|
|
}, |
205 |
|
|
Item_2 => { |
206 |
|
|
Text => 'DVD #]Convey[', |
207 |
|
|
Convey => [ @burned ], |
208 |
|
|
}, |
209 |
|
|
|
210 |
|
|
Select => 'Many', |
211 |
|
|
|
212 |
|
|
Banner => "Select one or more DVD media for burning", |
213 |
|
|
); |
214 |
|
|
|
215 |
|
|
my @archives_to_burn = Menu(\%Menu_archive); |
216 |
|
|
|
217 |
|
|
print "\n"; |
218 |
|
|
|
219 |
dpavlin |
175 |
sub skip($) { |
220 |
|
|
my $msg = shift; |
221 |
|
|
print "WARNING: $msg, skipping...\n"; |
222 |
|
|
goto SKIP; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
my $sth_archive_backup = $dbh->prepare( qq{ |
226 |
|
|
select |
227 |
|
|
backup_id, |
228 |
dpavlin |
176 |
archive_id, |
229 |
dpavlin |
175 |
hosts.name as host, |
230 |
|
|
shares.name as share, |
231 |
dpavlin |
199 |
backups.num as num, |
232 |
|
|
backups.parts as parts |
233 |
dpavlin |
175 |
from archive_backup |
234 |
|
|
join archive on archive_id = archive.id |
235 |
|
|
join backups on backup_id = backups.id |
236 |
|
|
join hosts on hostid = hosts.id |
237 |
|
|
join shares on shareid = shares.id |
238 |
|
|
where archive.dvd_nr = ? |
239 |
|
|
}); |
240 |
|
|
|
241 |
dpavlin |
176 |
my $sth_archive_burned = $dbh->prepare( qq{ |
242 |
|
|
insert into archive_burned |
243 |
dpavlin |
199 |
(archive_id, iso_size, part) |
244 |
|
|
values ( (select id from archive where dvd_nr =?), ?, ?) |
245 |
dpavlin |
176 |
}); |
246 |
|
|
|
247 |
dpavlin |
174 |
foreach my $arc (@archives_to_burn) { |
248 |
|
|
exit if ($arc eq ']quit['); |
249 |
|
|
|
250 |
|
|
my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/); |
251 |
|
|
die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr); |
252 |
|
|
|
253 |
dpavlin |
175 |
my $tmp_dir = "/$iso_dir/$dvd_nr"; |
254 |
dpavlin |
174 |
|
255 |
dpavlin |
175 |
my $t = time(); |
256 |
|
|
|
257 |
|
|
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
258 |
|
|
|
259 |
dpavlin |
199 |
my $part_path = ''; |
260 |
|
|
my $part_nr = 1; |
261 |
|
|
my $parts = 0; |
262 |
|
|
|
263 |
|
|
$sth_archive_backup->execute($dvd_nr); |
264 |
dpavlin |
175 |
|
265 |
dpavlin |
199 |
my $disk_name = 'unknown disk'; |
266 |
|
|
my $iso_size = 0; |
267 |
dpavlin |
175 |
|
268 |
dpavlin |
199 |
do { |
269 |
dpavlin |
175 |
|
270 |
dpavlin |
199 |
# do we need to re-execute query for multi-part increments? |
271 |
|
|
$sth_archive_backup->execute($dvd_nr) if ($parts > 1); |
272 |
dpavlin |
175 |
|
273 |
dpavlin |
199 |
my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row"; |
274 |
dpavlin |
175 |
|
275 |
|
|
|
276 |
dpavlin |
199 |
$parts = $row->{'parts'} || die "no parts?"; |
277 |
|
|
$disk_name = $dvd_nr; |
278 |
dpavlin |
175 |
|
279 |
dpavlin |
199 |
if ($parts > 1) { |
280 |
|
|
# parts start with 0, so we have -1 here |
281 |
|
|
$part_path = '.' . ($part_nr - 1); |
282 |
|
|
$disk_name .= '.' . $part_nr; |
283 |
|
|
} |
284 |
dpavlin |
188 |
|
285 |
dpavlin |
199 |
print "Processing part $part_nr/$parts\n"; |
286 |
dpavlin |
189 |
|
287 |
dpavlin |
199 |
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
288 |
|
|
$list_file .= '.list'; |
289 |
|
|
$iso_file .= $part_path . '.iso'; |
290 |
|
|
$xml_file .= '.xml'; |
291 |
dpavlin |
181 |
|
292 |
dpavlin |
199 |
# |
293 |
|
|
# check if ISO file exists |
294 |
|
|
# |
295 |
dpavlin |
175 |
|
296 |
dpavlin |
199 |
if (! -e $iso_file) { |
297 |
dpavlin |
176 |
|
298 |
dpavlin |
199 |
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
299 |
dpavlin |
176 |
|
300 |
dpavlin |
199 |
my $inc = 0; |
301 |
dpavlin |
176 |
|
302 |
dpavlin |
199 |
do { |
303 |
|
|
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
304 |
|
|
if (-f "$tar_dir/$tar_file") { |
305 |
|
|
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
306 |
|
|
print $list "$tar_dir/$tar_file\n"; |
307 |
|
|
} else { |
308 |
|
|
my $len = length("$parts"); |
309 |
|
|
my $nr = sprintf("%0${len}d", $part_nr); |
310 |
|
|
print $list "$tar_dir/$tar_file/part$nr\n"; |
311 |
|
|
} |
312 |
|
|
$inc++; |
313 |
|
|
} while ($row = $sth_archive_backup->fetchrow_hashref); |
314 |
dpavlin |
176 |
|
315 |
dpavlin |
199 |
# add file list and note in xml |
316 |
|
|
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
317 |
|
|
print $list "$xml_file\n"; |
318 |
dpavlin |
176 |
|
319 |
dpavlin |
199 |
# add css file for archive |
320 |
|
|
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
321 |
|
|
if (-r $css_file) { |
322 |
|
|
print $list "$css_file\n"; |
323 |
|
|
} else { |
324 |
|
|
print "WARNING: missing $css_file, not added to iso image!\n"; |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
close($list); |
328 |
|
|
|
329 |
|
|
print "Running mkisofs now for $inc increments, disk $disk_name\n"; |
330 |
|
|
|
331 |
|
|
my $cmd = $bin->{'mkisofs'} . qq{ -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $disk_name -o $iso_file -path-list $list_file }; |
332 |
|
|
|
333 |
|
|
system($cmd) == 0 or skip "can't run $cmd: $?"; |
334 |
|
|
|
335 |
|
|
$iso_size = (stat($iso_file))[7]; |
336 |
|
|
|
337 |
|
|
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
338 |
|
|
|
339 |
|
|
} else { |
340 |
|
|
print "ISO $iso_file allready exists\n"; |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
print "\nREADY TO BURN MEDIA $disk_name please insert blank media and press ENTER\n\n"; |
344 |
|
|
|
345 |
|
|
system($bin->{'eject'}) == 0 or skip "can't run eject: $?"; |
346 |
|
|
|
347 |
|
|
my $wait = <STDIN>; |
348 |
|
|
|
349 |
|
|
my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file; |
350 |
|
|
|
351 |
|
|
# system($cmd) == 0 or skip "can't run $cmd: $?"; |
352 |
|
|
print "## $cmd\n"; |
353 |
|
|
|
354 |
|
|
print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; |
355 |
|
|
|
356 |
|
|
$sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr); |
357 |
|
|
|
358 |
|
|
print "Media burn for $disk_name recorded\n"; |
359 |
|
|
|
360 |
|
|
$part_nr++; |
361 |
|
|
} until ($part_nr > $parts); |
362 |
|
|
|
363 |
dpavlin |
175 |
SKIP: |
364 |
|
|
|
365 |
dpavlin |
174 |
} |
366 |
|
|
|
367 |
|
|
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
368 |
|
|
#print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file "; |
369 |
|
|
|
370 |
dpavlin |
199 |
print "Recoding finished, exiting...\n"; |
371 |
|
|
|
372 |
dpavlin |
174 |
$sth->finish; |
373 |
dpavlin |
199 |
$sth_archive_backup->finish; |
374 |
|
|
$sth_archive_burned->finish; |
375 |
|
|
|
376 |
dpavlin |
174 |
$dbh->disconnect; |
377 |
|
|
|