tolerate bigger file sizes in database than on filesystem, consider zero-sized
[BackupPC.git] / bin / BackupPC_burnArchiveCLI
index bee4d05..5005889 100755 (executable)
@@ -16,21 +16,29 @@ use Data::Dumper;
 my $debug = 0;
 $|=1;
 
-my $cdr_opts = 'dev=/dev/hdc blank=fast -v -eject';
+# don't check for user
+my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
+my %Conf = $bpc->Conf();
+%BackupPC::SearchLib::Conf = %Conf;
+
+my $conf_bin;
+
+$conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
+my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
+$conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
+my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
+$conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
+
 
 my $bin;
-foreach my $c (qw/mkisofs cdrecord eject/) {
-       $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
+foreach my $c (qw/cdrecord eject mkisofs/) {
+       $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
 }
 
 my $start_t = time();
 
 my $t_fmt = '%Y-%m-%d %H:%M:%S';
 
-# don't check for user
-my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
-my %Conf = $bpc->Conf();
-%BackupPC::SearchLib::Conf = %Conf;
 
 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
 my $user = $Conf{SearchUser} || '';
@@ -64,10 +72,13 @@ sub curr_time {
 sub dumpArchive2XML($$$)
 {
        my ($dbh, $dvd_nr, $filename) = @_;
-       my %archive;    
-       my $output = new IO::File(">$filename");
+       my %archive;
+
+       my $output = new IO::File('> '.$filename.'.tmp');
        my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
 
+       print "Dumping file list for DVD $dvd_nr, countdown:";
+
        $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
 
        my $files_sql = q{
@@ -118,8 +129,19 @@ sub dumpArchive2XML($$$)
        my $sth_backups = $dbh->prepare( $backups_sql );
        $sth_backups->execute( $row_h->{'id'} );
 
+       my $total_backups = $sth_backups->rows;
+       my $curr_backup = 0;
+       my $last_pcnt = 0;
+
        while (my $row = $sth_backups->fetchrow_hashref()) {
 
+               $curr_backup++;
+               my $pcnt = int(($curr_backup * 10) / $total_backups);
+               if ($pcnt > $last_pcnt) {
+                       print " ",(10 - $pcnt);
+                       $last_pcnt = $pcnt;
+               }
+
                my $sth_files = $dbh->prepare( $files_sql);
                $sth_files->execute($row->{'backup_id'});
 
@@ -143,7 +165,10 @@ sub dumpArchive2XML($$$)
                         
        $writer->endTag("archive");
        $writer->end(); 
-       
+
+       rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
+
+       print "\n";
 }
 
 
@@ -240,8 +265,8 @@ my $sth_archive_backup = $dbh->prepare( qq{
 
 my $sth_archive_burned = $dbh->prepare( qq{
        insert into archive_burned
-               (archive_id, iso_size, part)
-       values ( (select id from archive where dvd_nr =?), ?, ?)
+               (archive_id, iso_size, part, copy)
+       values ( (select id from archive where dvd_nr =?), ?, ?, ?)
 });
 
 foreach my $arc (@archives_to_burn) {
@@ -279,13 +304,13 @@ foreach my $arc (@archives_to_burn) {
                if ($parts > 1) {
                        # parts start with 0, so we have -1 here
                        $part_path = '.' . ($part_nr - 1);
-                       $disk_name .= '.' . $part_nr;
+                       $disk_name .= ' ' . $part_nr . '/' . $parts;
                }
 
                print "Processing part $part_nr/$parts\n";
 
                my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
-               $list_file .= '.list';
+               $list_file .= $part_path . '.list';
                $iso_file .= $part_path . '.iso';
                $xml_file .= '.xml';
 
@@ -306,7 +331,7 @@ foreach my $arc (@archives_to_burn) {
                                        print $list "$tar_dir/$tar_file\n";
                                } else {
                                        my $len = length("$parts");
-                                       my $nr = sprintf("%0${len}d", $part_nr);
+                                       my $nr = sprintf("%0${len}d", ($part_nr - 1));
                                        print $list "$tar_dir/$tar_file/part$nr\n";
                                }
                                $inc++;
@@ -328,10 +353,12 @@ foreach my $arc (@archives_to_burn) {
 
                        print "Running mkisofs now for $inc increments, disk $disk_name\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 };
+                       my $cmd = $bin->{'mkisofs'} . qq{ -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V "$disk_name" -o ${iso_file}.tmp -path-list $list_file };
 
                        system($cmd) == 0 or skip "can't run $cmd: $?";
 
+                       rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
+
                        $iso_size = (stat($iso_file))[7];
 
                        print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
@@ -340,22 +367,28 @@ foreach my $arc (@archives_to_burn) {
                        print "ISO $iso_file allready exists\n";
                }
 
-               print "\nREADY TO BURN MEDIA $disk_name please insert blank media and press ENTER\n\n";
+               my $copies = $Conf{BurnMultipleCopies} || 1;
+
+               foreach my $copy_nr ( 1 .. $copies ) {
 
-               system($bin->{'eject'}) == 0 or skip "can't run eject: $?";
+                       print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
 
-               my $wait = <STDIN>;
+                       system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
 
-               my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
+                       my $wait = <STDIN>;
 
-#              system($cmd) == 0 or skip "can't run $cmd: $?";
-               print "## $cmd\n";
+                       my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
 
-               print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
+                       # FIXME
+                       print "## $cmd\n";
+                       system($cmd) == 0 or skip "can't run $cmd: $?";
 
-               $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr);
+                       print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
 
-               print "Media burn for $disk_name recorded\n";
+                       $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr);
+
+                       print "Media burn for $disk_name copy $copy_nr recorded\n";
+               }
 
                $part_nr++;
        } until ($part_nr > $parts);