5 use lib "/usr/local/BackupPC/lib";
10 use Time::HiRes qw/time/;
11 use POSIX qw/strftime/;
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 my %Conf = $bpc->Conf();
26 %BackupPC::Search::Conf = %Conf;
30 $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
31 my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
32 $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
33 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";
36 my $path = abs_path($0);
37 $path =~ s#/[^/]+$#/#;
38 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
41 foreach my $c (qw/cdrecord eject mkisofs/) {
42 $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
47 my $t_fmt = '%Y-%m-%d %H:%M:%S';
50 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
51 my $user = $Conf{SearchUser} || '';
53 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
55 my $tar_dir = $Conf{InstallDir}.'/';
56 $tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir 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{InstallDir}.'/';
61 $iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration";
62 die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir);
66 sub df_bytes_available {
68 my $out = `df -P $path`;
69 my $bytes = (split(/\s+/, (split(/\n/,$out))[-1]))[3];
75 my $t = shift || return;
77 my ($ss,$mm,$hh) = gmtime($t);
78 $out .= "${hh}h" if ($hh);
79 $out .= sprintf("%02d:%02d", $mm,$ss);
84 return strftime($t_fmt,localtime());
87 sub dumpArchive2XML($$$)
89 my ($dbh, $dvd_nr, $filename) = @_;
92 my $output = new IO::File('> '.$filename.'.tmp');
93 my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
95 print "Dumping file list for DVD $dvd_nr, countdown:";
97 $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
105 FROM files, backups, shares
106 WHERE files.backupnum=backups.num AND
107 files.shareid=shares.id AND
108 shares.hostid=backups.hostid AND
114 backups.id AS backup_id,
117 backups.date AS date,
118 shares.name AS share,
119 backups.size AS backup_size,
120 backups.inc_size AS compress_size
121 FROM backups, archive_backup, hosts, shares
122 WHERE archive_backup.backup_id = backups.id
123 AND hosts.id=backups.hostid
124 AND shares.id=backups.shareid
125 AND archive_backup.archive_id = ?
127 hosts.name, shares.name, backups.num
130 my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
131 $sth->execute($dvd_nr);
132 my $row = $sth->fetchrow_hashref();
135 foreach my $hide (qw/id note/) {
136 $row_h->{$hide} = $row->{$hide} || '';
137 delete $row->{$hide};
140 $writer->startTag("archive", %{$row});
142 $writer->startTag("note");
143 $writer->characters( $row_h->{'note'} );
144 $writer->endTag("note");
146 my $sth_backups = $dbh->prepare( $backups_sql );
147 $sth_backups->execute( $row_h->{'id'} );
149 my $total_backups = $sth_backups->rows;
153 while (my $row = $sth_backups->fetchrow_hashref()) {
156 my $pcnt = int(($curr_backup * 10) / $total_backups);
157 if ($pcnt > $last_pcnt) {
158 print " ",(10 - $pcnt);
162 my $sth_files = $dbh->prepare( $files_sql);
163 $sth_files->execute($row->{'backup_id'});
165 delete($row->{'backup_id'});
166 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
168 $writer->startTag( "backup", %{$row} );
170 while (my $row = $sth_files->fetchrow_hashref()) {
171 # convert filetype to string
172 $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
173 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
174 my $path = $row->{'path'}; delete $row->{'path'};
176 $writer->startTag("file", %{$row});
177 $writer->characters( $path );
178 $writer->endTag("file");
180 $writer->endTag("backup");
183 $writer->endTag("archive");
186 rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
195 my $sth = $dbh->prepare( qq{
197 id,dvd_nr,total_size,note,username,
198 archive.date as date,
199 count(archive_burned.archive_id) as copies
201 left outer join archive_burned on archive.id=archive_burned.archive_id
202 group by id,dvd_nr,total_size,note,username,archive.date
210 die "missing size" unless defined($s);
212 return sprintf("%.2f Mb", $s);
216 my $row = shift || die;
218 $row->{'date'} =~ s/\.\d+$//;
219 $row->{'copies'} =~ s/^\s*0+\s*$/no/;
221 my $copies = 'copies';
222 $copies = 'copy' if ($row->{'copies'} == 1);
225 sprintf("%d by %s on %s, %s %s [%s]",
229 $row->{'copies'}, $copies,
230 fmt_mb($row->{'total_size'}),
237 while (my $row = $sth->fetchrow_hashref) {
238 if ($row->{'copies'}) {
239 push @burned, fmt_archive($row);
241 push @not_burned, fmt_archive($row);
246 Label => 'Menu_archive',
249 Text => 'DVD #]Convey[',
250 Convey => [ @not_burned ],
254 Text => 'DVD #]Convey[',
255 Convey => [ @burned ],
260 Banner => "Select one or more DVD media for burning",
263 my @archives_to_burn = Menu(\%Menu_archive);
269 print "WARNING: $msg, skipping...\n";
273 sub add_symlink($$) {
274 my ($from, $to) = @_;
275 symlink $from, $to || skip("can't symlink $from to $to: $!");
279 my $dvd_nr = shift || return;
281 print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
284 if (lc($ok) eq 'yes') {
285 print "Deleting DVD #$dvd_nr from database...\n";
289 my $sth_delete_dvd = $dbh->prepare( qq{
290 delete from archive where dvd_nr = ?
292 $sth_delete_dvd->execute( $dvd_nr );
294 select setval('dvd_nr', (select max(dvd_nr) from archive), true)
297 # remove files for this DVD
300 print "\tremoving dir $_\n";
301 rmtree($_) || die "can't rmtree $_: $!";
303 print "\tremoving $_\n";
304 unlink($_) || die "can't rm $_: $!";
306 } glob ( "/$iso_dir/$dvd_nr.*" );
316 my $sth_archive_backup_parts = $dbh->prepare( qq{
318 archive_backup.backup_id,
321 shares.name as share,
323 backups.parts as parts,
324 backup_parts.part_nr as part_nr,
325 backup_parts.size as part_size,
326 backup_parts.md5 as md5,
329 join archive on archive_id = archive.id
330 join backups on archive_backup.backup_id = backups.id
331 join hosts on hostid = hosts.id
332 join shares on shareid = shares.id
333 join backup_parts on archive_backup.backup_id = backup_parts.backup_id
334 where archive.dvd_nr = ?
335 order by archive_backup.backup_id, backup_parts.part_nr
338 my $sth_archive_backup_check = $dbh->prepare( qq{
342 JOIN archive_backup on archive_backup.backup_id = backups.id
343 JOIN archive on archive_id = archive.id
347 my $sth_archive_burned = $dbh->prepare( qq{
348 insert into archive_burned
349 (archive_id, iso_size, part, copy)
350 values ( (select id from archive where dvd_nr =?), ?, ?, ?)
353 my $copies = $Conf{BurnMultipleCopies} || 1;
355 foreach my $copy_nr ( 1 .. $copies ) {
357 foreach my $arc (@archives_to_burn) {
358 exit if ($arc eq ']quit[');
360 my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
361 die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
363 my $tmp_dir = "/$iso_dir/$dvd_nr";
367 print "Working on DVD #$dvd_nr in $tmp_dir\n";
369 $sth_archive_backup_parts->execute($dvd_nr);
371 $sth_archive_backup_check->execute($dvd_nr);
373 my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
375 if ($parts_nr != $check_nr) {
376 warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",
377 ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",
378 " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";
379 delete_dvd( $dvd_nr );
383 if ($sth_archive_backup_parts->rows == 0) {
384 warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
385 next if delete_dvd( $dvd_nr );
388 if ($prompt_for_delete) {
389 next if delete_dvd( $dvd_nr );
393 my $v; # emtpy volume
396 my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
397 while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
398 if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
402 $v->{size} += $row->{part_size};
403 $v_size += $row->{part_size};
407 filename => BackupPC::Search::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
408 host => $row->{'host'},
409 share => $row->{'share'},
412 foreach my $fld (qw/part_nr md5/) {
413 $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
415 push @{ $v->{parts} }, $p;
417 push @volumes, $v if ($v);
419 #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
421 # check available disk space
423 my $df = df_bytes_available($iso_dir);
425 warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( $df ), " on $iso_dir]\n";
429 my $volumes = $#volumes + 1;
432 foreach my $v (@volumes) {
437 my $disk_name = $dvd_nr;
438 # suffix added to multi-volume archives
439 my $volume_suffix = '';
442 $volume_suffix = '_' . $volume_nr;
443 $disk_name .= ' ' . $volume_nr . '/' . $volumes;
446 print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
448 my $iso_file = my $xml_file = my $stage =
449 "${iso_dir}/${dvd_nr}";
451 $iso_file .= $volume_suffix . '.iso';
454 $stage .= $volume_suffix . '.stage';
456 my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
459 # check if ISO file exists
462 if (! -e $iso_file) {
464 # create stage directory
466 rmtree($stage) || die "can't remove $stage: $!";
470 # open file for md5sums
471 open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
473 my $parts_on_this_volume = 0;
475 foreach my $p (@{ $v->{parts} }) {
476 my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
477 my $rel_path = $tar_file;
479 if (-d "$tar_dir/$rel_path") {
480 mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
481 $rel_path .= '/' . $p->{part_nr};
483 $rel_path .= '.tar.gz';
486 unless (-r "$tar_dir/$rel_path") {
487 print "WARNING: can't find increment $rel_path , trying to recreate it using BackupPC_tarIncCreate...\n";
488 my $host = $p->{host};
489 my $share = $p->{share};
490 my $dump = $p->{num};
491 my $currUser = getlogin();
493 if ($currUser eq "agi") {
494 $otherUser = "backuppc-agi";
495 } elsif ($currUser eq "qc") {
496 $otherUser = "backuppc-qc";
498 if ($otherUser ne "") {
499 my $cmd = "sudo -u $otherUser ".$tarIncCreate. " -h $host -s $share -n $dump";
501 if (system($cmd) != 0) {
509 skip "can't find increment $rel_path, recreateing obviously did not work: $!" unless (-r "$tar_dir/$rel_path");
511 add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
513 my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
515 print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!";
517 $parts_on_this_volume++;
520 # add file list and note in xml
521 dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
523 add_symlink($xml_file, "$stage/${dvd_nr}.xml");
525 # add css file for archive
526 my $css_file = $Conf{CgiImageDir} . '/archive.css';
528 add_symlink($css_file, "$stage/archive.css");
530 print "WARNING: missing $css_file, not added to iso image!\n";
533 print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
535 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 };
537 system($cmd) == 0 or skip "can't run $cmd: $?";
539 rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
541 $iso_size = (stat($iso_file))[7];
543 print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
545 rmtree($stage) || warn "can't remove stage directory $stage: $!";
548 $iso_size = (stat($iso_file))[7];
549 print "ISO $iso_file already exists [$iso_size bytes]\n";
552 print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
554 system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
558 my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
562 system($cmd) == 0 or skip "can't run $cmd: $?";
564 print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
566 $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
568 print "Media burn for $disk_name copy $copy_nr recorded\n";
570 if ($copy_nr >= $copies) {
571 print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
572 foreach my $f (( $xml_file, $iso_file )) {
573 print STDERR "\t$f ";
574 unlink $f || die "can't remove $f: $!";
575 print STDERR "removed\n";
588 #my $tar_file = BackupPC::Search::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
589 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
591 print "Recoding finished, exiting...\n";
594 $sth_archive_backup_parts->finish;
595 $sth_archive_burned->finish;
596 $sth_archive_backup_check->finish;
597 $sth_archive_burned->finish;