/[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 202 by dpavlin, Fri Oct 14 14:02:52 2005 UTC revision 339 by dpavlin, Wed Mar 1 13:40:44 2006 UTC
# Line 10  use Time::HiRes qw/time/; Line 10  use Time::HiRes qw/time/;
10  use POSIX qw/strftime/;  use POSIX qw/strftime/;
11  use Term::Menus;  use Term::Menus;
12  use File::Which;  use File::Which;
13    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.
20    my $prompt_for_delete = shift @ARGV;
21  $|=1;  $|=1;
22    
23  # don't check for user  # don't check for user
# Line 21  my $bpc = BackupPC::Lib->new(undef, unde Line 25  my $bpc = BackupPC::Lib->new(undef, unde
25  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
26  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
27    
28  my $cdrecord = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";  my $conf_bin;
29    
30    $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
31  my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";  my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
32    $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
33    my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
34    $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
35    
36    
37  my $bin;  my $bin;
38  foreach my $c (qw/mkisofs eject/, $cdrecord) {  foreach my $c (qw/cdrecord eject mkisofs/) {
39          $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";
40  }  }
41    
42  my $start_t = time();  my $start_t = time();
# Line 67  sub curr_time { Line 76  sub curr_time {
76  sub dumpArchive2XML($$$)  sub dumpArchive2XML($$$)
77  {  {
78          my ($dbh, $dvd_nr, $filename) = @_;          my ($dbh, $dvd_nr, $filename) = @_;
79          my %archive;              my %archive;
80          my $output = new IO::File(">$filename");  
81            my $output = new IO::File('> '.$filename.'.tmp');
82          my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);          my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
83    
84            print "Dumping file list for DVD $dvd_nr, countdown:";
85    
86          $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');          $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
87    
88          my $files_sql = q{          my $files_sql = q{
# Line 100  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 121  sub dumpArchive2XML($$$) Line 135  sub dumpArchive2XML($$$)
135          my $sth_backups = $dbh->prepare( $backups_sql );          my $sth_backups = $dbh->prepare( $backups_sql );
136          $sth_backups->execute( $row_h->{'id'} );          $sth_backups->execute( $row_h->{'id'} );
137    
138            my $total_backups = $sth_backups->rows;
139            my $curr_backup = 0;
140            my $last_pcnt = 0;
141    
142          while (my $row = $sth_backups->fetchrow_hashref()) {          while (my $row = $sth_backups->fetchrow_hashref()) {
143    
144                    $curr_backup++;
145                    my $pcnt = int(($curr_backup * 10) / $total_backups);
146                    if ($pcnt > $last_pcnt) {
147                            print " ",(10 - $pcnt);
148                            $last_pcnt = $pcnt;
149                    }
150    
151                  my $sth_files = $dbh->prepare( $files_sql);                  my $sth_files = $dbh->prepare( $files_sql);
152                  $sth_files->execute($row->{'backup_id'});                  $sth_files->execute($row->{'backup_id'});
153    
# Line 142  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();
174            
175            rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
176    
177            print "\n";
178  }  }
179    
180    
# Line 166  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 225  sub skip($) { Line 259  sub skip($) {
259          goto SKIP;          goto SKIP;
260  }  }
261    
262  my $sth_archive_backup = $dbh->prepare( qq{  sub add_symlink($$) {
263            my ($from, $to) = @_;
264            symlink $from, $to || skip("can't symlink $from to $to: $!");
265    }
266    
267    sub delete_dvd($) {
268            my $dvd_nr = shift || return;
269    
270            print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
271            my $ok = <STDIN>;
272            chomp($ok);
273            if (lc($ok) eq 'yes') {
274                    print "Deleting DVD #$dvd_nr from database...\n";
275    
276                    $dbh->begin_work;
277    
278                    my $sth_delete_dvd = $dbh->prepare( qq{
279                            delete from archive where dvd_nr = ?
280                    } );
281                    $sth_delete_dvd->execute( $dvd_nr );
282                    $dbh->do( qq{
283                            select setval('dvd_nr', (select max(dvd_nr) from archive), true)
284                    } );
285    
286                    # remove files for this DVD
287                    map {
288                            if (-d $_) {
289                                    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.*" );
296    
297                    $dbh->commit;
298    
299                    return 1;
300            } else {
301                    return 0;
302            }
303    }
304    
305    my $sth_archive_backup_parts = $dbh->prepare( qq{
306          select          select
307                  backup_id,                  archive_backup.backup_id,
308                  archive_id,                  archive_id,
309                  hosts.name as host,                  hosts.name as host,
310                  shares.name as share,                  shares.name as share,
311                  backups.num as num,                  backups.num as num,
312                  backups.parts as parts                  backups.parts as parts,
313                    backup_parts.part_nr as part_nr,
314                    backup_parts.size as part_size,
315                    backup_parts.md5 as md5,
316                    backup_parts.items
317          from archive_backup          from archive_backup
318          join archive on archive_id = archive.id          join archive on archive_id = archive.id
319          join backups on backup_id = backups.id          join backups on archive_backup.backup_id = backups.id
320          join hosts on hostid = hosts.id          join hosts on hostid = hosts.id
321          join shares on shareid = shares.id          join shares on shareid = shares.id
322            join backup_parts on archive_backup.backup_id = backup_parts.backup_id
323          where archive.dvd_nr = ?          where archive.dvd_nr = ?
324            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{
# Line 247  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          my $part_path = '';                  my $t = time();
         my $part_nr = 1;  
         my $parts = 0;  
                   
         $sth_archive_backup->execute($dvd_nr);  
355    
356          my $disk_name = 'unknown disk';                  print "Working on DVD #$dvd_nr in $tmp_dir\n";
         my $iso_size = 0;  
357    
358          do {                  $sth_archive_backup_parts->execute($dvd_nr);
359    
360                  # do we need to re-execute query for multi-part increments?                  $sth_archive_backup_check->execute($dvd_nr);
                 $sth_archive_backup->execute($dvd_nr) if ($parts > 1);  
361    
362                  my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";                  my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
363    
364                    if ($parts_nr != $check_nr) {
365                            warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",
366                                    ($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                            delete_dvd( $dvd_nr );
369                            next;
370                    }
371    
372                  $parts =  $row->{'parts'} || die "no parts?";                  if ($sth_archive_backup_parts->rows == 0) {
373                  $disk_name = $dvd_nr;                          warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
374                            next if delete_dvd( $dvd_nr );
375                    }
376    
377                  if ($parts > 1) {                  if ($prompt_for_delete) {
378                          # parts start with 0, so we have -1 here                          next if delete_dvd( $dvd_nr );
                         $part_path = '.' . ($part_nr - 1);  
                         $disk_name .= '.' . $part_nr;  
379                  }                  }
380    
381                  print "Processing part $part_nr/$parts\n";                  my @volumes;
382                    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                  my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";                          # this part
395                  $list_file .= $part_path . '.list';                          my $p = {
396                  $iso_file .= $part_path . '.iso';                                  filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
397                  $xml_file .= '.xml';                          };
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                  #                  #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
                 # check if ISO file exists  
                 #  
406    
407                  if (! -e $iso_file) {                  # check available disk space
408    
409                          open(my $list, "> $list_file") || skip "can't open $list_file: $!";                  my $df = df($iso_dir);
410                    if ($df < $v_size) {
411                            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                          my $inc = 0;                  my $volumes = $#volumes + 1;
416                    my $volume_nr = 1;
417    
418                          do {                  foreach my $v (@volumes) {
                                 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 - 1));  
                                         print $list "$tar_dir/$tar_file/part$nr\n";  
                                 }  
                                 $inc++;  
                         } while ($row = $sth_archive_backup->fetchrow_hashref);  
419    
420                          # add file list and note in xml                          #print Dumper($v);
421                          dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);  
422                          print $list "$xml_file\n";                          my $iso_size = 0;
423                            my $disk_name = $dvd_nr;
424                          # add css file for archive                          # suffix added to multi-volume archives
425                          my $css_file = $Conf{CgiImageDir} . '/archive.css';                          my $volume_suffix = '';
426                          if (-r $css_file) {  
427                                  print $list "$css_file\n";                          if ($volumes > 1) {
428                          } else {                                  $volume_suffix = '_' . $volume_nr;
429                                  print "WARNING: missing $css_file, not added to iso image!\n";                                  $disk_name .= ' ' . $volume_nr . '/' . $volumes;
430                          }                          }
431    
432                          close($list);                          print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
433    
434                          print "Running mkisofs now for $inc increments, disk $disk_name\n";                          my $iso_file = my $xml_file = my $stage =
435                                    "${iso_dir}/${dvd_nr}";
436    
437                          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 };                          $iso_file .= $volume_suffix . '.iso';
438                            $xml_file .= '.xml';
439    
440                          system($cmd) == 0 or skip "can't run $cmd: $?";                          $stage .= $volume_suffix . '.stage';
441    
442                          $iso_size = (stat($iso_file))[7];                          my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
443    
444                          print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";                          #
445                            # check if ISO file exists
446                            #
447    
448                  } else {                          if (! -e $iso_file) {
449                          print "ISO $iso_file allready exists\n";  
450                  }                                  # create stage directory
451                                    if (-e $stage) {
452                                            rmtree($stage) || die "can't remove $stage: $!";
453                                    }
454                                    mkpath($stage);
455    
456                                    # open file for md5sums
457                                    open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
458    
459                                    my $parts_on_this_volume = 0;
460    
461                                    foreach my $p (@{ $v->{parts} }) {
462                                            my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
463                                            my $rel_path = $tar_file;
464    
465                                            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                                            skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
472    
473                                            add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
474    
475                                            my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
476                                            chomp($md5sum);
477                                            print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
478    
479                                            $parts_on_this_volume++;
480                                    }
481    
482                                    # add file list and note in xml
483                                    dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
484    
485                                    add_symlink($xml_file, "$stage/${dvd_nr}.xml");
486    
487                  my $copies = $Conf{BurnMultipleCopies} || 1;                                  # 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                                    print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
496    
497                                    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                                    system($cmd) == 0 or skip "can't run $cmd: $?";
500    
501                  foreach my $copy_nr ( 1 .. $copies ) {                                  rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
502    
503                                    $iso_size = (stat($iso_file))[7];
504    
505                                    print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
506    
507                                    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    
516                          system($bin->{'eject'}) == 0 or skip "can't run eject: $?";                          system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
517    
518                          my $wait = <STDIN>;                          my $wait = <STDIN>;
519    
520                          my $cmd = $bin->{$cdrecord} . ' ' . $cdr_opts . ' ' . $iso_file;                          my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
521    
522                          # FIXME                          # FIXME
523                          print "## $cmd\n";                          print "## $cmd\n";
# Line 361  foreach my $arc (@archives_to_burn) { Line 525  foreach my $arc (@archives_to_burn) {
525    
526                          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";
527    
528                          $sth_archive_burned->execute($dvd_nr, $iso_size, $part_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                            if ($copy_nr >= $copies) {
533                                    print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
534                                    foreach my $f (( $xml_file, $iso_file )) {
535                                            print STDERR "\t$f ";
536                                            unlink $f || die "can't remove $f: $!";
537                                            print STDERR "removed\n";
538                                    }
539                            }
540    
541                            $volume_nr++;
542                  }                  }
543    
544                  $part_nr++;          }
         } until ($part_nr > $parts);  
545    
546  SKIP:  SKIP:
547    
# Line 379  SKIP: Line 553  SKIP:
553  print "Recoding finished, exiting...\n";  print "Recoding finished, exiting...\n";
554    
555  $sth->finish;  $sth->finish;
556  $sth_archive_backup->finish;  $sth_archive_backup_parts->finish;
557    $sth_archive_burned->finish;
558    $sth_archive_backup_check->finish;
559  $sth_archive_burned->finish;  $sth_archive_burned->finish;
560    
561  $dbh->disconnect;  $dbh->disconnect;

Legend:
Removed from v.202  
changed lines
  Added in v.339

  ViewVC Help
Powered by ViewVC 1.1.26