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