/[BackupPC]/trunk/bin/BackupPC_burnArchiveCLI
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/bin/BackupPC_burnArchiveCLI

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (show annotations)
Sat Oct 15 14:11:46 2005 UTC (18 years, 6 months ago) by dpavlin
File size: 10103 byte(s)
create diskname in form "DVD_ID part/total_parts" (e.g. 66 1/3)

1 #!/usr/bin/perl
2
3 use strict;
4 use lib "__INSTALLDIR__/lib";
5
6 use DBI;
7 use BackupPC::Lib;
8 use BackupPC::SearchLib;
9 use Time::HiRes qw/time/;
10 use POSIX qw/strftime/;
11 use Term::Menus;
12 use File::Which;
13
14 use Data::Dumper;
15
16 my $debug = 0;
17 $|=1;
18
19 # don't check for user
20 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
21 my %Conf = $bpc->Conf();
22 %BackupPC::SearchLib::Conf = %Conf;
23
24 my $conf_bin;
25
26 $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
27 my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
28 $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
29 my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
30 $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
31
32
33 my $bin;
34 foreach my $c (qw/cdrecord eject mkisofs/) {
35 $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
36 }
37
38 my $start_t = time();
39
40 my $t_fmt = '%Y-%m-%d %H:%M:%S';
41
42
43 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
44 my $user = $Conf{SearchUser} || '';
45
46 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
47
48 my $tar_dir = $Conf{InstallDir}.'/';
49 $tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration";
50
51 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir);
52
53 my $iso_dir = $Conf{InstallDir}.'/';
54 $iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration";
55 die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir);
56
57 #---- subs ----
58
59 sub fmt_time {
60 my $t = shift || return;
61 my $out = "";
62 my ($ss,$mm,$hh) = gmtime($t);
63 $out .= "${hh}h" if ($hh);
64 $out .= sprintf("%02d:%02d", $mm,$ss);
65 return $out;
66 }
67
68 sub curr_time {
69 return strftime($t_fmt,localtime());
70 }
71
72 sub dumpArchive2XML($$$)
73 {
74 my ($dbh, $dvd_nr, $filename) = @_;
75 my %archive;
76
77 my $output = new IO::File('> '.$filename.'.tmp');
78 my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
79
80 print "Dumping file list for DVD $dvd_nr, countdown:";
81
82 $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
83
84 my $files_sql = q{
85 SELECT
86 files.path AS path,
87 files.date AS date,
88 files.type AS type,
89 files.size AS size
90 FROM files, backups, shares
91 WHERE files.backupnum=backups.num AND
92 files.shareid=shares.id AND
93 shares.hostid=backups.hostid AND
94 backups.id=?
95 };
96
97 my $backups_sql = q{
98 SELECT
99 backups.id AS backup_id,
100 hosts.name AS host,
101 backups.num AS num,
102 backups.date AS date,
103 shares.name AS share,
104 backups.size AS backup_size,
105 backups.inc_size AS compress_size
106 FROM backups, archive_backup, hosts, shares
107 WHERE archive_backup.backup_id = backups.id
108 AND hosts.id=backups.hostid
109 AND shares.id=backups.shareid
110 AND archive_backup.archive_id = ?
111 };
112
113 my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
114 $sth->execute($dvd_nr);
115 my $row = $sth->fetchrow_hashref();
116
117 my $row_h;
118 foreach my $hide (qw/id note/) {
119 $row_h->{$hide} = $row->{$hide} || '';
120 delete $row->{$hide};
121 }
122
123 $writer->startTag("archive", %{$row});
124
125 $writer->startTag("note");
126 $writer->characters( $row_h->{'note'} );
127 $writer->endTag("note");
128
129 my $sth_backups = $dbh->prepare( $backups_sql );
130 $sth_backups->execute( $row_h->{'id'} );
131
132 my $total_backups = $sth_backups->rows;
133 my $curr_backup = 0;
134 my $last_pcnt = 0;
135
136 while (my $row = $sth_backups->fetchrow_hashref()) {
137
138 $curr_backup++;
139 my $pcnt = int(($curr_backup * 10) / $total_backups);
140 if ($pcnt > $last_pcnt) {
141 print " ",(10 - $pcnt);
142 $last_pcnt = $pcnt;
143 }
144
145 my $sth_files = $dbh->prepare( $files_sql);
146 $sth_files->execute($row->{'backup_id'});
147
148 delete($row->{'backup_id'});
149 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
150
151 $writer->startTag( "backup", %{$row} );
152
153 while (my $row = $sth_files->fetchrow_hashref()) {
154 # convert filetype to string
155 $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
156 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
157 my $path = $row->{'path'}; delete $row->{'path'};
158
159 $writer->startTag("file", %{$row});
160 $writer->characters( $path );
161 $writer->endTag("file");
162 }
163 $writer->endTag("backup");
164 }
165
166 $writer->endTag("archive");
167 $writer->end();
168
169 rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
170
171 print "\n";
172 }
173
174
175
176 #----- main
177
178 my $sth = $dbh->prepare( qq{
179 select
180 id,dvd_nr,total_size,note,username,
181 archive.date as date,
182 count(archive_burned.archive_id) as copies
183 from archive
184 left outer join archive_burned on archive.id=archive_burned.archive_id
185 group by id,dvd_nr,total_size,note,username,archive.date
186 order by date asc
187 } );
188
189 $sth->execute();
190
191 sub fmt_archive($) {
192 my $row = shift || die;
193
194 $row->{'date'} =~ s/\.\d+$//;
195 $row->{'copies'} =~ s/^\s*0+\s*$/no/;
196 $row->{'total_size'} /= (1024*1024);
197
198 my $copies = 'copies';
199 $copies = 'copy' if ($row->{'copies'} == 1);
200
201 return
202 sprintf("%d by %s on %s, %s %s [%.2f Mb]",
203 $row->{'dvd_nr'},
204 $row->{'username'},
205 $row->{'date'},
206 $row->{'copies'}, $copies,
207 $row->{'total_size'},
208 );
209 }
210
211 my @burned;
212 my @not_burned;
213
214 while (my $row = $sth->fetchrow_hashref) {
215 if ($row->{'copies'}) {
216 push @burned, fmt_archive($row);
217 } else {
218 push @not_burned, fmt_archive($row);
219 }
220 }
221
222 my %Menu_archive = (
223 Label => 'Menu_archive',
224
225 Item_1 => {
226 Text => 'DVD #]Convey[',
227 Convey => [ @not_burned ],
228 Default => '*',
229 },
230 Item_2 => {
231 Text => 'DVD #]Convey[',
232 Convey => [ @burned ],
233 },
234
235 Select => 'Many',
236
237 Banner => "Select one or more DVD media for burning",
238 );
239
240 my @archives_to_burn = Menu(\%Menu_archive);
241
242 print "\n";
243
244 sub skip($) {
245 my $msg = shift;
246 print "WARNING: $msg, skipping...\n";
247 goto SKIP;
248 }
249
250 my $sth_archive_backup = $dbh->prepare( qq{
251 select
252 backup_id,
253 archive_id,
254 hosts.name as host,
255 shares.name as share,
256 backups.num as num,
257 backups.parts as parts
258 from archive_backup
259 join archive on archive_id = archive.id
260 join backups on backup_id = backups.id
261 join hosts on hostid = hosts.id
262 join shares on shareid = shares.id
263 where archive.dvd_nr = ?
264 });
265
266 my $sth_archive_burned = $dbh->prepare( qq{
267 insert into archive_burned
268 (archive_id, iso_size, part, copy)
269 values ( (select id from archive where dvd_nr =?), ?, ?, ?)
270 });
271
272 foreach my $arc (@archives_to_burn) {
273 exit if ($arc eq ']quit[');
274
275 my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
276 die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
277
278 my $tmp_dir = "/$iso_dir/$dvd_nr";
279
280 my $t = time();
281
282 print "Working on DVD #$dvd_nr in $tmp_dir\n";
283
284 my $part_path = '';
285 my $part_nr = 1;
286 my $parts = 0;
287
288 $sth_archive_backup->execute($dvd_nr);
289
290 my $disk_name = 'unknown disk';
291 my $iso_size = 0;
292
293 do {
294
295 # do we need to re-execute query for multi-part increments?
296 $sth_archive_backup->execute($dvd_nr) if ($parts > 1);
297
298 my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";
299
300
301 $parts = $row->{'parts'} || die "no parts?";
302 $disk_name = $dvd_nr;
303
304 if ($parts > 1) {
305 # parts start with 0, so we have -1 here
306 $part_path = '.' . ($part_nr - 1);
307 $disk_name .= ' ' . $part_nr . '/' . $parts;
308 }
309
310 print "Processing part $part_nr/$parts\n";
311
312 my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
313 $list_file .= $part_path . '.list';
314 $iso_file .= $part_path . '.iso';
315 $xml_file .= '.xml';
316
317 #
318 # check if ISO file exists
319 #
320
321 if (! -e $iso_file) {
322
323 open(my $list, "> $list_file") || skip "can't open $list_file: $!";
324
325 my $inc = 0;
326
327 do {
328 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
329 if (-f "$tar_dir/$tar_file") {
330 skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
331 print $list "$tar_dir/$tar_file\n";
332 } else {
333 my $len = length("$parts");
334 my $nr = sprintf("%0${len}d", ($part_nr - 1));
335 print $list "$tar_dir/$tar_file/part$nr\n";
336 }
337 $inc++;
338 } while ($row = $sth_archive_backup->fetchrow_hashref);
339
340 # add file list and note in xml
341 dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
342 print $list "$xml_file\n";
343
344 # add css file for archive
345 my $css_file = $Conf{CgiImageDir} . '/archive.css';
346 if (-r $css_file) {
347 print $list "$css_file\n";
348 } else {
349 print "WARNING: missing $css_file, not added to iso image!\n";
350 }
351
352 close($list);
353
354 print "Running mkisofs now for $inc increments, disk $disk_name\n";
355
356 my $cmd = $bin->{'mkisofs'} . qq{ -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V "$disk_name" -o ${iso_file}.tmp -path-list $list_file };
357
358 system($cmd) == 0 or skip "can't run $cmd: $?";
359
360 rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
361
362 $iso_size = (stat($iso_file))[7];
363
364 print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
365
366 } else {
367 print "ISO $iso_file allready exists\n";
368 }
369
370 my $copies = $Conf{BurnMultipleCopies} || 1;
371
372 foreach my $copy_nr ( 1 .. $copies ) {
373
374 print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
375
376 system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
377
378 my $wait = <STDIN>;
379
380 my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
381
382 # FIXME
383 print "## $cmd\n";
384 system($cmd) == 0 or skip "can't run $cmd: $?";
385
386 print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
387
388 $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr);
389
390 print "Media burn for $disk_name copy $copy_nr recorded\n";
391 }
392
393 $part_nr++;
394 } until ($part_nr > $parts);
395
396 SKIP:
397
398 }
399
400 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
401 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
402
403 print "Recoding finished, exiting...\n";
404
405 $sth->finish;
406 $sth_archive_backup->finish;
407 $sth_archive_burned->finish;
408
409 $dbh->disconnect;
410

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26