1 |
#!/usr/local/bin/perl |
2 |
|
3 |
use strict; |
4 |
#use lib "__INSTALLDIR__/lib"; |
5 |
# FIXME |
6 |
use lib "/data/backuppc/lib"; |
7 |
|
8 |
use DBI; |
9 |
use BackupPC::Lib; |
10 |
use BackupPC::SearchLib; |
11 |
use Time::HiRes qw/time/; |
12 |
use POSIX qw/strftime/; |
13 |
use Term::Menus; |
14 |
|
15 |
use Data::Dumper; |
16 |
|
17 |
my $debug = 0; |
18 |
$|=1; |
19 |
|
20 |
my $start_t = time(); |
21 |
|
22 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
23 |
|
24 |
# don't check for user |
25 |
my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; |
26 |
my %Conf = $bpc->Conf(); |
27 |
%BackupPC::SearchLib::Conf = %Conf; |
28 |
|
29 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
30 |
my $user = $Conf{SearchUser} || ''; |
31 |
|
32 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 }); |
33 |
|
34 |
my $tar_dir = $Conf{InstallDir}.'/'; |
35 |
$tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration"; |
36 |
|
37 |
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir); |
38 |
|
39 |
my $iso_dir = $Conf{InstallDir}.'/'; |
40 |
$iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration"; |
41 |
die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir); |
42 |
|
43 |
#---- subs ---- |
44 |
|
45 |
sub fmt_time { |
46 |
my $t = shift || return; |
47 |
my $out = ""; |
48 |
my ($ss,$mm,$hh) = gmtime($t); |
49 |
$out .= "${hh}h" if ($hh); |
50 |
$out .= sprintf("%02d:%02d", $mm,$ss); |
51 |
return $out; |
52 |
} |
53 |
|
54 |
sub curr_time { |
55 |
return strftime($t_fmt,localtime()); |
56 |
} |
57 |
|
58 |
#----- main |
59 |
|
60 |
my $sth = $dbh->prepare( qq{ |
61 |
select |
62 |
id,dvd_nr,total_size,note,username, |
63 |
archive.date as date, |
64 |
count(archive_burned.archive_id) as copies |
65 |
from archive |
66 |
left outer join archive_burned on archive.id=archive_burned.archive_id |
67 |
group by id,dvd_nr,total_size,note,username,archive.date |
68 |
order by date asc |
69 |
} ); |
70 |
|
71 |
$sth->execute(); |
72 |
|
73 |
sub fmt_archive($) { |
74 |
my $row = shift || die; |
75 |
|
76 |
$row->{'date'} =~ s/\.\d+$//; |
77 |
$row->{'copies'} =~ s/^\s*0+\s*$/no/; |
78 |
$row->{'total_size'} /= (1024*1024); |
79 |
|
80 |
my $copies = 'copies'; |
81 |
$copies = 'copy' if ($row->{'copies'} == 1); |
82 |
|
83 |
return |
84 |
sprintf("%d by %s on %s, %s %s [%.2f Mb]", |
85 |
$row->{'dvd_nr'}, |
86 |
$row->{'username'}, |
87 |
$row->{'date'}, |
88 |
$row->{'copies'}, $copies, |
89 |
$row->{'total_size'}, |
90 |
); |
91 |
} |
92 |
|
93 |
my @burned; |
94 |
my @not_burned; |
95 |
|
96 |
while (my $row = $sth->fetchrow_hashref) { |
97 |
if ($row->{'copies'}) { |
98 |
push @burned, fmt_archive($row); |
99 |
} else { |
100 |
push @not_burned, fmt_archive($row); |
101 |
} |
102 |
} |
103 |
|
104 |
my %Menu_archive = ( |
105 |
Label => 'Menu_archive', |
106 |
|
107 |
Item_1 => { |
108 |
Text => 'DVD #]Convey[', |
109 |
Convey => [ @not_burned ], |
110 |
Default => '*', |
111 |
}, |
112 |
Item_2 => { |
113 |
Text => 'DVD #]Convey[', |
114 |
Convey => [ @burned ], |
115 |
}, |
116 |
|
117 |
Select => 'Many', |
118 |
|
119 |
Banner => "Select one or more DVD media for burning", |
120 |
); |
121 |
|
122 |
my @archives_to_burn = Menu(\%Menu_archive); |
123 |
|
124 |
print "\n"; |
125 |
|
126 |
sub skip($) { |
127 |
my $msg = shift; |
128 |
print "WARNING: $msg, skipping...\n"; |
129 |
goto SKIP; |
130 |
} |
131 |
|
132 |
my $sth_archive_backup = $dbh->prepare( qq{ |
133 |
select |
134 |
backup_id, |
135 |
archive_id, |
136 |
hosts.name as host, |
137 |
shares.name as share, |
138 |
backups.num as num, |
139 |
from archive_backup |
140 |
join archive on archive_id = archive.id |
141 |
join backups on backup_id = backups.id |
142 |
join hosts on hostid = hosts.id |
143 |
join shares on shareid = shares.id |
144 |
where archive.dvd_nr = ? |
145 |
}); |
146 |
|
147 |
my $sth_archive_burned = $dbh->prepare( qq{ |
148 |
insert into archive_burned |
149 |
(archive_id, iso_size) |
150 |
values ( (select id from archive where dvd_nr =?), ?) |
151 |
}); |
152 |
|
153 |
foreach my $arc (@archives_to_burn) { |
154 |
exit if ($arc eq ']quit['); |
155 |
|
156 |
my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/); |
157 |
die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr); |
158 |
|
159 |
my $tmp_dir = "/$iso_dir/$dvd_nr"; |
160 |
|
161 |
my $t = time(); |
162 |
|
163 |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
164 |
|
165 |
my $list_file = my $iso_file = "${iso_dir}/${dvd_nr}"; |
166 |
$list_file .= '.list'; |
167 |
$iso_file .= '.iso'; |
168 |
|
169 |
if (-e $iso_file) { |
170 |
print "ISO $iso_file allready exists\n"; |
171 |
goto BURN; |
172 |
} |
173 |
|
174 |
$sth_archive_backup->execute($dvd_nr); |
175 |
|
176 |
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
177 |
|
178 |
my $inc = 0; |
179 |
|
180 |
while (my $row = $sth_archive_backup->fetchrow_hashref) { |
181 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
182 |
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
183 |
print $list "$tar_dir/$tar_file\n"; |
184 |
$inc++; |
185 |
|
186 |
} |
187 |
|
188 |
print "Running mkisofs now for $inc increments...\n"; |
189 |
|
190 |
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 }; |
191 |
|
192 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
193 |
|
194 |
my $size = (stat($iso_file))[7]; |
195 |
|
196 |
print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n"; |
197 |
|
198 |
BURN: |
199 |
# FIXME add call to cdrecord here! |
200 |
$sth_archive_burned->execute($dvd_nr, $size); |
201 |
print "Media burn for $dvd_nr recorded\n"; |
202 |
|
203 |
SKIP: |
204 |
|
205 |
} |
206 |
|
207 |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
208 |
#print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file "; |
209 |
|
210 |
$sth->finish; |
211 |
$dbh->disconnect; |
212 |
|