/[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 176 by dpavlin, Tue Oct 11 18:32:27 2005 UTC revision 203 by dpavlin, Fri Oct 14 15:53:54 2005 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    
14  use Data::Dumper;  use Data::Dumper;
15    
16  my $debug = 0;  my $debug = 0;
17  $|=1;  $|=1;
18    
 my $start_t = time();  
   
 my $t_fmt = '%Y-%m-%d %H:%M:%S';  
   
19  # don't check for user  # don't check for user
20  my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;  my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
21  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
22  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
23    
24    my $cdrecord = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
25    my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
26    
27    
28    my $bin;
29    foreach my $c (qw/mkisofs eject/, $cdrecord) {
30            $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
31    }
32    
33    my $start_t = time();
34    
35    my $t_fmt = '%Y-%m-%d %H:%M:%S';
36    
37    
38  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
39  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
40    
# Line 55  sub curr_time { Line 64  sub curr_time {
64          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
65  }  }
66    
67    sub dumpArchive2XML($$$)
68    {
69            my ($dbh, $dvd_nr, $filename) = @_;
70            my %archive;    
71            my $output = new IO::File(">$filename");
72            my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
73    
74            print "Dumping file list for DVD $dvd_nr";
75    
76            $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
77    
78            my $files_sql = q{
79                    SELECT
80                            files.path AS path,
81                            files.date AS date,
82                            files.type AS type,
83                            files.size AS size
84                    FROM files, backups, shares
85                    WHERE files.backupnum=backups.num AND
86                            files.shareid=shares.id AND
87                            shares.hostid=backups.hostid AND
88                            backups.id=?
89            };
90    
91            my $backups_sql = q{
92                    SELECT
93                            backups.id   AS backup_id,
94                            hosts.name   AS host,
95                            backups.num  AS num,
96                            backups.date AS date,
97                            shares.name  AS share,
98                            backups.size AS backup_size,
99                            backups.inc_size AS compress_size
100                    FROM backups, archive_backup, hosts, shares
101                    WHERE archive_backup.backup_id = backups.id
102                            AND hosts.id=backups.hostid
103                            AND shares.id=backups.shareid
104                            AND archive_backup.archive_id = ?
105            };
106    
107            my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
108            $sth->execute($dvd_nr);
109            my $row = $sth->fetchrow_hashref();
110    
111            my $row_h;
112            foreach my $hide (qw/id note/) {
113                    $row_h->{$hide} = $row->{$hide} || '';
114                    delete $row->{$hide};
115            }
116    
117            $writer->startTag("archive", %{$row});
118    
119            $writer->startTag("note");
120            $writer->characters( $row_h->{'note'} );
121            $writer->endTag("note");
122    
123            my $sth_backups = $dbh->prepare( $backups_sql );
124            $sth_backups->execute( $row_h->{'id'} );
125    
126            while (my $row = $sth_backups->fetchrow_hashref()) {
127    
128                    print ".";
129    
130                    my $sth_files = $dbh->prepare( $files_sql);
131                    $sth_files->execute($row->{'backup_id'});
132    
133                    delete($row->{'backup_id'});
134                    $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
135    
136                    $writer->startTag( "backup", %{$row} );
137    
138                    while (my $row = $sth_files->fetchrow_hashref()) {
139                            # convert filetype to string
140                            $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
141                            $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
142                            my $path = $row->{'path'}; delete $row->{'path'};
143    
144                            $writer->startTag("file", %{$row});
145                            $writer->characters( $path );
146                            $writer->endTag("file");
147                    }
148                    $writer->endTag("backup");
149            }      
150                            
151            $writer->endTag("archive");
152            $writer->end();
153    
154            print "\n";
155    }
156    
157    
158    
159  #----- main  #----- main
160    
161  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 136  my $sth_archive_backup = $dbh->prepare( Line 237  my $sth_archive_backup = $dbh->prepare(
237                  hosts.name as host,                  hosts.name as host,
238                  shares.name as share,                  shares.name as share,
239                  backups.num as num,                  backups.num as num,
240                    backups.parts as parts
241          from archive_backup          from archive_backup
242          join archive on archive_id = archive.id          join archive on archive_id = archive.id
243          join backups on backup_id = backups.id          join backups on backup_id = backups.id
# Line 146  my $sth_archive_backup = $dbh->prepare( Line 248  my $sth_archive_backup = $dbh->prepare(
248    
249  my $sth_archive_burned = $dbh->prepare( qq{  my $sth_archive_burned = $dbh->prepare( qq{
250          insert into archive_burned          insert into archive_burned
251                  (archive_id, iso_size)                  (archive_id, iso_size, part, copy)
252          values ( (select id from archive where dvd_nr =?), ?)          values ( (select id from archive where dvd_nr =?), ?, ?, ?)
253  });  });
254    
255  foreach my $arc (@archives_to_burn) {  foreach my $arc (@archives_to_burn) {
# Line 162  foreach my $arc (@archives_to_burn) { Line 264  foreach my $arc (@archives_to_burn) {
264    
265          print "Working on DVD #$dvd_nr in $tmp_dir\n";          print "Working on DVD #$dvd_nr in $tmp_dir\n";
266    
267          my $list_file = my $iso_file = "${iso_dir}/${dvd_nr}";          my $part_path = '';
268          $list_file .= '.list';          my $part_nr = 1;
269          $iso_file .= '.iso';          my $parts = 0;
270                    
         if (-e $iso_file) {  
                 print "ISO $iso_file allready exists\n";  
                 goto BURN;  
         }  
   
271          $sth_archive_backup->execute($dvd_nr);          $sth_archive_backup->execute($dvd_nr);
272    
273          open(my $list, "> $list_file") || skip "can't open $list_file: $!";          my $disk_name = 'unknown disk';
274            my $iso_size = 0;
275    
276          my $inc = 0;          do {
277    
278          while (my $row = $sth_archive_backup->fetchrow_hashref) {                  # do we need to re-execute query for multi-part increments?
279                  my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});                  $sth_archive_backup->execute($dvd_nr) if ($parts > 1);
                 skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");  
                 print $list "$tar_dir/$tar_file\n";  
                 $inc++;  
280    
281          }                  my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";
282    
283    
284                    $parts =  $row->{'parts'} || die "no parts?";
285                    $disk_name = $dvd_nr;
286    
287                    if ($parts > 1) {
288                            # parts start with 0, so we have -1 here
289                            $part_path = '.' . ($part_nr - 1);
290                            $disk_name .= '.' . $part_nr;
291                    }
292    
293                    print "Processing part $part_nr/$parts\n";
294    
295                    my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
296                    $list_file .= $part_path . '.list';
297                    $iso_file .= $part_path . '.iso';
298                    $xml_file .= '.xml';
299    
300                    #
301                    # check if ISO file exists
302                    #
303    
304                    if (! -e $iso_file) {
305    
306                            open(my $list, "> $list_file") || skip "can't open $list_file: $!";
307    
308                            my $inc = 0;
309    
310                            do {
311                                    my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
312                                    if (-f "$tar_dir/$tar_file") {
313                                            skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
314                                            print $list "$tar_dir/$tar_file\n";
315                                    } else {
316                                            my $len = length("$parts");
317                                            my $nr = sprintf("%0${len}d", ($part_nr - 1));
318                                            print $list "$tar_dir/$tar_file/part$nr\n";
319                                    }
320                                    $inc++;
321                            } while ($row = $sth_archive_backup->fetchrow_hashref);
322    
323                            # add file list and note in xml
324                            dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
325                            print $list "$xml_file\n";
326    
327                            # add css file for archive
328                            my $css_file = $Conf{CgiImageDir} . '/archive.css';
329                            if (-r $css_file) {
330                                    print $list "$css_file\n";
331                            } else {
332                                    print "WARNING: missing $css_file, not added to iso image!\n";
333                            }
334    
335          print "Running mkisofs now for $inc increments...\n";                          close($list);
336    
337          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 "Running mkisofs now for $inc increments, disk $disk_name\n";
338    
339          system($cmd) == 0 or skip "can't run $cmd: $?";                          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 };
340    
341          my $size = (stat($iso_file))[7];                          system($cmd) == 0 or skip "can't run $cmd: $?";
342    
343          print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n";                          $iso_size = (stat($iso_file))[7];
344    
345  BURN:                          print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
346          # FIXME add call to cdrecord here!  
347          $sth_archive_burned->execute($dvd_nr, $size);                  } else {
348          print "Media burn for $dvd_nr recorded\n";                          print "ISO $iso_file allready exists\n";
349                    }
350    
351                    my $copies = $Conf{BurnMultipleCopies} || 1;
352    
353                    foreach my $copy_nr ( 1 .. $copies ) {
354    
355                            print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
356    
357                            system($bin->{'eject'}) == 0 or skip "can't run eject: $?";
358    
359                            my $wait = <STDIN>;
360    
361                            my $cmd = $bin->{$cdrecord} . ' ' . $cdr_opts . ' ' . $iso_file;
362    
363                            # FIXME
364                            print "## $cmd\n";
365                            system($cmd) == 0 or skip "can't run $cmd: $?";
366    
367                            print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
368    
369                            $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr);
370    
371                            print "Media burn for $disk_name copy $copy_nr recorded\n";
372                    }
373    
374                    $part_nr++;
375            } until ($part_nr > $parts);
376    
377  SKIP:  SKIP:
378    
# Line 207  SKIP: Line 381  SKIP:
381  #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});  #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
382  #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 ";
383    
384    print "Recoding finished, exiting...\n";
385    
386  $sth->finish;  $sth->finish;
387    $sth_archive_backup->finish;
388    $sth_archive_burned->finish;
389    
390  $dbh->disconnect;  $dbh->disconnect;
391    

Legend:
Removed from v.176  
changed lines
  Added in v.203

  ViewVC Help
Powered by ViewVC 1.1.26