/[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 188 by dpavlin, Wed Oct 12 16:49:02 2005 UTC revision 288 by dpavlin, Sun Jan 15 14:50:39 2006 UTC
# Line 1  Line 1 
1  #!/usr/local/bin/perl  #!/usr/bin/perl
2    
3  use strict;  use strict;
4  #use lib "__INSTALLDIR__/lib";  use lib "__INSTALLDIR__/lib";
 # FIXME  
 use lib "/data/backuppc/lib";  
5    
6  use DBI;  use DBI;
7  use BackupPC::Lib;  use BackupPC::Lib;
# Line 11  use BackupPC::SearchLib; Line 9  use BackupPC::SearchLib;
9  use Time::HiRes qw/time/;  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;
13    use File::Path;
14    
15  use Data::Dumper;  use Data::Dumper;
16    
17  my $debug = 0;  my $debug = 0;
18    # set this to 1 to prompt for DVD removal for each selected item.
19    my $prompt_for_delete = 0;
20  $|=1;  $|=1;
21    
 my $start_t = time();  
   
 my $t_fmt = '%Y-%m-%d %H:%M:%S';  
   
22  # don't check for user  # don't check for user
23  my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;  my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
24  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
25  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
26    
27    my $conf_bin;
28    
29    $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
30    my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
31    $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
32    my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
33    $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
34    
35    
36    my $bin;
37    foreach my $c (qw/cdrecord eject mkisofs/) {
38            $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
39    }
40    
41    my $start_t = time();
42    
43    my $t_fmt = '%Y-%m-%d %H:%M:%S';
44    
45    
46  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
47  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
48    
# Line 58  sub curr_time { Line 75  sub curr_time {
75  sub dumpArchive2XML($$$)  sub dumpArchive2XML($$$)
76  {  {
77          my ($dbh, $dvd_nr, $filename) = @_;          my ($dbh, $dvd_nr, $filename) = @_;
78          my %archive;              my %archive;
79          my $output = new IO::File(">$filename");  
80            my $output = new IO::File('> '.$filename.'.tmp');
81          my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);          my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
82    
83            print "Dumping file list for DVD $dvd_nr, countdown:";
84    
85            $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
86    
87          my $files_sql = q{          my $files_sql = q{
88                  SELECT                  SELECT
89                          files.path AS path,                          files.path AS path,
# Line 97  sub dumpArchive2XML($$$) Line 119  sub dumpArchive2XML($$$)
119    
120          my $row_h;          my $row_h;
121          foreach my $hide (qw/id note/) {          foreach my $hide (qw/id note/) {
122                  $row_h->{$hide} = $row->{$hide} || die "no $hide";                  $row_h->{$hide} = $row->{$hide} || '';
123                  delete $row->{$hide};                  delete $row->{$hide};
124          }          }
125    
# Line 110  sub dumpArchive2XML($$$) Line 132  sub dumpArchive2XML($$$)
132          my $sth_backups = $dbh->prepare( $backups_sql );          my $sth_backups = $dbh->prepare( $backups_sql );
133          $sth_backups->execute( $row_h->{'id'} );          $sth_backups->execute( $row_h->{'id'} );
134    
135            my $total_backups = $sth_backups->rows;
136            my $curr_backup = 0;
137            my $last_pcnt = 0;
138    
139          while (my $row = $sth_backups->fetchrow_hashref()) {          while (my $row = $sth_backups->fetchrow_hashref()) {
140    
141                    $curr_backup++;
142                    my $pcnt = int(($curr_backup * 10) / $total_backups);
143                    if ($pcnt > $last_pcnt) {
144                            print " ",(10 - $pcnt);
145                            $last_pcnt = $pcnt;
146                    }
147    
148                  my $sth_files = $dbh->prepare( $files_sql);                  my $sth_files = $dbh->prepare( $files_sql);
149                  $sth_files->execute($row->{'backup_id'});                  $sth_files->execute($row->{'backup_id'});
150    
151                  delete($row->{'backup_id'});                  delete($row->{'backup_id'});
152                    $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
153    
154                  $writer->startTag( "backup", %{$row} );                  $writer->startTag( "backup", %{$row} );
155    
156                  while (my $row = $sth_files->fetchrow_hashref()) {                  while (my $row = $sth_files->fetchrow_hashref()) {
157                          # convert filetype to string                          # convert filetype to string
158                          $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});                          $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
159                            $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
160                          my $path = $row->{'path'}; delete $row->{'path'};                          my $path = $row->{'path'}; delete $row->{'path'};
161    
162                          $writer->startTag("file", %{$row});                          $writer->startTag("file", %{$row});
# Line 133  sub dumpArchive2XML($$$) Line 168  sub dumpArchive2XML($$$)
168                                                    
169          $writer->endTag("archive");          $writer->endTag("archive");
170          $writer->end();          $writer->end();
171            
172            rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
173    
174            print "\n";
175  }  }
176    
177    
# Line 212  sub skip($) { Line 250  sub skip($) {
250          goto SKIP;          goto SKIP;
251  }  }
252    
253  my $sth_archive_backup = $dbh->prepare( qq{  sub add_symlink($$) {
254            my ($from, $to) = @_;
255            symlink $from, $to || skip("can't symlink $from to $to: $!");
256    }
257    
258    sub delete_dvd($) {
259            my $dvd_nr = shift || return;
260    
261            print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
262            my $ok = <STDIN>;
263            chomp($ok);
264            if (lc($ok) eq 'yes') {
265                    print "Deleting DVD #$dvd_nr from database...\n";
266    
267                    $dbh->begin_work;
268    
269                    my $sth_delete_dvd = $dbh->prepare( qq{
270                            delete from archive where dvd_nr = ?
271                    } );
272                    $sth_delete_dvd->execute( $dvd_nr );
273                    $dbh->do( qq{
274                            select setval('dvd_nr', (select max(dvd_nr) from archive), true)
275                    } );
276    
277                    # remove files for this DVD
278                    map {
279                            print "\tremoving $_\n";
280                            unlink($_) || die "can't rm $_: $!";
281                    } glob ( "/$iso_dir/$dvd_nr.*" );
282    
283                    $dbh->commit;
284    
285                    return 1;
286            } else {
287                    return 0;
288            }
289    }
290    
291    my $sth_archive_backup_parts = $dbh->prepare( qq{
292          select          select
293                  backup_id,                  archive_backup.backup_id,
294                  archive_id,                  archive_id,
295                  hosts.name as host,                  hosts.name as host,
296                  shares.name as share,                  shares.name as share,
297                  backups.num as num                  backups.num as num,
298                    backups.parts as parts,
299                    backup_parts.part_nr as part_nr,
300                    backup_parts.size as part_size,
301                    backup_parts.md5 as md5,
302                    backup_parts.items
303          from archive_backup          from archive_backup
304          join archive on archive_id = archive.id          join archive on archive_id = archive.id
305          join backups on backup_id = backups.id          join backups on archive_backup.backup_id = backups.id
306          join hosts on hostid = hosts.id          join hosts on hostid = hosts.id
307          join shares on shareid = shares.id          join shares on shareid = shares.id
308            join backup_parts on archive_backup.backup_id = backup_parts.backup_id
309          where archive.dvd_nr = ?          where archive.dvd_nr = ?
310            order by archive_backup.backup_id, backup_parts.part_nr
311  });  });
312    
313  my $sth_archive_burned = $dbh->prepare( qq{  my $sth_archive_burned = $dbh->prepare( qq{
314          insert into archive_burned          insert into archive_burned
315                  (archive_id, iso_size)                  (archive_id, iso_size, part, copy)
316          values ( (select id from archive where dvd_nr =?), ?)          values ( (select id from archive where dvd_nr =?), ?, ?, ?)
317  });  });
318    
319  foreach my $arc (@archives_to_burn) {  foreach my $arc (@archives_to_burn) {
# Line 245  foreach my $arc (@archives_to_burn) { Line 328  foreach my $arc (@archives_to_burn) {
328    
329          print "Working on DVD #$dvd_nr in $tmp_dir\n";          print "Working on DVD #$dvd_nr in $tmp_dir\n";
330    
331          my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";          $sth_archive_backup_parts->execute($dvd_nr);
332          $list_file .= '.list';  
333          $iso_file .= '.iso';          if ($sth_archive_backup_parts->rows == 0) {
334          $xml_file .= '.xml';                  warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
335                    delete_dvd( $dvd_nr );
         if (-e $iso_file) {  
                 print "ISO $iso_file allready exists\n";  
                 goto BURN;  
336          }          }
337    
338          $sth_archive_backup->execute($dvd_nr);          if ($prompt_for_delete) {
339                    next if delete_dvd( $dvd_nr );
340            }
341    
342          open(my $list, "> $list_file") || skip "can't open $list_file: $!";          my @volumes;
343            my $v;  # emtpy volume
344            my $v_size = 0;
345    
346            my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
347            while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
348                    if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
349                            push @volumes, $v;
350                            $v = {};
351                    }
352                    $v->{size} += $row->{part_size};
353                    # this part
354                    my $p = {
355                            filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
356                    };
357                    foreach my $fld (qw/part_nr md5/) {
358                            $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
359                    }
360                    push @{ $v->{parts} }, $p;
361            }
362            push @volumes, $v if ($v);
363    
364          my $inc = 0;          #warn "# volumes: ",Dumper(\@volumes),"\n";
365    
366          while (my $row = $sth_archive_backup->fetchrow_hashref) {          my $volumes = $#volumes + 1;
367                  my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $volume_nr = 1;
                 skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");  
                 print $list "$tar_dir/$tar_file\n";  
                 $inc++;  
368    
369          }          foreach my $v (@volumes) {
370    
371          # FIXME add file list in xml and txt and note file                  #print Dumper($v);
372    
373          dumpArchive2XML($dbh, $dvd_nr, $xml_file);                  my $iso_size = 0;
374          print $list "$xml_file\n";                  my $disk_name = $dvd_nr;
375                    # suffix added to multi-volume archives
376                    my $volume_suffix = '';
377    
378          close($list);                  if ($volumes > 1) {
379                            $volume_suffix = '_' . $volume_nr;
380                            $disk_name .= ' ' . $volume_nr . '/' . $volumes;
381                    }
382    
383          print "Running mkisofs now for $inc increments...\n";                  print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";
384    
385          my $cmd = qq{ mkisofs -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $dvd_nr -o $iso_file -path-list $list_file };                  my $iso_file = my $xml_file = my $stage =
386                            "${iso_dir}/${dvd_nr}";
387    
388          system($cmd) == 0 or skip "can't run $cmd: $?";                  $iso_file .= $volume_suffix . '.iso';
389                    $xml_file .= '.xml';
390    
391          my $size = (stat($iso_file))[7];                  $stage .= $volume_suffix . '.stage';
392    
393          print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n";                  my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
394    
395  BURN:                  #
396          # FIXME add call to cdrecord here!                  # check if ISO file exists
397          $sth_archive_burned->execute($dvd_nr, $size);                  #
398          print "Media burn for $dvd_nr recorded\n";  
399                    if (! -e $iso_file) {
400    
401                            # create stage directory
402                            if (-e $stage) {
403                                    rmtree($stage) || die "can't remove $stage: $!";
404                            }
405                            mkpath($stage);
406    
407                            # open file for md5sums
408                            open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
409    
410                            my $parts_on_this_volume = 0;
411    
412                            foreach my $p (@{ $v->{parts} }) {
413                                    my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
414                                    my $rel_path = $tar_file;
415    
416                                    if (-d "$tar_dir/$rel_path") {
417                                            mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
418                                            $rel_path .= '/' . $p->{part_nr};
419                                    }
420                                    $rel_path .= '.tar.gz';
421    
422                                    skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
423    
424                                    add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
425    
426                                    my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
427                                    chomp($md5sum);
428                                    print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
429    
430                                    $parts_on_this_volume++;
431                            }
432    
433                            # add file list and note in xml
434                            dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
435    
436                            add_symlink($xml_file, "$stage/${dvd_nr}.xml");
437    
438                            # add css file for archive
439                            my $css_file = $Conf{CgiImageDir} . '/archive.css';
440                            if (-r $css_file) {
441                                    add_symlink($css_file, "$stage/archive.css");
442                            } else {
443                                    print "WARNING: missing $css_file, not added to iso image!\n";
444                            }
445    
446                            print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
447    
448                            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 };
449    
450                            system($cmd) == 0 or skip "can't run $cmd: $?";
451    
452                            rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
453    
454                            $iso_size = (stat($iso_file))[7];
455    
456                            print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
457    
458                            rmtree($stage) || warn "can't remove stage directory $stage: $!";
459    
460                    } else {
461                            print "ISO $iso_file allready exists\n";
462                    }
463    
464                    my $copies = $Conf{BurnMultipleCopies} || 1;
465    
466                    foreach my $copy_nr ( 1 .. $copies ) {
467    
468                            print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
469    
470                            system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
471    
472                            my $wait = <STDIN>;
473    
474                            my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
475    
476                            # FIXME
477                            print "## $cmd\n";
478                            system($cmd) == 0 or skip "can't run $cmd: $?";
479    
480                            print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
481    
482                            $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
483    
484                            print "Media burn for $disk_name copy $copy_nr recorded\n";
485                    }
486    
487                    $volume_nr++;
488            }
489    
490  SKIP:  SKIP:
491    
# Line 298  SKIP: Line 494  SKIP:
494  #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});  #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
495  #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";  #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
496    
497    print "Recoding finished, exiting...\n";
498    
499  $sth->finish;  $sth->finish;
500    $sth_archive_backup_parts->finish;
501    $sth_archive_burned->finish;
502    
503  $dbh->disconnect;  $dbh->disconnect;
504    

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

  ViewVC Help
Powered by ViewVC 1.1.26