/[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 319 by dpavlin, Mon Jan 30 14:58:46 2006 UTC revision 338 by dpavlin, Wed Mar 1 13:24:36 2006 UTC
# Line 11  use POSIX qw/strftime/; Line 11  use POSIX qw/strftime/;
11  use Term::Menus;  use Term::Menus;
12  use File::Which;  use File::Which;
13  use File::Path;  use File::Path;
14    use Filesys::Df;
15    
16  use Data::Dumper;  use Data::Dumper;
17    
# Line 193  order by date asc Line 194  order by date asc
194    
195  $sth->execute();  $sth->execute();
196    
197    sub fmt_mb($) {
198            my $s = shift;
199            die "missing size" unless defined($s);
200            $s /= (1024*1024);
201            return sprintf("%.2f Mb", $s);
202    }
203    
204  sub fmt_archive($) {  sub fmt_archive($) {
205          my $row = shift || die;          my $row = shift || die;
206    
207          $row->{'date'} =~ s/\.\d+$//;          $row->{'date'} =~ s/\.\d+$//;
208          $row->{'copies'} =~ s/^\s*0+\s*$/no/;          $row->{'copies'} =~ s/^\s*0+\s*$/no/;
         $row->{'total_size'} /= (1024*1024);  
209    
210          my $copies = 'copies';          my $copies = 'copies';
211          $copies = 'copy' if ($row->{'copies'} == 1);          $copies = 'copy' if ($row->{'copies'} == 1);
212    
213          return          return
214                  sprintf("%d by %s on %s, %s %s [%.2f Mb]",                  sprintf("%d by %s on %s, %s %s [%s]",
215                          $row->{'dvd_nr'},                          $row->{'dvd_nr'},
216                          $row->{'username'},                          $row->{'username'},
217                          $row->{'date'},                          $row->{'date'},
218                          $row->{'copies'}, $copies,                          $row->{'copies'}, $copies,
219                          $row->{'total_size'},                          fmt_mb($row->{'total_size'}),
220                  );                  );
221  }  }
222    
# Line 278  sub delete_dvd($) { Line 285  sub delete_dvd($) {
285    
286                  # remove files for this DVD                  # remove files for this DVD
287                  map {                  map {
288                          print "\tremoving $_\n";                          if (-d $_) {
289                          unlink($_) || die "can't rm $_: $!";                                  print "\tremoving dir $_\n";
290                                    rmtree($_) || die "can't rmtree $_: $!";
291                            } else {
292                                    print "\tremoving $_\n";
293                                    unlink($_) || die "can't rm $_: $!";
294                            }
295                  } glob ( "/$iso_dir/$dvd_nr.*" );                  } glob ( "/$iso_dir/$dvd_nr.*" );
296    
297                  $dbh->commit;                  $dbh->commit;
# Line 314  my $sth_archive_backup_parts = $dbh->pre Line 326  my $sth_archive_backup_parts = $dbh->pre
326    
327  my $sth_archive_backup_check = $dbh->prepare( qq{  my $sth_archive_backup_check = $dbh->prepare( qq{
328                  SELECT                  SELECT
329                          count(backups.id)                          sum(backups.parts)
330                  FROM backups                  FROM backups
331                  JOIN archive_backup on archive_backup.backup_id = backups.id                  JOIN archive_backup on archive_backup.backup_id = backups.id
332                  JOIN archive on archive_id = archive.id                  JOIN archive on archive_id = archive.id
# Line 327  my $sth_archive_burned = $dbh->prepare( Line 339  my $sth_archive_burned = $dbh->prepare(
339          values ( (select id from archive where dvd_nr =?), ?, ?, ?)          values ( (select id from archive where dvd_nr =?), ?, ?, ?)
340  });  });
341    
342  foreach my $arc (@archives_to_burn) {  my $copies = $Conf{BurnMultipleCopies} || 1;
         exit if ($arc eq ']quit[');  
343    
344          my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);  foreach my $copy_nr ( 1 .. $copies ) {
         die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);  
345    
346          my $tmp_dir = "/$iso_dir/$dvd_nr";          foreach my $arc (@archives_to_burn) {
347                    exit if ($arc eq ']quit[');
348    
349          my $t = time();                  my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
350                    die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
351    
352          print "Working on DVD #$dvd_nr in $tmp_dir\n";                  my $tmp_dir = "/$iso_dir/$dvd_nr";
353    
354          $sth_archive_backup_parts->execute($dvd_nr);                  my $t = time();
355    
356          $sth_archive_backup_check->execute($dvd_nr);                  print "Working on DVD #$dvd_nr in $tmp_dir\n";
357    
358          my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);                  $sth_archive_backup_parts->execute($dvd_nr);
359    
360          if ($parts_nr != $check_nr) {                  $sth_archive_backup_check->execute($dvd_nr);
                 warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",  
                         ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",  
                         " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";  
                 delete_dvd( $dvd_nr );  
                 next;  
         }  
   
         if ($sth_archive_backup_parts->rows == 0) {  
                 warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";  
                 next if delete_dvd( $dvd_nr );  
         }  
361    
362          if ($prompt_for_delete) {                  my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
                 next if delete_dvd( $dvd_nr );  
         }  
363    
364          my @volumes;                  if ($parts_nr != $check_nr) {
365          my $v;  # emtpy volume                          warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",
366          my $v_size = 0;                                  ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",
367                                    " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";
368          my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";                          delete_dvd( $dvd_nr );
369          while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {                          next;
                 if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {  
                         push @volumes, $v;  
                         $v = {};  
370                  }                  }
371                  $v->{size} += $row->{part_size};  
372                  # this part                  if ($sth_archive_backup_parts->rows == 0) {
373                  my $p = {                          warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
374                          filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),                          next if delete_dvd( $dvd_nr );
                 };  
                 foreach my $fld (qw/part_nr md5/) {  
                         $p->{$fld} = $row->{$fld} || die "missing $fld in row!";  
375                  }                  }
                 push @{ $v->{parts} }, $p;  
         }  
         push @volumes, $v if ($v);  
