#!/usr/local/bin/perl use strict; #use lib "__INSTALLDIR__/lib"; # FIXME use lib "/data/backuppc/lib"; use DBI; use BackupPC::Lib; use BackupPC::SearchLib; use Time::HiRes qw/time/; use POSIX qw/strftime/; use Term::Menus; use Data::Dumper; my $debug = 0; $|=1; my $start_t = time(); my $t_fmt = '%Y-%m-%d %H:%M:%S'; # don't check for user my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; my %Conf = $bpc->Conf(); %BackupPC::SearchLib::Conf = %Conf; my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; my $user = $Conf{SearchUser} || ''; my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 }); my $tar_dir = $Conf{InstallDir}.'/'; $tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration"; die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir); my $iso_dir = $Conf{InstallDir}.'/'; $iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration"; die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir); #---- subs ---- sub fmt_time { my $t = shift || return; my $out = ""; my ($ss,$mm,$hh) = gmtime($t); $out .= "${hh}h" if ($hh); $out .= sprintf("%02d:%02d", $mm,$ss); return $out; } sub curr_time { return strftime($t_fmt,localtime()); } sub dumpArchive2XML($$$) { my ($dbh, $dvd_nr, $filename) = @_; my %archive; my $output = new IO::File(">$filename"); my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); my $files_sql = q{ SELECT files.path AS path, files.date AS date, files.type AS type, files.size AS size FROM files, backups, shares WHERE files.backupnum=backups.num AND files.shareid=shares.id AND shares.hostid=backups.hostid AND backups.id=? }; my $backups_sql = q{ SELECT backups.id AS backup_id, hosts.name AS host, backups.num AS num, backups.date AS date, shares.name AS share, backups.size AS backup_size, backups.inc_size AS compress_size FROM backups, archive_backup, hosts, shares WHERE archive_backup.backup_id = backups.id AND hosts.id=backups.hostid AND shares.id=backups.shareid AND archive_backup.archive_id = ? }; my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?"); $sth->execute($dvd_nr); my $row = $sth->fetchrow_hashref(); my $row_h; foreach my $hide (qw/id note/) { $row_h->{$hide} = $row->{$hide} || die "no $hide"; delete $row->{$hide}; } $writer->startTag("archive", %{$row}); $writer->startTag("note"); $writer->characters( $row_h->{'note'} ); $writer->endTag("note"); my $sth_backups = $dbh->prepare( $backups_sql ); $sth_backups->execute( $row_h->{'id'} ); while (my $row = $sth_backups->fetchrow_hashref()) { my $sth_files = $dbh->prepare( $files_sql); $sth_files->execute($row->{'backup_id'}); delete($row->{'backup_id'}); $writer->startTag( "backup", %{$row} ); while (my $row = $sth_files->fetchrow_hashref()) { # convert filetype to string $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'}); my $path = $row->{'path'}; delete $row->{'path'}; $writer->startTag("file", %{$row}); $writer->characters( $path ); $writer->endTag("file"); } $writer->endTag("backup"); } $writer->endTag("archive"); $writer->end(); } #----- main my $sth = $dbh->prepare( qq{ select id,dvd_nr,total_size,note,username, archive.date as date, count(archive_burned.archive_id) as copies from archive left outer join archive_burned on archive.id=archive_burned.archive_id group by id,dvd_nr,total_size,note,username,archive.date order by date asc } ); $sth->execute(); sub fmt_archive($) { my $row = shift || die; $row->{'date'} =~ s/\.\d+$//; $row->{'copies'} =~ s/^\s*0+\s*$/no/; $row->{'total_size'} /= (1024*1024); my $copies = 'copies'; $copies = 'copy' if ($row->{'copies'} == 1); return sprintf("%d by %s on %s, %s %s [%.2f Mb]", $row->{'dvd_nr'}, $row->{'username'}, $row->{'date'}, $row->{'copies'}, $copies, $row->{'total_size'}, ); } my @burned; my @not_burned; while (my $row = $sth->fetchrow_hashref) { if ($row->{'copies'}) { push @burned, fmt_archive($row); } else { push @not_burned, fmt_archive($row); } } my %Menu_archive = ( Label => 'Menu_archive', Item_1 => { Text => 'DVD #]Convey[', Convey => [ @not_burned ], Default => '*', }, Item_2 => { Text => 'DVD #]Convey[', Convey => [ @burned ], }, Select => 'Many', Banner => "Select one or more DVD media for burning", ); my @archives_to_burn = Menu(\%Menu_archive); print "\n"; sub skip($) { my $msg = shift; print "WARNING: $msg, skipping...\n"; goto SKIP; } my $sth_archive_backup = $dbh->prepare( qq{ select backup_id, archive_id, hosts.name as host, shares.name as share, backups.num as num from archive_backup join archive on archive_id = archive.id join backups on backup_id = backups.id join hosts on hostid = hosts.id join shares on shareid = shares.id where archive.dvd_nr = ? }); my $sth_archive_burned = $dbh->prepare( qq{ insert into archive_burned (archive_id, iso_size) values ( (select id from archive where dvd_nr =?), ?) }); foreach my $arc (@archives_to_burn) { exit if ($arc eq ']quit['); my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/); die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr); my $tmp_dir = "/$iso_dir/$dvd_nr"; my $t = time(); print "Working on DVD #$dvd_nr in $tmp_dir\n"; my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; $list_file .= '.list'; $iso_file .= '.iso'; $xml_file .= '.xml'; if (-e $iso_file) { print "ISO $iso_file allready exists\n"; goto BURN; } $sth_archive_backup->execute($dvd_nr); open(my $list, "> $list_file") || skip "can't open $list_file: $!"; my $inc = 0; while (my $row = $sth_archive_backup->fetchrow_hashref) { my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); print $list "$tar_dir/$tar_file\n"; $inc++; } # FIXME add file list in xml and txt and note file dumpArchive2XML($dbh, $dvd_nr, $xml_file); print $list "$xml_file\n"; close($list); print "Running mkisofs now for $inc increments...\n"; 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 }; system($cmd) == 0 or skip "can't run $cmd: $?"; my $size = (stat($iso_file))[7]; print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n"; BURN: # FIXME add call to cdrecord here! $sth_archive_burned->execute($dvd_nr, $size); print "Media burn for $dvd_nr recorded\n"; SKIP: } #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file "; $sth->finish; $dbh->disconnect;