/[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 276 by dpavlin, Tue Dec 13 22:55:59 2005 UTC revision 372 by iklaric, Mon May 7 11:00:52 2007 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/local/bin/perl
2    
3  use strict;  use strict;
4  #use lib "__INSTALLDIR__/lib";  use lib "/data/backuppc-agi//lib";
 use lib "/data/backuppc-agi/lib";  
5    
6  use DBI;  use DBI;
7  use BackupPC::Lib;  use BackupPC::Lib;
# Line 12  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    use Cwd qw/abs_path/;
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 31  $conf_bin->{'eject'} = $Conf{ejectBin} | Line 33  $conf_bin->{'eject'} = $Conf{ejectBin} |
33  my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";  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";  $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
35    
36    my $path = abs_path($0);
37    $path =~ s#/[^/]+$#/#;
38    my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
39    
40  my $bin;  my $bin;
41  foreach my $c (qw/cdrecord eject mkisofs/) {  foreach my $c (qw/cdrecord eject mkisofs/) {
# Line 110  sub dumpArchive2XML($$$) Line 115  sub dumpArchive2XML($$$)
115                          AND hosts.id=backups.hostid                          AND hosts.id=backups.hostid
116                          AND shares.id=backups.shareid                          AND shares.id=backups.shareid
117                          AND archive_backup.archive_id = ?                          AND archive_backup.archive_id = ?
118                    ORDER BY
119                            hosts.name, shares.name, backups.num
120          };          };
121    
122          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 163  sub dumpArchive2XML($$$) Line 170  sub dumpArchive2XML($$$)
170                          $writer->endTag("file");                          $writer->endTag("file");
171                  }                  }
172                  $writer->endTag("backup");                  $writer->endTag("backup");
173          }                }
174                                                    
175          $writer->endTag("archive");          $writer->endTag("archive");
176          $writer->end();          $writer->end();
# Line 190  order by date asc Line 197  order by date asc
197    
198  $sth->execute();  $sth->execute();
199    
200    sub fmt_mb($) {
201            my $s = shift;
202            die "missing size" unless defined($s);
203            $s /= (1024*1024);
204            return sprintf("%.2f Mb", $s);
205    }
206    
207  sub fmt_archive($) {  sub fmt_archive($) {
208          my $row = shift || die;          my $row = shift || die;
209    
210          $row->{'date'} =~ s/\.\d+$//;          $row->{'date'} =~ s/\.\d+$//;
211          $row->{'copies'} =~ s/^\s*0+\s*$/no/;          $row->{'copies'} =~ s/^\s*0+\s*$/no/;
         $row->{'total_size'} /= (1024*1024);  
212    
213          my $copies = 'copies';          my $copies = 'copies';
214          $copies = 'copy' if ($row->{'copies'} == 1);          $copies = 'copy' if ($row->{'copies'} == 1);
215    
216          return          return
217                  sprintf("%d by %s on %s, %s %s [%.2f Mb]",                  sprintf("%d by %s on %s, %s %s [%s]",
218                          $row->{'dvd_nr'},                          $row->{'dvd_nr'},
219                          $row->{'username'},                          $row->{'username'},
220                          $row->{'date'},                          $row->{'date'},
221                          $row->{'copies'}, $copies,                          $row->{'copies'}, $copies,
222                          $row->{'total_size'},                          fmt_mb($row->{'total_size'}),
223                  );                  );
224  }  }
225    
# Line 254  sub add_symlink($$) { Line 267  sub add_symlink($$) {
267          symlink $from, $to || skip("can't symlink $from to $to: $!");          symlink $from, $to || skip("can't symlink $from to $to: $!");
268  }  }
269    
270    sub delete_dvd($) {
271            my $dvd_nr = shift || return;
272    
273            print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
274            my $ok = <STDIN>;
275            chomp($ok);
276            if (lc($ok) eq 'yes') {
277                    print "Deleting DVD #$dvd_nr from database...\n";
278    
279                    $dbh->begin_work;
280    
281                    my $sth_delete_dvd = $dbh->prepare( qq{
282                            delete from archive where dvd_nr = ?
283                    } );
284                    $sth_delete_dvd->execute( $dvd_nr );
285                    $dbh->do( qq{
286                            select setval('dvd_nr', (select max(dvd_nr) from archive), true)
287                    } );
288    
289                    # remove files for this DVD
290                    map {
291                            if (-d $_) {
292                                    print "\tremoving dir $_\n";
293                                    rmtree($_) || die "can't rmtree $_: $!";
294                            } else {
295                                    print "\tremoving $_\n";
296                                    unlink($_) || die "can't rm $_: $!";
297                            }
298                    } glob ( "/$iso_dir/$dvd_nr.*" );
299    
300                    $dbh->commit;
301    
302                    return 1;
303            } else {
304                    return 0;
305            }
306    }
307    
308  my $sth_archive_backup_parts = $dbh->prepare( qq{  my $sth_archive_backup_parts = $dbh->prepare( qq{
309          select          select
310                  archive_backup.backup_id,                  archive_backup.backup_id,
# Line 276  my $sth_archive_backup_parts = $dbh->pre Line 327  my $sth_archive_backup_parts = $dbh->pre
327          order by archive_backup.backup_id, backup_parts.part_nr          order by archive_backup.backup_id, backup_parts.part_nr
328  });  });
329    
330    my $sth_archive_backup_check = $dbh->prepare( qq{
331                    SELECT
332                            sum(backups.parts)
333                    FROM backups
334                    JOIN archive_backup on archive_backup.backup_id = backups.id
335                    JOIN archive on archive_id = archive.id
336                    WHERE dvd_nr = ?
337    });
338    
339  my $sth_archive_burned = $dbh->prepare( qq{  my $sth_archive_burned = $dbh->prepare( qq{
340          insert into archive_burned          insert into archive_burned
341                  (archive_id, iso_size, part, copy)                  (archive_id, iso_size, part, copy)
342          values ( (select id from archive where dvd_nr =?), ?, ?, ?)          values ( (select id from archive where dvd_nr =?), ?, ?, ?)
343  });  });
344    
345  foreach my $arc (@archives_to_burn) {  my $copies = $Conf{BurnMultipleCopies} || 1;
         exit if ($arc eq ']quit[');  
346    
347          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);  
348    
349          my $tmp_dir = "/$iso_dir/$dvd_nr";          foreach my $arc (@archives_to_burn) {
350                    exit if ($arc eq ']quit[');
351    
352          my $t = time();                  my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
353                    die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
354    
355          print "Working on DVD #$dvd_nr in $tmp_dir\n";                  my $tmp_dir = "/$iso_dir/$dvd_nr";
356    
357          $sth_archive_backup_parts->execute($dvd_nr);                  my $t = time();
358    
359          my @volumes;                  print "Working on DVD #$dvd_nr in $tmp_dir\n";
         my $v;  # emtpy volume  
         my $v_size = 0;  
360    
361          my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";                  $sth_archive_backup_parts->execute($dvd_nr);
362          while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {  
363                  if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {                  $sth_archive_backup_check->execute($dvd_nr);
                         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);  
364    
365          my $volumes = $#volumes + 1;                  my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
         my $volume_nr = 1;  
366    
367          foreach my $v (@volumes) {                  if ($parts_nr != $check_nr) {
368                            warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",
369                                    ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",
370                                    " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";
371                            delete_dvd( $dvd_nr );
372                            next;
373                    }
374    
375                    if ($sth_archive_backup_parts->rows == 0) {
376                            warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
377                            next if delete_dvd( $dvd_nr );
378                    }
379    
380                  print Dumper($v);                  if ($prompt_for_delete) {
381                            next if delete_dvd( $dvd_nr );
382                    }
383    
384                  my $iso_size = 0;                  my @volumes;
385                  my $disk_name = $dvd_nr;                  my $v;  # emtpy volume
386                  # suffix added to multi-volume archives                  my $v_size = 0;
387                  my $volume_suffix = $dvd_nr;  
388                    my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
389                    while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
390                            if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
391                                    push @volumes, $v;
392                                    $v = {};
393                            }
394                            $v->{size} += $row->{part_size};
395                            $v_size += $row->{part_size};
396    
397                  if ($volumes > 1) {                          # this part
398                          $volume_suffix = '_' . $volume_nr;                          my $p = {
399                          $disk_name .= ' ' . $volume_nr . '/' . $volumes;                                  filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
400                                    host => $row->{'host'},
401                                    share => $row->{'share'},
402                                    num => $row->{'num'}
403                            };
404                            foreach my $fld (qw/part_nr md5/) {
405                                    $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
406                            }
407                            push @{ $v->{parts} }, $p;
408                  }                  }
409                    push @volumes, $v if ($v);
410    
411                  print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";                  #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
412    
413                  my $iso_file = my $xml_file = my $stage =                  # check available disk space
                         "${iso_dir}/${dvd_nr}";  
414    
415                  $iso_file .= $volume_suffix . '.iso';                  my $df = df($iso_dir)->{bavail} || die "can't get free space on $iso_dir";
416                  $xml_file .= '.xml';                  $df *= 1024;    # calulate space in bytes
417                    if ($df < $v_size) {
418                            warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( $df ), " on $iso_dir]\n";
419                            next;
420                    }
421    
422                  $stage .= $volume_suffix . '.stage';                  my $volumes = $#volumes + 1;
423                    my $volume_nr = 1;
424    
425                  my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";                  foreach my $v (@volumes) {
426    
427                  #                          #print Dumper($v);
                 # check if ISO file exists  
                 #  
428    
429                  if (! -e $iso_file) {                          my $iso_size = 0;
430                            my $disk_name = $dvd_nr;
431                            # suffix added to multi-volume archives
432                            my $volume_suffix = '';
433    
434                          # create stage directory                          if ($volumes > 1) {
435                          if (-e $stage) {                                  $volume_suffix = '_' . $volume_nr;
436                                  rmtree($stage) || die "can't remove $stage: $!";                                  $disk_name .= ' ' . $volume_nr . '/' . $volumes;
437                          }                          }
                         mkpath($stage);  
438    
439                          # open file for md5sums                          print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
                         open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";  
440    
441                          my $parts_on_this_volume = 0;                          my $iso_file = my $xml_file = my $stage =
442                                    "${iso_dir}/${dvd_nr}";
443    
444                          foreach my $p (@{ $v->{parts} }) {                          $iso_file .= $volume_suffix . '.iso';
445                                  my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);                          $xml_file .= '.xml';
                                 my $rel_path = $tar_file;  
446    
447                                  if (-d "$tar_dir/$rel_path") {                          $stage .= $volume_suffix . '.stage';
                                         $rel_path .= '/' . $p->{part_nr};  
                                         mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");  
                                 }  
                                 $rel_path .= '.tar.gz';  
448    
449                                  skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");                          my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
450    
451                                  add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");                          #
452                            # check if ISO file exists
453                            #
454    
455                                  my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);                          if (! -e $iso_file) {
                                 chomp($md5sum);  
                                 print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";  
456    
457                                  $parts_on_this_volume++;                                  # create stage directory
458                          }                                  if (-e $stage) {
459                                            rmtree($stage) || die "can't remove $stage: $!";
460                                    }
461                                    mkpath($stage);
462    
463                          # add file list and note in xml                                  # open file for md5sums
464                          dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);                                  open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
465    
466                          add_symlink($xml_file, "$stage/${dvd_nr}.xml");                                  my $parts_on_this_volume = 0;
467    
468                          # add css file for archive                                  foreach my $p (@{ $v->{parts} }) {
469                          my $css_file = $Conf{CgiImageDir} . '/archive.css';                                          my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
470                          if (-r $css_file) {                                          my $rel_path = $tar_file;
471                                  add_symlink($css_file, "$stage/archive.css");  
472                          } else {                                          if (-d "$tar_dir/$rel_path") {
473                                  print "WARNING: missing $css_file, not added to iso image!\n";                                                  mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
474                          }                                                  $rel_path .= '/' . $p->{part_nr};
475                                            }
476                                            $rel_path .= '.tar.gz';
477    
478                                            
479                                            unless (-r "$tar_dir/$rel_path") {
480                                                    print "WARNING: can't find increment $rel_path , trying to recreate it using BackupPC_tarIncCreate...\n";
481                                                    my $host = $p->{host};
482                                                    my $share = $p->{share};
483                                                    my $dump = $p->{num};
484                                                    my $otherUser = "backuppc-agi";
485    
486                                                    my $cmd = "sudo -u $otherUser ".$tarIncCreate. " -h $host -s $share -n $dump";
487                                                    print "$cmd ";
488                                                    if (system($cmd) != 0) {
489                                                            print " FAILED.\n";
490                                                    } else {
491                                                            print " done.\n";
492                                                    }
493                                            }
494    
495                                            skip "can't find increment $rel_path, recreateing obviously did not work: $!" unless (-r "$tar_dir/$rel_path");
496                                            
497                                            add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
498    
499                                            my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
500                                            chomp($md5sum);
501                                            print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
502    
503                          print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";                                          $parts_on_this_volume++;
504                                    }
505    
506                          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
507                                    dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
508    
509                          system($cmd) == 0 or skip "can't run $cmd: $?";                                  add_symlink($xml_file, "$stage/${dvd_nr}.xml");
510    
511                          rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";                                  # add css file for archive
512                                    my $css_file = $Conf{CgiImageDir} . '/archive.css';
513                                    if (-r $css_file) {
514                                            add_symlink($css_file, "$stage/archive.css");
515                                    } else {
516                                            print "WARNING: missing $css_file, not added to iso image!\n";
517                                    }
518    
519                          $iso_size = (stat($iso_file))[7];                                  print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
520    
521                          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 };
522    
523                          # FIXME                                  system($cmd) == 0 or skip "can't run $cmd: $?";
                         #rmtree($stage) || warn "can't remove stage directory $stage: $!";  
