X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=bin%2FBackupPC_burnArchiveCLI;h=ab5a29f28fbe212cfe067649f07cfb366323b4b3;hb=2c4fed2613239b442411047d76f96f3b7988a7e9;hp=bee4d05887d41966779e8124b0ea4b42e4505c1b;hpb=6cd4c5b3a631276c8dec795a6e6fd1d3674af76b;p=BackupPC.git diff --git a/bin/BackupPC_burnArchiveCLI b/bin/BackupPC_burnArchiveCLI index bee4d05..ab5a29f 100755 --- a/bin/BackupPC_burnArchiveCLI +++ b/bin/BackupPC_burnArchiveCLI @@ -10,27 +10,36 @@ use Time::HiRes qw/time/; use POSIX qw/strftime/; use Term::Menus; use File::Which; +use File::Path; 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 +73,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 +130,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 +166,10 @@ sub dumpArchive2XML($$$) $writer->endTag("archive"); $writer->end(); - + + rename $filename.'.tmp', $filename || die "can't rename $filename: $!"; + + print "\n"; } @@ -222,26 +248,37 @@ sub skip($) { goto SKIP; } -my $sth_archive_backup = $dbh->prepare( qq{ +sub add_symlink($$) { + my ($from, $to) = @_; + symlink $from, $to || skip("can't symlink $from to $to: $!"); +} + +my $sth_archive_backup_parts = $dbh->prepare( qq{ select - backup_id, + archive_backup.backup_id, archive_id, hosts.name as host, shares.name as share, backups.num as num, - backups.parts as parts + backups.parts as parts, + backup_parts.part_nr as part_nr, + backup_parts.size as part_size, + backup_parts.md5 as md5, + backup_parts.items from archive_backup join archive on archive_id = archive.id - join backups on backup_id = backups.id + join backups on archive_backup.backup_id = backups.id join hosts on hostid = hosts.id join shares on shareid = shares.id + join backup_parts on archive_backup.backup_id = backup_parts.backup_id where archive.dvd_nr = ? + order by archive_backup.backup_id, backup_parts.part_nr }); 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) { @@ -256,109 +293,159 @@ foreach my $arc (@archives_to_burn) { print "Working on DVD #$dvd_nr in $tmp_dir\n"; - my $part_path = ''; - my $part_nr = 1; - my $parts = 0; - - $sth_archive_backup->execute($dvd_nr); + $sth_archive_backup_parts->execute($dvd_nr); + + if ($sth_archive_backup_parts->rows == 0) { + warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n"; + } + + my @volumes; + my $v; # emtpy volume + my $v_size = 0; - my $disk_name = 'unknown disk'; - my $iso_size = 0; + my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize"; + while (my $row = $sth_archive_backup_parts->fetchrow_hashref) { + if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) { + push @volumes, $v; + $v = {}; + } + $v->{size} += $row->{part_size}; + # this part + my $p = { + filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}), + }; + foreach my $fld (qw/part_nr md5/) { + $p->{$fld} = $row->{$fld} || die "missing $fld in row!"; + } + push @{ $v->{parts} }, $p; + } + push @volumes, $v if ($v); - do { + #warn "# volumes: ",Dumper(\@volumes),"\n"; - # do we need to re-execute query for multi-part increments? - $sth_archive_backup->execute($dvd_nr) if ($parts > 1); + my $volumes = $#volumes + 1; + my $volume_nr = 1; - my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row"; + foreach my $v (@volumes) { + #print Dumper($v); - $parts = $row->{'parts'} || die "no parts?"; - $disk_name = $dvd_nr; + my $iso_size = 0; + my $disk_name = $dvd_nr; + # suffix added to multi-volume archives + my $volume_suffix = $dvd_nr; - if ($parts > 1) { - # parts start with 0, so we have -1 here - $part_path = '.' . ($part_nr - 1); - $disk_name .= '.' . $part_nr; + if ($volumes > 1) { + $volume_suffix = '_' . $volume_nr; + $disk_name .= ' ' . $volume_nr . '/' . $volumes; } - print "Processing part $part_nr/$parts\n"; + print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n"; - my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; - $list_file .= '.list'; - $iso_file .= $part_path . '.iso'; + my $iso_file = my $xml_file = my $stage = + "${iso_dir}/${dvd_nr}"; + + $iso_file .= $volume_suffix . '.iso'; $xml_file .= '.xml'; + $stage .= $volume_suffix . '.stage'; + + my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5"; + # # check if ISO file exists # if (! -e $iso_file) { - open(my $list, "> $list_file") || skip "can't open $list_file: $!"; + # create stage directory + if (-e $stage) { + rmtree($stage) || die "can't remove $stage: $!"; + } + mkpath($stage); + + # open file for md5sums + open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!"; + + my $parts_on_this_volume = 0; - my $inc = 0; + foreach my $p (@{ $v->{parts} }) { + my $tar_file = $p->{filename} || die "no filename in part", Dumper($p); + my $rel_path = $tar_file; - 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"; + if (-d "$tar_dir/$rel_path") { + mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path"); + $rel_path .= '/' . $p->{part_nr}; } - $inc++; - } while ($row = $sth_archive_backup->fetchrow_hashref); + $rel_path .= '.tar.gz'; + + skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path"); + + add_symlink("$tar_dir/$rel_path", "$stage/$rel_path"); + + my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p); + chomp($md5sum); + print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!"; + + $parts_on_this_volume++; + } # add file list and note in xml dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); - print $list "$xml_file\n"; + + add_symlink($xml_file, "$stage/${dvd_nr}.xml"); # add css file for archive my $css_file = $Conf{CgiImageDir} . '/archive.css'; if (-r $css_file) { - print $list "$css_file\n"; + add_symlink($css_file, "$stage/archive.css"); } else { print "WARNING: missing $css_file, not added to iso image!\n"; } - close($list); + print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n"; - 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 -f $stage }; 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"; + rmtree($stage) || warn "can't remove stage directory $stage: $!"; + } else { 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; - system($bin->{'eject'}) == 0 or skip "can't run eject: $?"; + foreach my $copy_nr ( 1 .. $copies ) { - my $wait = ; + print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n"; - my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file; + system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?"; -# system($cmd) == 0 or skip "can't run $cmd: $?"; - print "## $cmd\n"; + my $wait = ; - print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; + my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file; - $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr); + # FIXME + print "## $cmd\n"; + system($cmd) == 0 or skip "can't run $cmd: $?"; + + 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, $volume_nr, $copy_nr); - $part_nr++; - } until ($part_nr > $parts); + print "Media burn for $disk_name copy $copy_nr recorded\n"; + } + + $volume_nr++; + } SKIP: @@ -370,7 +457,7 @@ SKIP: print "Recoding finished, exiting...\n"; $sth->finish; -$sth_archive_backup->finish; +$sth_archive_backup_parts->finish; $sth_archive_burned->finish; $dbh->disconnect;