/[BackupPC]/trunk/bin/BackupPC_burnArchiveCLI
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/BackupPC_burnArchiveCLI

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 204 by dpavlin, Fri Oct 14 16:46:33 2005 UTC revision 275 by dpavlin, Tue Dec 13 22:53:30 2005 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3  use strict;  use strict;
4  use lib "__INSTALLDIR__/lib";  #use lib "__INSTALLDIR__/lib";
5    use lib "/data/backuppc-agi/lib";
6    
7  use DBI;  use DBI;
8  use BackupPC::Lib;  use BackupPC::Lib;
# Line 10  use Time::HiRes qw/time/; Line 11  use Time::HiRes qw/time/;
11  use POSIX qw/strftime/;  use POSIX qw/strftime/;
12  use Term::Menus;  use Term::Menus;
13  use File::Which;  use File::Which;
14    use File::Path;
15    
16  use Data::Dumper;  use Data::Dumper;
17    
# Line 21  my $bpc = BackupPC::Lib->new(undef, unde Line 23  my $bpc = BackupPC::Lib->new(undef, unde
23  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
24  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
25    
26  my $cdrecord = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";  my $conf_bin;
27    
28    $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
29  my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";  my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
30    $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
31    my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
32    $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
33    
34    
35  my $bin;  my $bin;
36  foreach my $c (qw/mkisofs eject/, $cdrecord) {  foreach my $c (qw/cdrecord eject mkisofs/) {
37          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";          $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
38  }  }
39    
40  my $start_t = time();  my $start_t = time();
# Line 242  sub skip($) { Line 249  sub skip($) {
249          goto SKIP;          goto SKIP;
250  }  }
251    
252  my $sth_archive_backup = $dbh->prepare( qq{  sub add_symlink($$) {
253            my ($from, $to) = @_;
254            symlink $from, $to || skip("can't symlink $from to $to: $!");
255    }
256    
257    my $sth_archive_backup_parts = $dbh->prepare( qq{
258          select          select
259                  backup_id,                  archive_backup.backup_id,
260                  archive_id,                  archive_id,
261                  hosts.name as host,                  hosts.name as host,
262                  shares.name as share,                  shares.name as share,
263                  backups.num as num,                  backups.num as num,
264                  backups.parts as parts                  backups.parts as parts,
265                    backup_parts.part_nr as part_nr,
266                    backup_parts.size as part_size,
267                    backup_parts.md5 as md5,
268                    backup_parts.items
269          from archive_backup          from archive_backup
270          join archive on archive_id = archive.id          join archive on archive_id = archive.id
271          join backups on backup_id = backups.id          join backups on archive_backup.backup_id = backups.id
272          join hosts on hostid = hosts.id          join hosts on hostid = hosts.id
273          join shares on shareid = shares.id          join shares on shareid = shares.id
274            join backup_parts on archive_backup.backup_id = backup_parts.backup_id
275          where archive.dvd_nr = ?          where archive.dvd_nr = ?
276            order by archive_backup.backup_id, backup_parts.part_nr
277  });  });
278    
279  my $sth_archive_burned = $dbh->prepare( qq{  my $sth_archive_burned = $dbh->prepare( qq{
# Line 276  foreach my $arc (@archives_to_burn) { Line 294  foreach my $arc (@archives_to_burn) {
294    
295          print "Working on DVD #$dvd_nr in $tmp_dir\n";          print "Working on DVD #$dvd_nr in $tmp_dir\n";
296    
297          my $part_path = '';          $sth_archive_backup_parts->execute($dvd_nr);
         my $part_nr = 1;  
         my $parts = 0;  
                   
         $sth_archive_backup->execute($dvd_nr);  
298    
299          my $disk_name = 'unknown disk';          my @volumes;
300          my $iso_size = 0;          my $v;  # emtpy volume
301            my $v_size = 0;
302          do {  
303            my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
304            while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
305                    if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
306                            push @volumes, $v;
307                            $v = {};
308                    }
309                    $v->{size} += $row->{part_size};
310                    # this part
311                    my $p = {
312                            filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
313                    };
314                    foreach my $fld (qw/part_nr md5/) {
315                            $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
316                    }
317                    push @{ $v->{parts} }, $p;
318            }
319            push @volumes, $v if ($v);
320    
321                  # do we need to re-execute query for multi-part increments?          my $volumes = $#volumes + 1;
322                  $sth_archive_backup->execute($dvd_nr) if ($parts > 1);          my $volume_nr = 1;
323    
324                  my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";          foreach my $v (@volumes) {
325    
326                    print Dumper($v);
327    
328                  $parts =  $row->{'parts'} || die "no parts?";                  my $iso_size = 0;
329                  $disk_name = $dvd_nr;                  my $disk_name = $dvd_nr;
330                    # suffix added to multi-volume archives
331                    my $volume_suffix = $dvd_nr;
332    
333                  if ($parts > 1) {                  if ($volumes > 1) {
334                          # parts start with 0, so we have -1 here                          $volume_suffix = '_' . $volume_nr;
335                          $part_path = '.' . ($part_nr - 1);                          $disk_name .= ' ' . $volume_nr . '/' . $volumes;
                         $disk_name .= '.' . $part_nr;  
336                  }                  }
337    
338                  print "Processing part $part_nr/$parts\n";                  print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";
339    
340                    my $iso_file = my $xml_file = my $stage =
341                            "${iso_dir}/${dvd_nr}";
342    
343                  my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";                  $iso_file .= $volume_suffix . '.iso';
                 $list_file .= $part_path . '.list';  
                 $iso_file .= $part_path . '.iso';  
344                  $xml_file .= '.xml';                  $xml_file .= '.xml';
345    
346                    $stage .= $volume_suffix . '.stage';
347    
348                    my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
349    
350                  #                  #
351                  # check if ISO file exists                  # check if ISO file exists
352                  #                  #
353    
354                  if (! -e $iso_file) {                  if (! -e $iso_file) {
355    
356                          open(my $list, "> $list_file") || skip "can't open $list_file: $!";                          # create stage directory
357                            if (-e $stage) {
358                                    rmtree($stage) || die "can't remove $stage: $!";
359                            }
360                            mkpath($stage);
361    
362                            # open file for md5sums
363                            open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
364    
365                          my $inc = 0;                          my $parts_on_this_volume = 0;
366    
367                          do {                          foreach my $p (@{ $v->{parts} }) {
368                                  my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});                                  my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
369                                  if (-f "$tar_dir/$tar_file") {                                  my $rel_path = $tar_file;
370                                          skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");  
371                                          print $list "$tar_dir/$tar_file\n";                                  if (-d "$tar_dir/$rel_path") {
372                                  } else {                                          $rel_path .= '/' . $p->{part_nr};
373                                          my $len = length("$parts");                                          mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
                                         my $nr = sprintf("%0${len}d", ($part_nr - 1));  
                                         print $list "$tar_dir/$tar_file/part$nr\n";  
374                                  }                                  }
375                                  $inc++;                                  $rel_path .= '.tar.gz';
376                          } while ($row = $sth_archive_backup->fetchrow_hashref);  
377                                    skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
378    
379                                    add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
380    
381                                    my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
382                                    print $md5 "$md5sum\s\s$rel_path\n" || die "can't write md5sum: $!";
383    
384                                    $parts_on_this_volume++;
385                            }
386    
387                          # add file list and note in xml                          # add file list and note in xml
388                          dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);                          dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
389                          print $list "$xml_file\n";  
390                            add_symlink($xml_file, "$stage/${dvd_nr}.xml");
391    
392                          # add css file for archive                          # add css file for archive
393                          my $css_file = $Conf{CgiImageDir} . '/archive.css';                          my $css_file = $Conf{CgiImageDir} . '/archive.css';
394                          if (-r $css_file) {                          if (-r $css_file) {
395                                  print $list "$css_file\n";                                  add_symlink($css_file, "$stage/archive.css");
396                          } else {                          } else {
397                                  print "WARNING: missing $css_file, not added to iso image!\n";                                  print "WARNING: missing $css_file, not added to iso image!\n";
398                          }                          }
399    
400                          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";  
401    
402                          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 };
403    
404                          system($cmd) == 0 or skip "can't run $cmd: $?";                          system($cmd) == 0 or skip "can't run $cmd: $?";
405    
406                            rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
407    
408                          $iso_size = (stat($iso_file))[7];                          $iso_size = (stat($iso_file))[7];
409    
410                          print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";                          print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
411    
412                            # FIXME
413                            #rmtree($stage) || warn "can't remove stage directory $stage: $!";
414    
415                  } else {                  } else {
416                          print "ISO $iso_file allready exists\n";                          print "ISO $iso_file allready exists\n";
417                  }                  }
# Line 366  foreach my $arc (@archives_to_burn) { Line 422  foreach my $arc (@archives_to_burn) {
422    
423                          print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";                          print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
424    
425                          system($bin->{'eject'}) == 0 or skip "can't run eject: $?";                          system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
426    
427                          my $wait = <STDIN>;                          my $wait = <STDIN>;
428    
429                          my $cmd = $bin->{$cdrecord} . ' ' . $cdr_opts . ' ' . $iso_file;                          my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
430    
431                          # FIXME                          # FIXME
432                          print "## $cmd\n";                          print "## $cmd\n";
# Line 378  foreach my $arc (@archives_to_burn) { Line 434  foreach my $arc (@archives_to_burn) {
434    
435                          print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";                          print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
436    
437                          $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr);                          $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
438    
439                          print "Media burn for $disk_name copy $copy_nr recorded\n";                          print "Media burn for $disk_name copy $copy_nr recorded\n";
440                  }                  }
441    
442                  $part_nr++;                  $volume_nr++;
443          } until ($part_nr > $parts);          }
444    
445  SKIP:  SKIP:
446    
# Line 396  SKIP: Line 452  SKIP:
452  print "Recoding finished, exiting...\n";  print "Recoding finished, exiting...\n";
453    
454  $sth->finish;  $sth->finish;
455  $sth_archive_backup->finish;  $sth_archive_backup_parts->finish;
456  $sth_archive_burned->finish;  $sth_archive_burned->finish;
457    
458  $dbh->disconnect;  $dbh->disconnect;

Legend:
Removed from v.204  
changed lines
  Added in v.275

  ViewVC Help
Powered by ViewVC 1.1.26