r8512@llin: dpavlin | 2005-10-13 23:06:07 +0200
[BackupPC.git] / bin / BackupPC_burnArchiveCLI
index 9522047..bee4d05 100755 (executable)
@@ -9,12 +9,20 @@ use BackupPC::SearchLib;
 use Time::HiRes qw/time/;
 use POSIX qw/strftime/;
 use Term::Menus;
+use File::Which;
 
 use Data::Dumper;
 
 my $debug = 0;
 $|=1;
 
+my $cdr_opts = 'dev=/dev/hdc blank=fast -v -eject';
+
+my $bin;
+foreach my $c (qw/mkisofs cdrecord eject/) {
+       $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
+}
+
 my $start_t = time();
 
 my $t_fmt = '%Y-%m-%d %H:%M:%S';
@@ -97,7 +105,7 @@ sub dumpArchive2XML($$$)
 
        my $row_h;
        foreach my $hide (qw/id note/) {
-               $row_h->{$hide} = $row->{$hide} || die "no $hide";
+               $row_h->{$hide} = $row->{$hide} || '';
                delete $row->{$hide};
        }
 
@@ -220,7 +228,8 @@ my $sth_archive_backup = $dbh->prepare( qq{
                archive_id,
                hosts.name as host,
                shares.name as share,
-               backups.num as num
+               backups.num as num,
+               backups.parts as parts
        from archive_backup
        join archive on archive_id = archive.id
        join backups on backup_id = backups.id
@@ -231,8 +240,8 @@ my $sth_archive_backup = $dbh->prepare( qq{
 
 my $sth_archive_burned = $dbh->prepare( qq{
        insert into archive_burned
-               (archive_id, iso_size)
-       values ( (select id from archive where dvd_nr =?), ?)
+               (archive_id, iso_size, part)
+       values ( (select id from archive where dvd_nr =?), ?, ?)
 });
 
 foreach my $arc (@archives_to_burn) {
@@ -247,58 +256,109 @@ foreach my $arc (@archives_to_burn) {
 
        print "Working on DVD #$dvd_nr in $tmp_dir\n";
 
-       my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
-       $list_file .= '.list';
-       $iso_file .= '.iso';
-       $xml_file .= '.xml';
+       my $part_path = '';
+       my $part_nr = 1;
+       my $parts = 0;
+               
+       $sth_archive_backup->execute($dvd_nr);
 
-       if (-e $iso_file) {
-               print "ISO $iso_file allready exists\n";
-               goto BURN;
-       }
+       my $disk_name = 'unknown disk';
+       my $iso_size = 0;
 
-       $sth_archive_backup->execute($dvd_nr);
+       do {
 
-       open(my $list, "> $list_file") || skip "can't open $list_file: $!";
+               # do we need to re-execute query for multi-part increments?
+               $sth_archive_backup->execute($dvd_nr) if ($parts > 1);
 
-       my $inc = 0;
+               my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";
 
-       while (my $row = $sth_archive_backup->fetchrow_hashref) {
-               my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
-               skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
-               print $list "$tar_dir/$tar_file\n";
-               $inc++;
 
-       }
+               $parts =  $row->{'parts'} || die "no parts?";
+               $disk_name = $dvd_nr;
 
-       # add file list and note in xml
-       dumpArchive2XML($dbh, $dvd_nr, $xml_file);
-       print $list "$xml_file\n";
+               if ($parts > 1) {
+                       # parts start with 0, so we have -1 here
+                       $part_path = '.' . ($part_nr - 1);
+                       $disk_name .= '.' . $part_nr;
+               }
 
-       # add css file for archive
-       my $css_file = $Conf{CgiImageDir} . '/archive.css';
-       if (-r $css_file) {
-               print $list "$css_file\n";
-       } else {
-               print "WARNING: missing $css_file, not added to iso image!\n";
-       }
+               print "Processing part $part_nr/$parts\n";
+
+               my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
+               $list_file .= '.list';
+               $iso_file .= $part_path . '.iso';
+               $xml_file .= '.xml';
+
+               #
+               # check if ISO file exists
+               #
+
+               if (! -e $iso_file) {
+
+                       open(my $list, "> $list_file") || skip "can't open $list_file: $!";
+
+                       my $inc = 0;
 
-       close($list);
+                       do {
+                               my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
+                               if (-f "$tar_dir/$tar_file") {
+                                       skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
+                                       print $list "$tar_dir/$tar_file\n";
+                               } else {
+                                       my $len = length("$parts");
+                                       my $nr = sprintf("%0${len}d", $part_nr);
+                                       print $list "$tar_dir/$tar_file/part$nr\n";
+                               }
+                               $inc++;
+                       } while ($row = $sth_archive_backup->fetchrow_hashref);
 
-       print "Running mkisofs now for $inc increments...\n";
+                       # add file list and note in xml
+                       dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
+                       print $list "$xml_file\n";
 
-       my $cmd = qq{ mkisofs -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $dvd_nr -o $iso_file -path-list $list_file };
+                       # add css file for archive
+                       my $css_file = $Conf{CgiImageDir} . '/archive.css';
+                       if (-r $css_file) {
+                               print $list "$css_file\n";
+                       } else {
+                               print "WARNING: missing $css_file, not added to iso image!\n";
+                       }
 
-       system($cmd) == 0 or skip "can't run $cmd: $?";
+                       close($list);
 
-       my $size = (stat($iso_file))[7];
+                       print "Running mkisofs now for $inc increments, disk $disk_name\n";
 
-       print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n";
+                       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 };
 
-BURN:
-       # FIXME add call to cdrecord here!
-       $sth_archive_burned->execute($dvd_nr, $size);
-       print "Media burn for $dvd_nr recorded\n";
+                       system($cmd) == 0 or skip "can't run $cmd: $?";
+
+                       $iso_size = (stat($iso_file))[7];
+
+                       print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
+
+               } else {
+                       print "ISO $iso_file allready exists\n";
+               }
+
+               print "\nREADY TO BURN MEDIA $disk_name please insert blank media and press ENTER\n\n";
+
+               system($bin->{'eject'}) == 0 or skip "can't run eject: $?";
+
+               my $wait = <STDIN>;
+
+               my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
+
+#              system($cmd) == 0 or skip "can't run $cmd: $?";
+               print "## $cmd\n";
+
+               print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
+
+               $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr);
+
+               print "Media burn for $disk_name recorded\n";
+
+               $part_nr++;
+       } until ($part_nr > $parts);
 
 SKIP:
 
@@ -307,6 +367,11 @@ SKIP:
 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
 
+print "Recoding finished, exiting...\n";
+
 $sth->finish;
+$sth_archive_backup->finish;
+$sth_archive_burned->finish;
+
 $dbh->disconnect;