r8469@llin: dpavlin | 2005-10-11 19:55:44 +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 && -w $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 #----- main
59
60 my $sth = $dbh->prepare( qq{
61 select
62         id,dvd_nr,total_size,note,username,
63         archive.date as date,
64         count(archive_burned.archive_id) as copies
65 from archive
66         left outer join archive_burned on archive.id=archive_burned.archive_id
67 group by id,dvd_nr,total_size,note,username,archive.date
68 order by date asc
69 } );
70
71 $sth->execute();
72
73 sub fmt_archive($) {
74         my $row = shift || die;
75
76         $row->{'date'} =~ s/\.\d+$//;
77         $row->{'copies'} =~ s/^\s*0+\s*$/no/;
78         $row->{'total_size'} /= (1024*1024);
79
80         my $copies = 'copies';
81         $copies = 'copy' if ($row->{'copies'} == 1);
82
83         return
84                 sprintf("%d by %s on %s, %s %s [%.2f Mb]",
85                         $row->{'dvd_nr'},
86                         $row->{'username'},
87                         $row->{'date'},
88                         $row->{'copies'}, $copies,
89                         $row->{'total_size'},
90                 );
91 }
92
93 my @burned;
94 my @not_burned;
95
96 while (my $row = $sth->fetchrow_hashref) {
97         if ($row->{'copies'}) {
98                 push @burned, fmt_archive($row);
99         } else {
100                 push @not_burned, fmt_archive($row);
101         }
102 }
103
104 my %Menu_archive = (
105         Label => 'Menu_archive',
106
107         Item_1 => {
108                 Text => 'DVD #]Convey[',
109                 Convey => [ @not_burned ],
110                 Default => '*',
111         },
112         Item_2 => {
113                 Text => 'DVD #]Convey[',
114                 Convey => [ @burned ],
115         },
116
117         Select => 'Many',
118
119         Banner => "Select one or more DVD media for burning",
120 );
121
122 my @archives_to_burn = Menu(\%Menu_archive);
123
124 print "\n";
125
126 sub skip($) {
127         my $msg = shift;
128         print "WARNING: $msg, skipping...\n";
129         goto SKIP;
130 }
131
132 my $sth_archive_backup = $dbh->prepare( qq{
133         select
134                 backup_id,
135                 hosts.name as host,
136                 shares.name as share,
137                 backups.num as num
138         from archive_backup
139         join archive on archive_id = archive.id
140         join backups on backup_id = backups.id
141         join hosts on hostid = hosts.id
142         join shares on shareid = shares.id
143         where archive.dvd_nr = ?
144 });
145
146 foreach my $arc (@archives_to_burn) {
147         exit if ($arc eq ']quit[');
148
149         my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
150         die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
151
152         my $tmp_dir = "/$iso_dir/$dvd_nr";
153
154         my $t = time();
155
156         print "Working on DVD #$dvd_nr in $tmp_dir\n";
157
158         my $iso_file = "${iso_dir}/${dvd_nr}.iso";
159
160         skip "ISO $iso_file allready exists" if (-e $iso_file);
161
162         $sth_archive_backup->execute($dvd_nr);
163
164         print "Running mkisofs now...\n";
165
166         my $cmd = qq{ mkisofs -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $dvd_nr -o $iso_file -path-list - };
167
168         open(my $mkisofs, "| $cmd 2>&1") || skip "can't run $cmd: $!";
169
170         while (my $row = $sth_archive_backup->fetchrow_hashref) {
171                 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
172                 skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
173                 print $mkisofs "$tar_dir/$tar_file\n";
174         }
175
176         while(! -e $iso_file) {
177                 sleep 1;
178         }
179
180         print "Created $iso_file in ", fmt_time(time() - $t), "\n";
181
182 SKIP:
183
184 }
185
186 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
187 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
188
189 $sth->finish;
190 $dbh->disconnect;
191