524    
525                  } else {                                  rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
526                          print "ISO $iso_file allready exists\n";  
527                  }                                  $iso_size = (stat($iso_file))[7];
528    
529                  my $copies = $Conf{BurnMultipleCopies} || 1;                                  print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
530    
531                  foreach my $copy_nr ( 1 .. $copies ) {                                  rmtree($stage) || warn "can't remove stage directory $stage: $!";
532    
533                            } else {
534                                    $iso_size = (stat($iso_file))[7];
535                                    print "ISO $iso_file already exists [$iso_size bytes]\n";
536                            }
537    
538                          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";
539    
# Line 438  foreach my $arc (@archives_to_burn) { Line 552  foreach my $arc (@archives_to_burn) {
552                          $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);                          $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
553    
554                          print "Media burn for $disk_name copy $copy_nr recorded\n";                          print "Media burn for $disk_name copy $copy_nr recorded\n";
555    
556                            if ($copy_nr >= $copies) {
557                                    print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
558                                    foreach my $f (( $xml_file, $iso_file )) {
559                                            print STDERR "\t$f ";
560                                            unlink $f || die "can't remove $f: $!";
561                                            print STDERR "removed\n";
562                                    }
563                            }
564    
565                            $volume_nr++;
566                  }                  }
567    
                 $volume_nr++;  
568          }          }
569    
570  SKIP:  SKIP:
# Line 455  print "Recoding finished, exiting...\n"; Line 579  print "Recoding finished, exiting...\n";
579  $sth->finish;  $sth->finish;
580  $sth_archive_backup_parts->finish;  $sth_archive_backup_parts->finish;
581  $sth_archive_burned->finish;  $sth_archive_burned->finish;
582    $sth_archive_backup_check->finish;
583    $sth_archive_burned->finish;
584    
585  $dbh->disconnect;  $dbh->disconnect;
586    

Legend:
Removed from v.276  
changed lines
  Added in v.372

  ViewVC Help
Powered by ViewVC 1.1.26