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) = @_;
72 my $output = new IO::File('> '.$filename.'.tmp');
73 my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
75 print "Dumping file list for DVD $dvd_nr, countdown:";
77 $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
85 FROM files, backups, shares
86 WHERE files.backupnum=backups.num AND
87 files.shareid=shares.id AND
88 shares.hostid=backups.hostid AND
94 backups.id AS backup_id,
99 backups.size AS backup_size,
100 backups.inc_size AS compress_size
101 FROM backups, archive_backup, hosts, shares
102 WHERE archive_backup.backup_id = backups.id
103 AND hosts.id=backups.hostid
104 AND shares.id=backups.shareid
105 AND archive_backup.archive_id = ?
108 my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
109 $sth->execute($dvd_nr);
110 my $row = $sth->fetchrow_hashref();
113 foreach my $hide (qw/id note/) {
114 $row_h->{$hide} = $row->{$hide} || '';
115 delete $row->{$hide};
118 $writer->startTag("archive", %{$row});
120 $writer->startTag("note");
121 $writer->characters( $row_h->{'note'} );
122 $writer->endTag("note");
124 my $sth_backups = $dbh->prepare( $backups_sql );
125 $sth_backups->execute( $row_h->{'id'} );
127 my $total_backups = $sth_backups->rows;
131 while (my $row = $sth_backups->fetchrow_hashref()) {
134 my $pcnt = int(($curr_backup * 10) / $total_backups);
135 if ($pcnt > $last_pcnt) {
136 print " ",(10 - $pcnt);
140 my $sth_files = $dbh->prepare( $files_sql);
141 $sth_files->execute($row->{'backup_id'});
143 delete($row->{'backup_id'});
144 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
146 $writer->startTag( "backup", %{$row} );
148 while (my $row = $sth_files->fetchrow_hashref()) {
149 # convert filetype to string
150 $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
151 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
152 my $path = $row->{'path'}; delete $row->{'path'};
154 $writer->startTag("file", %{$row});
155 $writer->characters( $path );
156 $writer->endTag("file");
158 $writer->endTag("backup");
161 $writer->endTag("archive");
164 rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
173 my $sth = $dbh->prepare( qq{
175 id,dvd_nr,total_size,note,username,
176 archive.date as date,
177 count(archive_burned.archive_id) as copies
179 left outer join archive_burned on archive.id=archive_burned.archive_id
180 group by id,dvd_nr,total_size,note,username,archive.date
187 my $row = shift || die;
189 $row->{'date'} =~ s/\.\d+$//;
190 $row->{'copies'} =~ s/^\s*0+\s*$/no/;
191 $row->{'total_size'} /= (1024*1024);
193 my $copies = 'copies';
194 $copies = 'copy' if ($row->{'copies'} == 1);
197 sprintf("%d by %s on %s, %s %s [%.2f Mb]",
201 $row->{'copies'}, $copies,
202 $row->{'total_size'},
209 while (my $row = $sth->fetchrow_hashref) {
210 if ($row->{'copies'}) {
211 push @burned, fmt_archive($row);
213 push @not_burned, fmt_archive($row);
218 Label => 'Menu_archive',
221 Text => 'DVD #]Convey[',
222 Convey => [ @not_burned ],
226 Text => 'DVD #]Convey[',
227 Convey => [ @burned ],
232 Banner => "Select one or more DVD media for burning",
235 my @archives_to_burn = Menu(\%Menu_archive);
241 print "WARNING: $msg, skipping...\n";
245 my $sth_archive_backup = $dbh->prepare( qq{
250 shares.name as share,
252 backups.parts as parts
254 join archive on archive_id = archive.id
255 join backups on backup_id = backups.id
256 join hosts on hostid = hosts.id
257 join shares on shareid = shares.id
258 where archive.dvd_nr = ?
261 my $sth_archive_burned = $dbh->prepare( qq{
262 insert into archive_burned
263 (archive_id, iso_size, part, copy)
264 values ( (select id from archive where dvd_nr =?), ?, ?, ?)
267 foreach my $arc (@archives_to_burn) {
268 exit if ($arc eq ']quit[');
270 my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
271 die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
273 my $tmp_dir = "/$iso_dir/$dvd_nr";
277 print "Working on DVD #$dvd_nr in $tmp_dir\n";
283 $sth_archive_backup->execute($dvd_nr);
285 my $disk_name = 'unknown disk';
290 # do we need to re-execute query for multi-part increments?
291 $sth_archive_backup->execute($dvd_nr) if ($parts > 1);
293 my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";
296 $parts = $row->{'parts'} || die "no parts?";
297 $disk_name = $dvd_nr;
300 # parts start with 0, so we have -1 here
301 $part_path = '.' . ($part_nr - 1);
302 $disk_name .= '.' . $part_nr;
305 print "Processing part $part_nr/$parts\n";
307 my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
308 $list_file .= $part_path . '.list';
309 $iso_file .= $part_path . '.iso';
313 # check if ISO file exists
316 if (! -e $iso_file) {
318 open(my $list, "> $list_file") || skip "can't open $list_file: $!";
323 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
324 if (-f "$tar_dir/$tar_file") {
325 skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
326 print $list "$tar_dir/$tar_file\n";
328 my $len = length("$parts");
329 my $nr = sprintf("%0${len}d", ($part_nr - 1));
330 print $list "$tar_dir/$tar_file/part$nr\n";
333 } while ($row = $sth_archive_backup->fetchrow_hashref);
335 # add file list and note in xml
336 dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
337 print $list "$xml_file\n";
339 # add css file for archive
340 my $css_file = $Conf{CgiImageDir} . '/archive.css';
342 print $list "$css_file\n";
344 print "WARNING: missing $css_file, not added to iso image!\n";
349 print "Running mkisofs now for $inc increments, disk $disk_name\n";
351 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 };
353 system($cmd) == 0 or skip "can't run $cmd: $?";
355 $iso_size = (stat($iso_file))[7];
357 print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
360 print "ISO $iso_file allready exists\n";
363 my $copies = $Conf{BurnMultipleCopies} || 1;
365 foreach my $copy_nr ( 1 .. $copies ) {
367 print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
369 system($bin->{'eject'}) == 0 or skip "can't run eject: $?";
373 my $cmd = $bin->{$cdrecord} . ' ' . $cdr_opts . ' ' . $iso_file;
377 system($cmd) == 0 or skip "can't run $cmd: $?";
379 print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
381 $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr);
383 print "Media burn for $disk_name copy $copy_nr recorded\n";
387 } until ($part_nr > $parts);
393 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
394 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
396 print "Recoding finished, exiting...\n";
399 $sth_archive_backup->finish;
400 $sth_archive_burned->finish;