376    
377          #warn "# volumes: ",Dumper(\@volumes),"\n";                  if ($prompt_for_delete) {
378                            next if delete_dvd( $dvd_nr );
379                    }
380    
381          my $volumes = $#volumes + 1;                  my @volumes;
382          my $volume_nr = 1;                  my $v;  # emtpy volume
383                    my $v_size = 0;
384    
385                    my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
386                    while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
387                            if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
388                                    push @volumes, $v;
389                                    $v = {};
390                            }
391                            $v->{size} += $row->{part_size};
392                            $v_size += $row->{part_size};
393    
394          foreach my $v (@volumes) {                          # this part
395                            my $p = {
396                                    filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
397                            };
398                            foreach my $fld (qw/part_nr md5/) {
399                                    $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
400                            }
401                            push @{ $v->{parts} }, $p;
402                    }
403                    push @volumes, $v if ($v);
404    
405                  #print Dumper($v);                  #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
406    
407                  my $iso_size = 0;                  # check available disk space
                 my $disk_name = $dvd_nr;  
                 # suffix added to multi-volume archives  
                 my $volume_suffix = '';  
408    
409                  if ($volumes > 1) {                  my $df = df($iso_dir);
410                          $volume_suffix = '_' . $volume_nr;                  if ($df < $v_size) {
411                          $disk_name .= ' ' . $volume_nr . '/' . $volumes;                          warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( df($iso_dir) ), "]\n";
412                            next;
413                  }                  }
414    
415                  print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";                  my $volumes = $#volumes + 1;
416                    my $volume_nr = 1;
417    
418                  my $iso_file = my $xml_file = my $stage =                  foreach my $v (@volumes) {
                         "${iso_dir}/${dvd_nr}";  
419    
420                  $iso_file .= $volume_suffix . '.iso';                          #print Dumper($v);
                 $xml_file .= '.xml';  
421    
422                  $stage .= $volume_suffix . '.stage';                          my $iso_size = 0;
423                            my $disk_name = $dvd_nr;
424                            # suffix added to multi-volume archives
425                            my $volume_suffix = '';
426    
427                  my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";                          if ($volumes > 1) {
428                                    $volume_suffix = '_' . $volume_nr;
429                                    $disk_name .= ' ' . $volume_nr . '/' . $volumes;
430                            }
431    
432                  #                          print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
                 # check if ISO file exists  
                 #  
