/[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 288 by dpavlin, Sun Jan 15 14:50:39 2006 UTC revision 341 by dpavlin, Tue Mar 7 10:06:52 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    
18  my $debug = 0;  my $debug = 0;
19  # set this to 1 to prompt for DVD removal for each selected item.  # set this to 1 to prompt for DVD removal for each selected item.
20  my $prompt_for_delete = 0;  my $prompt_for_delete = shift @ARGV;
21  $|=1;  $|=1;
22    
23  # don't check for user  # don't check for user
# Line 111  sub dumpArchive2XML($$$) Line 112  sub dumpArchive2XML($$$)
112                          AND hosts.id=backups.hostid                          AND hosts.id=backups.hostid
113                          AND shares.id=backups.shareid                          AND shares.id=backups.shareid
114                          AND archive_backup.archive_id = ?                          AND archive_backup.archive_id = ?
115                    ORDER BY
116                            hosts.name, shares.name, backups.num
117          };          };
118    
119          my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");          my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
# Line 164  sub dumpArchive2XML($$$) Line 167  sub dumpArchive2XML($$$)
167                          $writer->endTag("file");                          $writer->endTag("file");
168                  }                  }
169                  $writer->endTag("backup");                  $writer->endTag("backup");
170          }                }
171                                                    
172          $writer->endTag("archive");          $writer->endTag("archive");
173          $writer->end();          $writer->end();
# Line 191  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 276  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 310  my $sth_archive_backup_parts = $dbh->pre Line 324  my $sth_archive_backup_parts = $dbh->pre
324          order by archive_backup.backup_id, backup_parts.part_nr          order by archive_backup.backup_id, backup_parts.part_nr
325  });  });
326    
327    my $sth_archive_backup_check = $dbh->prepare( qq{
328                    SELECT
329                            sum(backups.parts)
330                    FROM backups
331                    JOIN archive_backup on archive_backup.backup_id = backups.id
332                    JOIN archive on archive_id = archive.id
333                    WHERE dvd_nr = ?
334    });
335    
336  my $sth_archive_burned = $dbh->prepare( qq{  my $sth_archive_burned = $dbh->prepare( qq{
337          insert into archive_burned          insert into archive_burned
338                  (archive_id, iso_size, part, copy)                  (archive_id, iso_size, part, copy)
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          if ($sth_archive_backup_parts->rows == 0) {                  print "Working on DVD #$dvd_nr in $tmp_dir\n";
                 warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";  
                 delete_dvd( $dvd_nr );  
         }  
357    
358          if ($prompt_for_delete) {                  $sth_archive_backup_parts->execute($dvd_nr);
                 next if delete_dvd( $dvd_nr );  
         }  
359    
360          my @volumes;                  $sth_archive_backup_check->execute($dvd_nr);
361          my $v;  # emtpy volume  
362          my $v_size = 0;                  my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
363    
364          my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";                  if ($parts_nr != $check_nr) {
365          while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {                          warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",
366                  if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {                                  ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",
367                          push @volumes, $v;                                  " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";
368                          $v = {};                          delete_dvd( $dvd_nr );
369                            next;
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)->{bavail} || die "can't get free space on $iso_dir";
410                          $volume_suffix = '_' . $volume_nr;                  $df *= 1024;    # calulate space in bytes
411                          $disk_name .= ' ' . $volume_nr . '/' . $volumes;                  if ($df < $v_size) {
412                            warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( $df ), " on $iso_dir]\n";
413                            next;
414                  }                  }
415    
416                  print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";                  my $volumes = $#volumes + 1;
417                    my $volume_nr = 1;
418    
419                  my $iso_file = my $xml_file = my $stage =                  foreach my $v (@volumes) {
                         "${iso_dir}/${dvd_nr}";  
420    
421                  $iso_file .= $volume_suffix . '.iso';                          #print Dumper($v);
                 $xml_file .= '.xml';  
422    
423                  $stage .= $volume_suffix . '.stage';                          my $iso_size = 0;
424                            my $disk_name = $dvd_nr;
425                            # suffix added to multi-volume archives
426                            my $volume_suffix = '';
427    
428                  my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";                          if ($volumes > 1) {
429                                    $volume_suffix = '_' . $volume_nr;
430                                    $disk_name .= ' ' . $volume_nr . '/' . $volumes;
431                            }
432    
433                  #                          print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
                 # check if ISO file exists  
                 #  
434    
435                  if (! -e $iso_file) {                          my $iso_file = my $xml_file = my $stage =
436                                    "${iso_dir}/${dvd_nr}";
437    
438                          # create stage directory                          $iso_file .= $volume_suffix . '.iso';
439                          if (-e $stage) {                          $xml_file .= '.xml';
440                                  rmtree($stage) || die "can't remove $stage: $!";  
441                          }                          $stage .= $volume_suffix . '.stage';
                         mkpath($stage);  
442    
443                          # open file for md5sums                          my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
                         open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";  
444    
445                          my $parts_on_this_volume = 0;                          #
446                            # check if ISO file exists
447                            #
448    
449                          foreach my $p (@{ $v->{parts} }) {                          if (! -e $iso_file) {
                                 my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);  
                                 my $rel_path = $tar_file;  
450    
451                                  if (-d "$tar_dir/$rel_path") {                                  # create stage directory
452                                          mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");                                  if (-e $stage) {
453                                          $rel_path .= '/' . $p->{part_nr};                                          rmtree($stage) || die "can't remove $stage: $!";
454                                  }                                  }
455                                  $rel_path .= '.tar.gz';                                  mkpath($stage);
456    
457                                  skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");                                  # open file for md5sums
458                                    open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
459    
460                                  add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");                                  my $parts_on_this_volume = 0;
461    
462                                  my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);                                  foreach my $p (@{ $v->{parts} }) {
463                                  chomp($md5sum);                                          my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
464                                  print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";                                          my $rel_path = $tar_file;
465    
466                                  $parts_on_this_volume++;                                          if (-d "$tar_dir/$rel_path") {
467                          }                                                  mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
468                                                    $rel_path .= '/' . $p->{part_nr};
469                                            }
470                                            $rel_path .= '.tar.gz';
471    
472                          # 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);  
473    
474                          add_symlink($xml_file, "$stage/${dvd_nr}.xml");                                          add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
475    
476                          # add css file for archive                                          my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
477                          my $css_file = $Conf{CgiImageDir} . '/archive.css';                                          chomp($md5sum);
478                          if (-r $css_file) {                                          print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
                                 add_symlink($css_file, "$stage/archive.css");  
                         } else {  
                                 print "WARNING: missing $css_file, not added to iso image!\n";  
                         }  
479    
480                          print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";                                          $parts_on_this_volume++;
481                                    }
482    
483                          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 file list and note in xml
484                                    dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
485    
486                          system($cmd) == 0 or skip "can't run $cmd: $?";                                  add_symlink($xml_file, "$stage/${dvd_nr}.xml");
487    
488                          rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";                                  # add css file for archive
489                                    my $css_file = $Conf{CgiImageDir} . '/archive.css';
490                                    if (-r $css_file) {
491                                            add_symlink($css_file, "$stage/archive.css");
492                                    } else {
493                                            print "WARNING: missing $css_file, not added to iso image!\n";
494                                    }
495    
496                          $iso_size = (stat($iso_file))[7];                                  print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
497    
498                          print "Created $iso_file [$iso_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}.tmp -f $stage };
499    
500                          rmtree($stage) || warn "can't remove stage directory $stage: $!";                                  system($cmd) == 0 or skip "can't run $cmd: $?";
501    
502                  } else {                                  rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
                         print "ISO $iso_file allready exists\n";  
                 }  
503    
504                  my $copies = $Conf{BurnMultipleCopies} || 1;                                  $iso_size = (stat($iso_file))[7];
505    
506                  foreach my $copy_nr ( 1 .. $copies ) {                                  print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
507    
508                                    rmtree($stage) || warn "can't remove stage directory $stage: $!";
509    
510                            } else {
511                                    $iso_size = (stat($iso_file))[7];
512                                    print "ISO $iso_file allready exists [$iso_size bytes]\n";
513                            }
514    
515                          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";
516    
# Line 482  foreach my $arc (@archives_to_burn) { Line 529  foreach my $arc (@archives_to_burn) {
529                          $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);                          $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
530    
531                          print "Media burn for $disk_name copy $copy_nr recorded\n";                          print "Media burn for $disk_name copy $copy_nr recorded\n";
532    
533                            if ($copy_nr >= $copies) {
534                                    print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
535                                    foreach my $f (( $xml_file, $iso_file )) {
536                                            print STDERR "\t$f ";
537                                            unlink $f || die "can't remove $f: $!";
538                                            print STDERR "removed\n";
539                                    }
540                            }
541    
542                            $volume_nr++;
543                  }                  }
544    
                 $volume_nr++;  
545          }          }
546    
547  SKIP:  SKIP:
# Line 499  print "Recoding finished, exiting...\n"; Line 556  print "Recoding finished, exiting...\n";
556  $sth->finish;  $sth->finish;
557  $sth_archive_backup_parts->finish;  $sth_archive_backup_parts->finish;
558  $sth_archive_burned->finish;  $sth_archive_burned->finish;
559    $sth_archive_backup_check->finish;
560    $sth_archive_burned->finish;
561    
562  $dbh->disconnect;  $dbh->disconnect;
563    

Legend:
Removed from v.288  
changed lines
  Added in v.341

  ViewVC Help
Powered by ViewVC 1.1.26