4 use lib "__INSTALLDIR__/lib";
8 use BackupPC::SearchLib;
9 use Time::HiRes qw/time/;
10 use POSIX qw/strftime/;
19 # don't check for user
20 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
21 my %Conf = $bpc->Conf();
22 %BackupPC::SearchLib::Conf = %Conf;
24 my $cdrecord = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
25 my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
29 foreach my $c (qw/mkisofs eject/, $cdrecord) {
30 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
35 my $t_fmt = '%Y-%m-%d %H:%M:%S';
38 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
39 my $user = $Conf{SearchUser} || '';
41 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
43 my $tar_dir = $Conf{InstallDir}.'/';
44 $tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration";
46 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir);
48 my $iso_dir = $Conf{InstallDir}.'/';
49 $iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration";
50 die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir);
55 my $t = shift || return;
57 my ($ss,$mm,$hh) = gmtime($t);
58 $out .= "${hh}h" if ($hh);
59 $out .= sprintf("%02d:%02d", $mm,$ss);
64 return strftime($t_fmt,localtime());
67 sub dumpArchive2XML($$$)
69 my ($dbh, $dvd_nr, $filename) = @_;
71 my $output = new IO::File(">$filename");
72 my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
74 print "Dumping file list for DVD $dvd_nr";
76 $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
84 FROM files, backups, shares
85 WHERE files.backupnum=backups.num AND
86 files.shareid=shares.id AND
87 shares.hostid=backups.hostid AND
93 backups.id AS backup_id,
98 backups.size AS backup_size,
99 backups.inc_size AS compress_size
100 FROM backups, archive_backup, hosts, shares
101 WHERE archive_backup.backup_id = backups.id
102 AND hosts.id=backups.hostid
103 AND shares.id=backups.shareid
104 AND archive_backup.archive_id = ?
107 my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
108 $sth->execute($dvd_nr);
109 my $row = $sth->fetchrow_hashref();
112 foreach my $hide (qw/id note/) {
113 $row_h->{$hide} = $row->{$hide} || '';
114 delete $row->{$hide};
117 $writer->startTag("archive", %{$row});
119 $writer->startTag("note");
120 $writer->characters( $row_h->{'note'} );
121 $writer->endTag("note");
123 my $sth_backups = $dbh->prepare( $backups_sql );
124 $sth_backups->execute( $row_h->{'id'} );
126 while (my $row = $sth_backups->fetchrow_hashref()) {
130 my $sth_files = $dbh->prepare( $files_sql);
131 $sth_files->execute($row->{'backup_id'});
133 delete($row->{'backup_id'});
134 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
136 $writer->startTag( "backup", %{$row} );
138 while (my $row = $sth_files->fetchrow_hashref()) {
139 # convert filetype to string
140 $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
141 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
142 my $path = $row->{'path'}; delete $row->{'path'};
144 $writer->startTag("file", %{$row});
145 $writer->characters( $path );
146 $writer->endTag("file");
148 $writer->endTag("backup");
151 $writer->endTag("archive");
161 my $sth = $dbh->prepare( qq{
163 id,dvd_nr,total_size,note,username,
164 archive.date as date,
165 count(archive_burned.archive_id) as copies
167 left outer join archive_burned on archive.id=archive_burned.archive_id
168 group by id,dvd_nr,total_size,note,username,archive.date
175 my $row = shift || die;
177 $row->{'date'} =~ s/\.\d+$//;
178 $row->{'copies'} =~ s/^\s*0+\s*$/no/;
179 $row->{'total_size'} /= (1024*1024);
181 my $copies = 'copies';
182 $copies = 'copy' if ($row->{'copies'} == 1);
185 sprintf("%d by %s on %s, %s %s [%.2f Mb]",
189 $row->{'copies'}, $copies,
190 $row->{'total_size'},
197 while (my $row = $sth->fetchrow_hashref) {
198 if ($row->{'copies'}) {
199 push @burned, fmt_archive($row);
201 push @not_burned, fmt_archive($row);
206 Label => 'Menu_archive',
209 Text => 'DVD #]Convey[',
210 Convey => [ @not_burned ],
214 Text => 'DVD #]Convey[',
215 Convey => [ @burned ],
220 Banner => "Select one or more DVD media for burning",
223 my @archives_to_burn = Menu(\%Menu_archive);
229 print "WARNING: $msg, skipping...\n";
233 my $sth_archive_backup = $dbh->prepare( qq{
238 shares.name as share,
240 backups.parts as parts
242 join archive on archive_id = archive.id
243 join backups on backup_id = backups.id
244 join hosts on hostid = hosts.id
245 join shares on shareid = shares.id
246 where archive.dvd_nr = ?
249 my $sth_archive_burned = $dbh->prepare( qq{
250 insert into archive_burned
251 (archive_id, iso_size, part, copy)
252 values ( (select id from archive where dvd_nr =?), ?, ?, ?)
255 foreach my $arc (@archives_to_burn) {
256 exit if ($arc eq ']quit[');
258 my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
259 die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
261 my $tmp_dir = "/$iso_dir/$dvd_nr";
265 print "Working on DVD #$dvd_nr in $tmp_dir\n";
271 $sth_archive_backup->execute($dvd_nr);
273 my $disk_name = 'unknown disk';
278 # do we need to re-execute query for multi-part increments?
279 $sth_archive_backup->execute($dvd_nr) if ($parts > 1);
281 my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";
284 $parts = $row->{'parts'} || die "no parts?";
285 $disk_name = $dvd_nr;
288 # parts start with 0, so we have -1 here
289 $part_path = '.' . ($part_nr - 1);
290 $disk_name .= '.' . $part_nr;
293 print "Processing part $part_nr/$parts\n";
295 my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
296 $list_file .= $part_path . '.list';
297 $iso_file .= $part_path . '.iso';
301 # check if ISO file exists
304 if (! -e $iso_file) {
306 open(my $list, "> $list_file") || skip "can't open $list_file: $!";
311 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
312 if (-f "$tar_dir/$tar_file") {
313 skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
314 print $list "$tar_dir/$tar_file\n";
316 my $len = length("$parts");
317 my $nr = sprintf("%0${len}d", ($part_nr - 1));
318 print $list "$tar_dir/$tar_file/part$nr\n";
321 } while ($row = $sth_archive_backup->fetchrow_hashref);
323 # add file list and note in xml
324 dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
325 print $list "$xml_file\n";
327 # add css file for archive
328 my $css_file = $Conf{CgiImageDir} . '/archive.css';
330 print $list "$css_file\n";
332 print "WARNING: missing $css_file, not added to iso image!\n";
337 print "Running mkisofs now for $inc increments, disk $disk_name\n";
339 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 };
341 system($cmd) == 0 or skip "can't run $cmd: $?";
343 $iso_size = (stat($iso_file))[7];
345 print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
348 print "ISO $iso_file allready exists\n";
351 my $copies = $Conf{BurnMultipleCopies} || 1;
353 foreach my $copy_nr ( 1 .. $copies ) {
355 print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
357 system($bin->{'eject'}) == 0 or skip "can't run eject: $?";
361 my $cmd = $bin->{$cdrecord} . ' ' . $cdr_opts . ' ' . $iso_file;
365 system($cmd) == 0 or skip "can't run $cmd: $?";
367 print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
369 $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr);
371 print "Media burn for $disk_name copy $copy_nr recorded\n";
375 } until ($part_nr > $parts);
381 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
382 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
384 print "Recoding finished, exiting...\n";
387 $sth_archive_backup->finish;
388 $sth_archive_burned->finish;