/[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 181 by dpavlin, Wed Oct 12 10:34:26 2005 UTC revision 292 by dpavlin, Thu Jan 26 00:39:47 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 = shift @ARGV;
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 55  sub curr_time { Line 72  sub curr_time {
72          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
73  }  }
74    
75    sub dumpArchive2XML($$$)
76    {
77            my ($dbh, $dvd_nr, $filename) = @_;
78            my %archive;
79    
80            my $output = new IO::File('> '.$filename.'.tmp');
81            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{
88                    SELECT
89                            files.path AS path,
90                            files.date AS date,
91                            files.type AS type,
92                            files.size AS size
93                    FROM files, backups, shares
94                    WHERE files.backupnum=backups.num AND
95                            files.shareid=shares.id AND
96                            shares.hostid=backups.hostid AND
97                            backups.id=?
98            };
99    
100            my $backups_sql = q{
101                    SELECT
102                            backups.id   AS backup_id,
103                            hosts.name   AS host,
104                            backups.num  AS num,
105                            backups.date AS date,
106                            shares.name  AS share,
107                            backups.size AS backup_size,
108                            backups.inc_size AS compress_size
109                    FROM backups, archive_backup, hosts, shares
110                    WHERE archive_backup.backup_id = backups.id
111                            AND hosts.id=backups.hostid
112                            AND shares.id=backups.shareid
113                            AND archive_backup.archive_id = ?
114                    ORDER BY
115                            hosts.name, shares.name, backups.num
116            };
117    
118            my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
119            $sth->execute($dvd_nr);
120            my $row = $sth->fetchrow_hashref();
121    
122            my $row_h;
123            foreach my $hide (qw/id note/) {
124                    $row_h->{$hide} = $row->{$hide} || '';
125                    delete $row->{$hide};
126            }
127    
128            $writer->startTag("archive", %{$row});
129    
130            $writer->startTag("note");
131            $writer->characters( $row_h->{'note'} );
132            $writer->endTag("note");
133    
134            my $sth_backups = $dbh->prepare( $backups_sql );
135            $sth_backups->execute( $row_h->{'id'} );
136    
137            my $total_backups = $sth_backups->rows;
138            my $curr_backup = 0;
139            my $last_pcnt = 0;
140    
141            while (my $row = $sth_backups->fetchrow_hashref()) {
142    
143                    $curr_backup++;
144                    my $pcnt = int(($curr_backup * 10) / $total_backups);
145                    if ($pcnt > $last_pcnt) {
146                            print " ",(10 - $pcnt);
147                            $last_pcnt = $pcnt;
148                    }
149    
150                    my $sth_files = $dbh->prepare( $files_sql);
151                    $sth_files->execute($row->{'backup_id'});
152    
153                    delete($row->{'backup_id'});
154                    $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
155    
156                    $writer->startTag( "backup", %{$row} );
157    
158                    while (my $row = $sth_files->fetchrow_hashref()) {
159                            # convert filetype to string
160                            $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
161                            $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
162                            my $path = $row->{'path'}; delete $row->{'path'};
163    
164                            $writer->startTag("file", %{$row});
165                            $writer->characters( $path );
166                            $writer->endTag("file");
167                    }
168                    $writer->endTag("backup");
169            }
170                            
171            $writer->endTag("archive");
172            $writer->end();
173    
174            rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
175    
176            print "\n";
177    }
178    
179    
180    
181  #----- main  #----- main
182    
183  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 129  sub skip($) { Line 252  sub skip($) {
252          goto SKIP;          goto SKIP;
253  }  }
254    
255  my $sth_archive_backup = $dbh->prepare( qq{  sub add_symlink($$) {
256            my ($from, $to) = @_;
257            symlink $from, $to || skip("can't symlink $from to $to: $!");
258    }
259    
260    sub delete_dvd($) {
261            my $dvd_nr = shift || return;
262    
263            print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
264            my $ok = <STDIN>;
265            chomp($ok);
266            if (lc($ok) eq 'yes') {
267                    print "Deleting DVD #$dvd_nr from database...\n";
268    
269                    $dbh->begin_work;
270    
271                    my $sth_delete_dvd = $dbh->prepare( qq{
272                            delete from archive where dvd_nr = ?
273                    } );
274                    $sth_delete_dvd->execute( $dvd_nr );
275                    $dbh->do( qq{
276                            select setval('dvd_nr', (select max(dvd_nr) from archive), true)
277                    } );
278    
279                    # remove files for this DVD
280                    map {
281                            print "\tremoving $_\n";
282                            unlink($_) || die "can't rm $_: $!";
283                    } glob ( "/$iso_dir/$dvd_nr.*" );
284    
285                    $dbh->commit;
286    
287                    return 1;
288            } else {
289                    return 0;
290            }
291    }
292    
293    my $sth_archive_backup_parts = $dbh->prepare( qq{
294          select          select
295                  backup_id,                  archive_backup.backup_id,
296                  archive_id,                  archive_id,
297                  hosts.name as host,                  hosts.name as host,
298                  shares.name as share,                  shares.name as share,
299                  backups.num as num                  backups.num as num,
300                    backups.parts as parts,
301                    backup_parts.part_nr as part_nr,
302                    backup_parts.size as part_size,
303                    backup_parts.md5 as md5,
304                    backup_parts.items
305          from archive_backup          from archive_backup
306          join archive on archive_id = archive.id          join archive on archive_id = archive.id
307          join backups on backup_id = backups.id          join backups on archive_backup.backup_id = backups.id
308          join hosts on hostid = hosts.id          join hosts on hostid = hosts.id
309          join shares on shareid = shares.id          join shares on shareid = shares.id
310            join backup_parts on archive_backup.backup_id = backup_parts.backup_id
311          where archive.dvd_nr = ?          where archive.dvd_nr = ?
312            order by archive_backup.backup_id, backup_parts.part_nr
313    });
314    
315    my $sth_archive_backup_check = $dbh->prepare( qq{
316                    SELECT
317                            count(backups.id)
318                    FROM backups
319                    JOIN archive_backup on archive_backup.backup_id = backups.id
320                    JOIN archive on archive_id = archive.id
321                    WHERE dvd_nr = ?
322  });  });
323    
324  my $sth_archive_burned = $dbh->prepare( qq{  my $sth_archive_burned = $dbh->prepare( qq{
325          insert into archive_burned          insert into archive_burned
326                  (archive_id, iso_size)                  (archive_id, iso_size, part, copy)
327          values ( (select id from archive where dvd_nr =?), ?)          values ( (select id from archive where dvd_nr =?), ?, ?, ?)
328  });  });
329    
330  foreach my $arc (@archives_to_burn) {  foreach my $arc (@archives_to_burn) {
# Line 162  foreach my $arc (@archives_to_burn) { Line 339  foreach my $arc (@archives_to_burn) {
339    
340          print "Working on DVD #$dvd_nr in $tmp_dir\n";          print "Working on DVD #$dvd_nr in $tmp_dir\n";
341    
342          my $list_file = my $iso_file = "${iso_dir}/${dvd_nr}";          $sth_archive_backup_parts->execute($dvd_nr);
         $list_file .= '.list';  
         $iso_file .= '.iso';  
343    
344          if (-e $iso_file) {          $sth_archive_backup_check->execute($dvd_nr);
                 print "ISO $iso_file allready exists\n";  
                 goto BURN;  
         }  
345    
346          $sth_archive_backup->execute($dvd_nr);          my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
347    
348          open(my $list, "> $list_file") || skip "can't open $list_file: $!";          if ($parts_nr != $check_nr) {
349                    warn "ERROR: DVD #$dvd_nr is still not in consistent state. Some backup parts are ",
350                            ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",
351                            " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";
352                    next if delete_dvd( $dvd_nr );
353            }
354    
355          my $inc = 0;          if ($sth_archive_backup_parts->rows == 0) {
356                    warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
357                    next if delete_dvd( $dvd_nr );
358            }
359    
360          while (my $row = $sth_archive_backup->fetchrow_hashref) {          if ($prompt_for_delete) {
361                  my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});                  next if delete_dvd( $dvd_nr );
362                  skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");          }
                 print $list "$tar_dir/$tar_file\n";  
                 $inc++;  
363    
364            my @volumes;
365            my $v;  # emtpy volume
366            my $v_size = 0;
367    
368            my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
369            while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
370                    if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
371                            push @volumes, $v;
372                            $v = {};
373                    }
374                    $v->{size} += $row->{part_size};
375                    # this part
376                    my $p = {
377                            filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
378                    };
379                    foreach my $fld (qw/part_nr md5/) {
380                            $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
381                    }
382                    push @{ $v->{parts} }, $p;
383          }          }
384            push @volumes, $v if ($v);
385    
386          # FIXME add file list in xml and txt and note file          #warn "# volumes: ",Dumper(\@volumes),"\n";
387    
388          close($list);          my $volumes = $#volumes + 1;
389            my $volume_nr = 1;
390    
391          print "Running mkisofs now for $inc increments...\n";          foreach my $v (@volumes) {
392    
393          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 };                  #print Dumper($v);
394    
395          system($cmd) == 0 or skip "can't run $cmd: $?";                  my $iso_size = 0;
396                    my $disk_name = $dvd_nr;
397                    # suffix added to multi-volume archives
398                    my $volume_suffix = '';
399    
400          my $size = (stat($iso_file))[7];                  if ($volumes > 1) {
401                            $volume_suffix = '_' . $volume_nr;
402                            $disk_name .= ' ' . $volume_nr . '/' . $volumes;
403                    }
404    
405          print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n";                  print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";
406    
407  BURN:                  my $iso_file = my $xml_file = my $stage =
408          # FIXME add call to cdrecord here!                          "${iso_dir}/${dvd_nr}";
409          $sth_archive_burned->execute($dvd_nr, $size);  
410          print "Media burn for $dvd_nr recorded\n";                  $iso_file .= $volume_suffix . '.iso';
411                    $xml_file .= '.xml';
412    
413                    $stage .= $volume_suffix . '.stage';
414    
415                    my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
416    
417                    #
418                    # check if ISO file exists
419                    #
420    
421                    if (! -e $iso_file) {
422    
423                            # create stage directory
424                            if (-e $stage) {
425                                    rmtree($stage) || die "can't remove $stage: $!";
426                            }
427                            mkpath($stage);
428    
429                            # open file for md5sums
430                            open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
431    
432                            my $parts_on_this_volume = 0;
433    
434                            foreach my $p (@{ $v->{parts} }) {
435                                    my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
436                                    my $rel_path = $tar_file;
437    
438                                    if (-d "$tar_dir/$rel_path") {
439                                            mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
440                                            $rel_path .= '/' . $p->{part_nr};
441                                    }
442                                    $rel_path .= '.tar.gz';
443    
444                                    skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
445    
446                                    add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
447    
448                                    my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
449                                    chomp($md5sum);
450                                    print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
451    
452                                    $parts_on_this_volume++;
453                            }
454    
455                            # add file list and note in xml
456                            dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
457    
458                            add_symlink($xml_file, "$stage/${dvd_nr}.xml");
459    
460                            # add css file for archive
461                            my $css_file = $Conf{CgiImageDir} . '/archive.css';
462                            if (-r $css_file) {
463                                    add_symlink($css_file, "$stage/archive.css");
464                            } else {
465                                    print "WARNING: missing $css_file, not added to iso image!\n";
466                            }
467    
468                            print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
469    
470                            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 };
471    
472                            system($cmd) == 0 or skip "can't run $cmd: $?";
473    
474                            rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
475    
476                            $iso_size = (stat($iso_file))[7];
477    
478                            print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
479    
480                            rmtree($stage) || warn "can't remove stage directory $stage: $!";
481    
482                    } else {
483                            print "ISO $iso_file allready exists\n";
484                    }
485    
486                    my $copies = $Conf{BurnMultipleCopies} || 1;
487    
488                    foreach my $copy_nr ( 1 .. $copies ) {
489    
490                            print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
491    
492                            system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
493    
494                            my $wait = <STDIN>;
495    
496                            my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
497    
498                            # FIXME
499                            print "## $cmd\n";
500                            system($cmd) == 0 or skip "can't run $cmd: $?";
501    
502                            print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
503    
504                            $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
505    
506                            print "Media burn for $disk_name copy $copy_nr recorded\n";
507                    }
508    
509                    $volume_nr++;
510            }
511    
512  SKIP:  SKIP:
513    
# Line 211  SKIP: Line 516  SKIP:
516  #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});  #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
517  #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 ";
518    
519    print "Recoding finished, exiting...\n";
520    
521  $sth->finish;  $sth->finish;
522    $sth_archive_backup_parts->finish;
523    $sth_archive_burned->finish;
524    
525  $dbh->disconnect;  $dbh->disconnect;
526    

Legend:
Removed from v.181  
changed lines
  Added in v.292

  ViewVC Help
Powered by ViewVC 1.1.26