433    
434                  if (! -e $iso_file) {                          my $iso_file = my $xml_file = my $stage =
435                                    "${iso_dir}/${dvd_nr}";
436    
437                          # create stage directory                          $iso_file .= $volume_suffix . '.iso';
438                          if (-e $stage) {                          $xml_file .= '.xml';
                                 rmtree($stage) || die "can't remove $stage: $!";  
                         }  
                         mkpath($stage);  
439    
440                          # open file for md5sums                          $stage .= $volume_suffix . '.stage';
                         open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";  
441    
442                          my $parts_on_this_volume = 0;                          my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
443    
444                          foreach my $p (@{ $v->{parts} }) {                          #
445                                  my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);                          # check if ISO file exists
446                                  my $rel_path = $tar_file;                          #
447    
448                                  if (-d "$tar_dir/$rel_path") {                          if (! -e $iso_file) {
449                                          mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");  
450                                          $rel_path .= '/' . $p->{part_nr};                                  # create stage directory
451                                    if (-e $stage) {
452                                            rmtree($stage) || die "can't remove $stage: $!";
453                                  }                                  }
454                                  $rel_path .= '.tar.gz';                                  mkpath($stage);
455    
456                                  skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");                                  # open file for md5sums
457                                    open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
458    
459                                  add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");                                  my $parts_on_this_volume = 0;
460    
461                                  my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);                                  foreach my $p (@{ $v->{parts} }) {
462                                  chomp($md5sum);                                          my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
463                                  print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";                                          my $rel_path = $tar_file;
464    
465                                  $parts_on_this_volume++;                                          if (-d "$tar_dir/$rel_path") {
466                          }                                                  mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
467                                                    $rel_path .= '/' . $p->{part_nr};
468                                            }
469                                            $rel_path .= '.tar.gz';
470    
471                          # add file list and note in xml                                          skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
                         dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);  
472    
473                          add_symlink($xml_file, "$stage/${dvd_nr}.xml");                                          add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
474    
475                          # add css file for archive                                          my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
476                          my $css_file = $Conf{CgiImageDir} . '/archive.css';                                          chomp($md5sum);
477                          if (-r $css_file) {                                          print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
478                                  add_symlink($css_file, "$stage/archive.css");  
479                          } else {                                          $parts_on_this_volume++;
480                                  print "WARNING: missing $css_file, not added to iso image!\n";                                  }
                         }  
481    
482                          print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";                                  # add file list and note in xml
483                                    dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
484    
485                          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 };                                  add_symlink($xml_file, "$stage/${dvd_nr}.xml");
486    
487                          system($cmd) == 0 or skip "can't run $cmd: $?";                                  # add css file for archive
488                                    my $css_file = $Conf{CgiImageDir} . '/archive.css';
489                                    if (-r $css_file) {
490                                            add_symlink($css_file, "$stage/archive.css");
491                                    } else {
492                                            print "WARNING: missing $css_file, not added to iso image!\n";
493                                    }
494    
495                          rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";                                  print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
496    
497                          $iso_size = (stat($iso_file))[7];                                  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 };
498    
499                          print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";                                  system($cmd) == 0 or skip "can't run $cmd: $?";
500    
501                          rmtree($stage) || warn "can't remove stage directory $stage: $!";                                  rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
502    
503                  } else {                                  $iso_size = (stat($iso_file))[7];
                         print "ISO $iso_file allready exists\n";  
                 }  
504    
505                  my $copies = $Conf{BurnMultipleCopies} || 1;                                  print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
506    
507                  foreach my $copy_nr ( 1 .. $copies ) {                                  rmtree($stage) || warn "can't remove stage directory $stage: $!";
508    
509                            } else {
510                                    $iso_size = (stat($iso_file))[7];
511                                    print "ISO $iso_file allready exists [$iso_size bytes]\n";
512                            }
513    
514                          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";
515    
# Line 505  foreach my $arc (@archives_to_burn) { Line 528  foreach my $arc (@archives_to_burn) {
528                          $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);                          $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
529    
530                          print "Media burn for $disk_name copy $copy_nr recorded\n";                          print "Media burn for $disk_name copy $copy_nr recorded\n";
531                    
532                            $volume_nr++;
533                  }                  }
534    
                 $volume_nr++;  
535          }          }
536    
537  SKIP:  SKIP:

Legend:
Removed from v.319  
changed lines
  Added in v.338

  ViewVC Help
Powered by ViewVC 1.1.26