5 use lib "/usr/local/BackupPC/lib";
10 use Time::HiRes qw/time/;
11 use POSIX qw/strftime/;
18 my $debug = $ENV{DEBUG} || 0;
19 # set this to 1 to prompt for DVD removal for each selected item.
20 my $prompt_for_delete = shift @ARGV;
23 # don't check for user
24 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
25 $bpc->ConfigRead('_search_archive');
26 my %Conf = $bpc->Conf();
27 %BackupPC::Search::Conf = %Conf;
31 $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
32 my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
33 $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
34 my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
35 $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
37 my $path = abs_path($0);
38 $path =~ s#/[^/]+$#/#;
39 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
42 foreach my $c (qw/cdrecord eject mkisofs/) {
43 $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
48 my $t_fmt = '%Y-%m-%d %H:%M:%S';
51 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
52 my $user = $Conf{SearchUser} || '';
54 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
56 my $tar_dir = $Conf{ArchiveDest} || die "ArchiveDest isn't defined in configuration";
58 #die "problem with $tar_dir, check GzipTempDir in configuration\n" unless -d $tar_dir;
60 my $iso_dir = $Conf{ArchiveDest}.'/iso';
61 if ( ! -e $iso_dir ) {
62 mkdir $iso_dir || die "can't create $iso_dir: $!";
67 sub df_bytes_available {
69 my $out = `df -P $path`;
70 my $bytes = (split(/\s+/, (split(/\n/,$out))[-1]))[3];
76 my $t = shift || return;
78 my ($ss,$mm,$hh) = gmtime($t);
79 $out .= "${hh}h" if ($hh);
80 $out .= sprintf("%02d:%02d", $mm,$ss);
85 return strftime($t_fmt,localtime());
88 sub dumpArchive2XML($$$)
90 my ($dbh, $dvd_nr, $filename) = @_;
93 my $output = new IO::File('> '.$filename.'.tmp');
94 my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
96 print "Dumping file list for DVD $dvd_nr, countdown:";
98 $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
106 FROM files, backups, shares
107 WHERE files.backupnum=backups.num AND
108 files.shareid=shares.id AND
109 shares.hostid=backups.hostid AND
122 FROM archive_backup_parts
124 ORDER BY backup_part_id
127 my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date, id FROM archive WHERE dvd_nr=?");
128 $sth->execute($dvd_nr);
129 my $row = $sth->fetchrow_hashref();
132 foreach my $hide (qw/id note/) {
133 $row_h->{$hide} = $row->{$hide} || '';
134 delete $row->{$hide};
137 $writer->startTag("archive", %{$row});
139 $writer->startTag("note");
140 $writer->characters( $row_h->{'note'} );
141 $writer->endTag("note");
143 my $sth_backups = $dbh->prepare( $backups_sql );
144 $sth_backups->execute( $row_h->{'id'} );
146 my $total_backups = $sth_backups->rows;
150 while (my $row = $sth_backups->fetchrow_hashref()) {
153 my $pcnt = int(($curr_backup * 10) / $total_backups);
154 if ($pcnt > $last_pcnt) {
155 print " ",(10 - $pcnt);
159 my $sth_files = $dbh->prepare( $files_sql);
160 $sth_files->execute($row->{'backup_id'});
162 delete($row->{'backup_id'});
163 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
165 $writer->startTag( "backup", %{$row} );
167 while (my $row = $sth_files->fetchrow_hashref()) {
168 # convert filetype to string
169 $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
170 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
171 my $path = $row->{'path'}; delete $row->{'path'};
173 $writer->startTag("file", %{$row});
174 $writer->characters( $path );
175 $writer->endTag("file");
177 $writer->endTag("backup");
180 $writer->endTag("archive");
183 rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
192 my $sth = $dbh->prepare( qq{
194 id,dvd_nr,total_size,note,username,
195 archive.date as date,
196 count(archive_burned.archive_id) as copies
198 left outer join archive_burned on archive.id=archive_burned.archive_id
199 group by id,dvd_nr,total_size,note,username,archive.date
207 die "missing size" unless defined($s);
209 return sprintf("%.2f Mb", $s);
213 my $row = shift || die;
215 $row->{'date'} =~ s/\.\d+$//;
216 $row->{'copies'} =~ s/^\s*0+\s*$/no/;
218 my $copies = 'copies';
219 $copies = 'copy' if ($row->{'copies'} == 1);
222 sprintf("%d by %s on %s, %s %s [%s]",
226 $row->{'copies'}, $copies,
227 fmt_mb($row->{'total_size'}),
234 while (my $row = $sth->fetchrow_hashref) {
235 if ($row->{'copies'}) {
236 push @burned, fmt_archive($row);
238 push @not_burned, fmt_archive($row);
243 Label => 'Menu_archive',
246 Text => 'DVD #]Convey[',
247 Convey => [ @not_burned ],
251 Text => 'DVD #]Convey[',
252 Convey => [ @burned ],
257 Banner => "Select one or more DVD media for burning",
260 my @archives_to_burn = Menu(\%Menu_archive);
266 print "WARNING: $msg, skipping...\n";
270 sub add_symlink($$) {
271 my ($from, $to) = @_;
272 symlink $from, $to || skip("can't symlink $from to $to: $!");
276 my $dvd_nr = shift || return;
278 print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
281 if (lc($ok) eq 'yes') {
282 print "Deleting DVD #$dvd_nr from database...\n";
286 my $sth_delete_dvd = $dbh->prepare( qq{
287 delete from archive where dvd_nr = ?
289 $sth_delete_dvd->execute( $dvd_nr );
291 select setval('dvd_nr', (select max(dvd_nr) from archive), true)
294 # remove files for this DVD
297 print "\tremoving dir $_\n";
298 rmtree($_) || die "can't rmtree $_: $!";
300 print "\tremoving $_\n";
301 unlink($_) || die "can't rm $_: $!";
303 } glob ( "/$iso_dir/$dvd_nr.*" );
313 my $sth_archive_backup_parts = $dbh->prepare( qq{
326 FROM archive_backup_parts
328 ORDER BY backup_id, part_nr
331 my $sth_archive_burned = $dbh->prepare( qq{
332 insert into archive_burned
333 (archive_id, iso_size, part, copy)
334 values ( (select id from archive where dvd_nr =?), ?, ?, ?)
337 my $copies = $Conf{BurnMultipleCopies} || 1;
339 foreach my $copy_nr ( 1 .. $copies ) {
341 foreach my $arc (@archives_to_burn) {
342 exit if ($arc eq ']quit[');
344 my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
345 die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
347 my $tmp_dir = "/$iso_dir/$dvd_nr";
351 print "Working on DVD #$dvd_nr in $tmp_dir\n";
353 $sth_archive_backup_parts->execute($dvd_nr);
355 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 );
360 if ($prompt_for_delete) {
361 next if delete_dvd( $dvd_nr );
365 my $v; # emtpy volume
368 my $max_archive_size = $Conf{ArchiveMediaSize} || die "no ArchiveMediaSize";
369 while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
370 if (($v->{size} || 0) + $row->{size} > $max_archive_size) {
374 $v->{size} += $row->{size};
375 $v_size += $row->{size};
379 $p->{$_} = $row->{$_} foreach (qw(
380 filename host share num part_nr md5
381 )); # include minimun number of columns in XML output
383 push @{ $v->{parts} }, $p;
385 push @volumes, $v if ($v);
387 warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n" if $debug;
389 # check available disk space
391 my $df = df_bytes_available($iso_dir);
393 warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( $df ), " on $iso_dir]\n";
397 my $volumes = $#volumes + 1;
400 foreach my $v (@volumes) {
405 my $disk_name = $dvd_nr;
406 # suffix added to multi-volume archives
407 my $volume_suffix = '';
410 $volume_suffix = '_' . $volume_nr;
411 $disk_name .= ' ' . $volume_nr . '/' . $volumes;
414 print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
416 my $iso_file = my $xml_file = my $stage =
417 "${iso_dir}/${dvd_nr}";
419 $iso_file .= $volume_suffix . '.iso';
422 $stage .= $volume_suffix . '.stage';
424 my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
427 # check if ISO file exists
430 if (! -e $iso_file) {
432 # create stage directory
434 rmtree($stage) || die "can't remove $stage: $!";
438 # open file for md5sums
439 open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
441 my $parts_on_this_volume = 0;
443 foreach my $p (@{ $v->{parts} }) {
444 my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
446 die "ERROR: increment missing $tar_file:$!" unless -r "$tar_dir/$tar_file";
448 add_symlink("$tar_dir/$tar_file", "$stage/$tar_file");
450 my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
452 print $md5 "$md5sum $tar_file\n" || die "can't write md5sum: $!";
454 $parts_on_this_volume++;
457 # add file list and note in xml
458 dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
460 add_symlink($xml_file, "$stage/${dvd_nr}.xml");
462 # add css file for archive
463 my $css_file = $Conf{CgiImageDir} . '/archive.css';
465 add_symlink($css_file, "$stage/archive.css");
467 print "WARNING: missing $css_file, not added to iso image!\n";
470 print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
472 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 };
474 system($cmd) == 0 or skip "can't run $cmd: $?";
476 rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
478 $iso_size = (stat($iso_file))[7];
480 print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
482 rmtree($stage) || warn "can't remove stage directory $stage: $!";
485 $iso_size = (stat($iso_file))[7];
486 print "ISO $iso_file already exists [$iso_size bytes]\n";
489 print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
491 system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
495 my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
499 system($cmd) == 0 or skip "can't run $cmd: $?";
501 print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
503 $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
505 print "Media burn for $disk_name copy $copy_nr recorded\n";
507 if ($copy_nr >= $copies) {
508 print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
509 foreach my $f (( $xml_file, $iso_file )) {
510 print STDERR "\t$f ";
511 unlink $f || die "can't remove $f: $!";
512 print STDERR "removed\n";
525 #my $tar_file = BackupPC::Search::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
526 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
528 print "Recoding finished, exiting...\n";
531 $sth_archive_backup_parts->finish;
532 $sth_archive_burned->finish;
533 $sth_archive_burned->finish;