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 |
sub dumpArchive2XML($$$) |
59 |
{ |
60 |
my ($dbh, $dvd_nr, $filename) = @_; |
61 |
my %archive; |
62 |
my $output = new IO::File(">$filename"); |
63 |
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
64 |
|
65 |
my $files_sql = q{ |
66 |
SELECT |
67 |
files.path AS path, |
68 |
files.date AS date, |
69 |
files.type AS type, |
70 |
files.size AS size |
71 |
FROM files, backups, shares |
72 |
WHERE files.backupnum=backups.num AND |
73 |
files.shareid=shares.id AND |
74 |
shares.hostid=backups.hostid AND |
75 |
backups.id=? |
76 |
}; |
77 |
|
78 |
my $backups_sql = q{ |
79 |
SELECT |
80 |
backups.id AS backup_id, |
81 |
hosts.name AS host, |
82 |
backups.num AS num, |
83 |
backups.date AS date, |
84 |
shares.name AS share, |
85 |
backups.size AS backup_size, |
86 |
backups.inc_size AS compress_size |
87 |
FROM backups, archive_backup, hosts, shares |
88 |
WHERE archive_backup.backup_id = backups.id |
89 |
AND hosts.id=backups.hostid |
90 |
AND shares.id=backups.shareid |
91 |
AND archive_backup.archive_id = ? |
92 |
}; |
93 |
|
94 |
my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?"); |
95 |
$sth->execute($dvd_nr); |
96 |
my $row = $sth->fetchrow_hashref(); |
97 |
|
98 |
my $row_h; |
99 |
foreach my $hide (qw/id note/) { |
100 |
$row_h->{$hide} = $row->{$hide} || die "no $hide"; |
101 |
delete $row->{$hide}; |
102 |
} |
103 |
|
104 |
$writer->startTag("archive", %{$row}); |
105 |
|
106 |
$writer->startTag("note"); |
107 |
$writer->characters( $row_h->{'note'} ); |
108 |
$writer->endTag("note"); |
109 |
|
110 |
my $sth_backups = $dbh->prepare( $backups_sql ); |
111 |
$sth_backups->execute( $row_h->{'id'} ); |
112 |
|
113 |
while (my $row = $sth_backups->fetchrow_hashref()) { |
114 |
|
115 |
my $sth_files = $dbh->prepare( $files_sql); |
116 |
$sth_files->execute($row->{'backup_id'}); |
117 |
|
118 |
delete($row->{'backup_id'}); |
119 |
|
120 |
$writer->startTag( "backup", %{$row} ); |
121 |
|
122 |
while (my $row = $sth_files->fetchrow_hashref()) { |
123 |
# convert filetype to string |
124 |
$row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'}); |
125 |
my $path = $row->{'path'}; delete $row->{'path'}; |
126 |
|
127 |
$writer->startTag("file", %{$row}); |
128 |
$writer->characters( $path ); |
129 |
$writer->endTag("file"); |
130 |
} |
131 |
$writer->endTag("backup"); |
132 |
} |
133 |
|
134 |
$writer->endTag("archive"); |
135 |
$writer->end(); |
136 |
|
137 |
} |
138 |
|
139 |
|
140 |
|
141 |
#----- main |
142 |
|
143 |
my $sth = $dbh->prepare( qq{ |
144 |
select |
145 |
id,dvd_nr,total_size,note,username, |
146 |
archive.date as date, |
147 |
count(archive_burned.archive_id) as copies |
148 |
from archive |
149 |
left outer join archive_burned on archive.id=archive_burned.archive_id |
150 |
group by id,dvd_nr,total_size,note,username,archive.date |
151 |
order by date asc |
152 |
} ); |
153 |
|
154 |
$sth->execute(); |
155 |
|
156 |
sub fmt_archive($) { |
157 |
my $row = shift || die; |
158 |
|
159 |
$row->{'date'} =~ s/\.\d+$//; |
160 |
$row->{'copies'} =~ s/^\s*0+\s*$/no/; |
161 |
$row->{'total_size'} /= (1024*1024); |
162 |
|
163 |
my $copies = 'copies'; |
164 |
$copies = 'copy' if ($row->{'copies'} == 1); |
165 |
|
166 |
return |
167 |
sprintf("%d by %s on %s, %s %s [%.2f Mb]", |
168 |
$row->{'dvd_nr'}, |
169 |
$row->{'username'}, |
170 |
$row->{'date'}, |
171 |
$row->{'copies'}, $copies, |
172 |
$row->{'total_size'}, |
173 |
); |
174 |
} |
175 |
|
176 |
my @burned; |
177 |
my @not_burned; |
178 |
|
179 |
while (my $row = $sth->fetchrow_hashref) { |
180 |
if ($row->{'copies'}) { |
181 |
push @burned, fmt_archive($row); |
182 |
} else { |
183 |
push @not_burned, fmt_archive($row); |
184 |
} |
185 |
} |
186 |
|
187 |
my %Menu_archive = ( |
188 |
Label => 'Menu_archive', |
189 |
|
190 |
Item_1 => { |
191 |
Text => 'DVD #]Convey[', |
192 |
Convey => [ @not_burned ], |
193 |
Default => '*', |
194 |
}, |
195 |
Item_2 => { |
196 |
Text => 'DVD #]Convey[', |
197 |
Convey => [ @burned ], |
198 |
}, |
199 |
|
200 |
Select => 'Many', |
201 |
|
202 |
Banner => "Select one or more DVD media for burning", |
203 |
); |
204 |
|
205 |
my @archives_to_burn = Menu(\%Menu_archive); |
206 |
|
207 |
print "\n"; |
208 |
|
209 |
sub skip($) { |
210 |
my $msg = shift; |
211 |
print "WARNING: $msg, skipping...\n"; |
212 |
goto SKIP; |
213 |
} |
214 |
|
215 |
my $sth_archive_backup = $dbh->prepare( qq{ |
216 |
select |
217 |
backup_id, |
218 |
archive_id, |
219 |
hosts.name as host, |
220 |
shares.name as share, |
221 |
backups.num as num |
222 |
from archive_backup |
223 |
join archive on archive_id = archive.id |
224 |
join backups on backup_id = backups.id |
225 |
join hosts on hostid = hosts.id |
226 |
join shares on shareid = shares.id |
227 |
where archive.dvd_nr = ? |
228 |
}); |
229 |
|
230 |
my $sth_archive_burned = $dbh->prepare( qq{ |
231 |
insert into archive_burned |
232 |
(archive_id, iso_size) |
233 |
values ( (select id from archive where dvd_nr =?), ?) |
234 |
}); |
235 |
|
236 |
foreach my $arc (@archives_to_burn) { |
237 |
exit if ($arc eq ']quit['); |
238 |
|
239 |
my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/); |
240 |
die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr); |
241 |
|
242 |
my $tmp_dir = "/$iso_dir/$dvd_nr"; |
243 |
|
244 |
my $t = time(); |
245 |
|
246 |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
247 |
|
248 |
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
249 |
$list_file .= '.list'; |
250 |
$iso_file .= '.iso'; |
251 |
$xml_file .= '.xml'; |
252 |
|
253 |
if (-e $iso_file) { |
254 |
print "ISO $iso_file allready exists\n"; |
255 |
goto BURN; |
256 |
} |
257 |
|
258 |
$sth_archive_backup->execute($dvd_nr); |
259 |
|
260 |
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
261 |
|
262 |
my $inc = 0; |
263 |
|
264 |
while (my $row = $sth_archive_backup->fetchrow_hashref) { |
265 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
266 |
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
267 |
print $list "$tar_dir/$tar_file\n"; |
268 |
$inc++; |
269 |
|
270 |
} |
271 |
|
272 |
# FIXME add file list in xml and txt and note file |
273 |
|
274 |
dumpArchive2XML($dbh, $dvd_nr, $xml_file); |
275 |
print $list "$xml_file\n"; |
276 |
|
277 |
close($list); |
278 |
|
279 |
print "Running mkisofs now for $inc increments...\n"; |
280 |
|
281 |
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 }; |
282 |
|
283 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
284 |
|
285 |
my $size = (stat($iso_file))[7]; |
286 |
|
287 |
print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n"; |
288 |
|
289 |
BURN: |
290 |
# FIXME add call to cdrecord here! |
291 |
$sth_archive_burned->execute($dvd_nr, $size); |
292 |
print "Media burn for $dvd_nr recorded\n"; |
293 |
|
294 |
SKIP: |
295 |
|
296 |
} |
297 |
|
298 |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
299 |
#print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file "; |
300 |
|
301 |
$sth->finish; |
302 |
$dbh->disconnect; |
303 |
|