r8500@llin: dpavlin | 2005-10-13 17:11:38 +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
13 use Data::Dumper;
14
15 my $debug = 0;
16 $|=1;
17
18 my $start_t = time();
19
20 my $t_fmt = '%Y-%m-%d %H:%M:%S';
21
22 # don't check for user
23 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
24 my %Conf = $bpc->Conf();
25 %BackupPC::SearchLib::Conf = %Conf;
26
27 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
28 my $user = $Conf{SearchUser} || '';
29
30 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
31
32 my $tar_dir = $Conf{InstallDir}.'/';
33 $tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration";
34
35 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir);
36
37 my $iso_dir = $Conf{InstallDir}.'/';
38 $iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration";
39 die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir);
40
41 #---- subs ----
42
43 sub fmt_time {
44         my $t = shift || return;
45         my $out = "";
46         my ($ss,$mm,$hh) = gmtime($t);
47         $out .= "${hh}h" if ($hh);
48         $out .= sprintf("%02d:%02d", $mm,$ss);
49         return $out;
50 }
51
52 sub curr_time {
53         return strftime($t_fmt,localtime());
54 }
55
56 sub dumpArchive2XML($$$)
57 {
58         my ($dbh, $dvd_nr, $filename) = @_;
59         my %archive;    
60         my $output = new IO::File(">$filename");
61         my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
62
63         $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
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                 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
120
121                 $writer->startTag( "backup", %{$row} );
122
123                 while (my $row = $sth_files->fetchrow_hashref()) {
124                         # convert filetype to string
125                         $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
126                         $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
127                         my $path = $row->{'path'}; delete $row->{'path'};
128
129                         $writer->startTag("file", %{$row});
130                         $writer->characters( $path );
131                         $writer->endTag("file");
132                 }
133                 $writer->endTag("backup");
134         }       
135                          
136         $writer->endTag("archive");
137         $writer->end(); 
138         
139 }
140
141
142
143 #----- main
144
145 my $sth = $dbh->prepare( qq{
146 select
147         id,dvd_nr,total_size,note,username,
148         archive.date as date,
149         count(archive_burned.archive_id) as copies
150 from archive
151         left outer join archive_burned on archive.id=archive_burned.archive_id
152 group by id,dvd_nr,total_size,note,username,archive.date
153 order by date asc
154 } );
155
156 $sth->execute();
157
158 sub fmt_archive($) {
159         my $row = shift || die;
160
161         $row->{'date'} =~ s/\.\d+$//;
162         $row->{'copies'} =~ s/^\s*0+\s*$/no/;
163         $row->{'total_size'} /= (1024*1024);
164
165         my $copies = 'copies';
166         $copies = 'copy' if ($row->{'copies'} == 1);
167
168         return
169                 sprintf("%d by %s on %s, %s %s [%.2f Mb]",
170                         $row->{'dvd_nr'},
171                         $row->{'username'},
172                         $row->{'date'},
173                         $row->{'copies'}, $copies,
174                         $row->{'total_size'},
175                 );
176 }
177
178 my @burned;
179 my @not_burned;
180
181 while (my $row = $sth->fetchrow_hashref) {
182         if ($row->{'copies'}) {
183                 push @burned, fmt_archive($row);
184         } else {
185                 push @not_burned, fmt_archive($row);
186         }
187 }
188
189 my %Menu_archive = (
190         Label => 'Menu_archive',
191
192         Item_1 => {
193                 Text => 'DVD #]Convey[',
194                 Convey => [ @not_burned ],
195                 Default => '*',
196         },
197         Item_2 => {
198                 Text => 'DVD #]Convey[',
199                 Convey => [ @burned ],
200         },
201
202         Select => 'Many',
203
204         Banner => "Select one or more DVD media for burning",
205 );
206
207 my @archives_to_burn = Menu(\%Menu_archive);
208
209 print "\n";
210
211 sub skip($) {
212         my $msg = shift;
213         print "WARNING: $msg, skipping...\n";
214         goto SKIP;
215 }
216
217 my $sth_archive_backup = $dbh->prepare( qq{
218         select
219                 backup_id,
220                 archive_id,
221                 hosts.name as host,
222                 shares.name as share,
223                 backups.num as num
224         from archive_backup
225         join archive on archive_id = archive.id
226         join backups on backup_id = backups.id
227         join hosts on hostid = hosts.id
228         join shares on shareid = shares.id
229         where archive.dvd_nr = ?
230 });
231
232 my $sth_archive_burned = $dbh->prepare( qq{
233         insert into archive_burned
234                 (archive_id, iso_size)
235         values ( (select id from archive where dvd_nr =?), ?)
236 });
237
238 foreach my $arc (@archives_to_burn) {
239         exit if ($arc eq ']quit[');
240
241         my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
242         die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
243
244         my $tmp_dir = "/$iso_dir/$dvd_nr";
245
246         my $t = time();
247
248         print "Working on DVD #$dvd_nr in $tmp_dir\n";
249
250         my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
251         $list_file .= '.list';
252         $iso_file .= '.iso';
253         $xml_file .= '.xml';
254
255         if (-e $iso_file) {
256                 print "ISO $iso_file allready exists\n";
257                 goto BURN;
258         }
259
260         $sth_archive_backup->execute($dvd_nr);
261
262         open(my $list, "> $list_file") || skip "can't open $list_file: $!";
263
264         my $inc = 0;
265
266         while (my $row = $sth_archive_backup->fetchrow_hashref) {
267                 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
268                 skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
269                 print $list "$tar_dir/$tar_file\n";
270                 $inc++;
271
272         }
273
274         # add file list and note in xml
275         dumpArchive2XML($dbh, $dvd_nr, $xml_file);
276         print $list "$xml_file\n";
277
278         # add css file for archive
279         my $css_file = $Conf{CgiImageDir} . '/archive.css';
280         if (-r $css_file) {
281                 print $list "$css_file\n";
282         } else {
283                 print "WARNING: missing $css_file, not added to iso image!\n";
284         }
285
286         close($list);
287
288         print "Running mkisofs now for $inc increments...\n";
289
290         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 };
291
292         system($cmd) == 0 or skip "can't run $cmd: $?";
293
294         my $size = (stat($iso_file))[7];
295
296         print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n";
297
298 BURN:
299         # FIXME add call to cdrecord here!
300         $sth_archive_burned->execute($dvd_nr, $size);
301         print "Media burn for $dvd_nr recorded\n";
302
303 SKIP:
304
305 }
306
307 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
308 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
309
310 $sth->finish;
311 $dbh->disconnect;
312