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