c6f2d40b62b224cd39b98dc9bc8b47288c12f3c0
[BackupPC.git] / lib / BackupPC / CGI / Archive.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::Archive package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the Archive action for the CGI interface.
8 #
9 # AUTHOR
10 #   Craig Barratt  <cbarratt@users.sourceforge.net>
11 #
12 # COPYRIGHT
13 #   Copyright (C) 2003-2009  Craig Barratt
14 #
15 #   This program is free software; you can redistribute it and/or modify
16 #   it under the terms of the GNU General Public License as published by
17 #   the Free Software Foundation; either version 2 of the License, or
18 #   (at your option) any later version.
19 #
20 #   This program is distributed in the hope that it will be useful,
21 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
22 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 #   GNU General Public License for more details.
24 #
25 #   You should have received a copy of the GNU General Public License
26 #   along with this program; if not, write to the Free Software
27 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 #
29 #========================================================================
30 #
31 # Version 3.2.0, released 31 Jul 2010.
32 #
33 # See http://backuppc.sourceforge.net.
34 #
35 #========================================================================
36
37 package BackupPC::CGI::Archive;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41 use Data::Dumper;
42 use DBI;
43
44 sub action
45 {
46     my $archHost = $In{host};
47     my $Privileged = CheckPermission();
48
49     if ( !$Privileged ) {
50         ErrorExit($Lang->{Only_privileged_users_can_archive} );
51     }
52     if ( $In{type} == 0 ) {
53         my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
54            $strNone, $strGood, $hostCntGood, $hostCntNone, $checkBoxCnt,
55            $backupnumber);
56
57         $hostCntGood = $hostCntNone = $checkBoxCnt = $fullSizeTot = 0;
58         GetStatusInfo("hosts");
59
60         foreach my $host ( sort(keys(%Status)) ) {
61             my($fullDur, $incrCnt, $fullSize, $fullRate);
62             my @Backups = $bpc->BackupInfoRead($host);
63             my $fullCnt = $incrCnt = 0;
64             my $all_backup_numbers;
65
66             for ( my $i = 0 ; $i < @Backups ; $i++ ) {
67                 if ( $Backups[$i]{type} eq "full" ) {
68                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
69                     $incrSizeTot = 0;
70                 } else {
71                     $incrSizeTot = $Backups[$i]{size} / (1024 * 1024);
72                 }
73                 $backupnumber = $Backups[$i]{num};
74                 $all_backup_numbers->{$backupnumber}++;
75             }
76             $fullSizeTot += $fullSize + $incrSizeTot;
77             $fullSize = sprintf("%.2f", ($fullSize + $incrSizeTot) / 1000);
78
79                 $bpc->ConfigRead($archHost);
80                 %Conf = $bpc->Conf();
81
82 use Data::Dump qw(dump);
83
84                 my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
85                 my $sth = $dbh->prepare(qq{
86                         select
87                                 backups.num as num,
88                                 inc_size,
89                                 size,
90                                 inc_deleted
91                         from backups
92                         join hosts on hosts.id = hostid
93                         where hosts.name = ?
94                 });
95                 $sth->execute($host);
96                 # and inc_size < 0 and size > 0 and not inc_deleted
97                 while( my $row = $sth->fetchrow_hashref ) {
98 warn "# row ",dump $row;
99                         $all_backup_numbers->{ $row->{num} } =
100                         $row->{inc_deleted}  ? 0 :
101                         $row->{size}    == 0 ? 0 :
102                         $row->{inc_size} < 0 ? 1 :
103                         $row->{size}     > 0 ? 1 :
104                         0;
105                 }
106
107 warn dump($all_backup_numbers);
108                 my @backup_nums = 
109                         sort
110                         grep { $all_backup_numbers->{$_} }
111                         keys %$all_backup_numbers;
112
113                 my $checkboxes;
114                 foreach my $backupnumber ( @backup_nums ) {
115                         $checkboxes .= qq|
116 <input type="hidden" name="fcb$checkBoxCnt" value="$host">
117 <input type="checkbox" name="backup$checkBoxCnt" value="$backupnumber">$backupnumber
118                         |;
119                         $checkBoxCnt++;
120                 }
121
122 =for ASA extension removed this
123             $str = <<EOF;
124 <tr>
125 <td class="border"><input type="hidden" name="backup$checkBoxCnt" value="$backupnumber"><input type="checkbox" name="fcb$checkBoxCnt" value="$host">&nbsp;${HostLink($host)} </td>
126 <td align="center" class="border"> ${UserLink($Hosts->{$host}{user})} </td>
127 <td align="center" class="border"> $fullSize </td>
128 EOF
129             $checkBoxCnt++;
130 =cut
131
132             $str = <<EOF;
133 <tr>
134 <td class="border">
135 ${HostLink($host)}
136 $checkboxes
137 </td>
138 <td align="center" class="border">
139 ${UserLink($Hosts->{$host}{user})}
140 </td>
141 <td align="center" class="border"> $fullSize </td>
142 EOF
143
144             if ( @Backups == 0 ) {
145                 $hostCntNone++;
146                 $strNone .= $str;
147             } else {
148                 $hostCntGood++;
149                 $strGood .= $str;
150             }
151         }
152         $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
153         my $now      = timeStamp2(time);
154         my $checkAllHosts = $Lang->{checkAllHosts};
155         $strGood .= <<EOF;
156 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
157 EOF
158         my $content = eval("qq{$Lang->{BackupPC_Archive}}");
159         Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
160         Trailer();
161     } else {
162         my(@HostList, @BackupList, $HostListStr, $hiddenStr, $pathHdr,
163            $badFileCnt, $reply, $str);
164
165         #
166         # Pick up the archive host's config file
167         #
168         $bpc->ConfigRead($archHost);
169         %Conf = $bpc->Conf();
170
171         my $args = {
172             SplitPath    => $Conf{SplitPath},
173             ParPath      => $Conf{ParPath},
174             CatPath      => $Conf{CatPath},
175             GzipPath     => $Conf{GzipPath},
176             Bzip2Path    => $Conf{Bzip2Path},
177             ArchiveDest  => $Conf{ArchiveDest},
178             ArchiveComp  => $Conf{ArchiveComp},
179             ArchivePar   => $Conf{ArchivePar},
180             ArchiveSplit => $Conf{ArchiveSplit},
181             topDir       => $bpc->{TopDir},
182         };
183
184         ServerConnect();
185
186         for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
187             next if ( !defined($In{"fcb$i"}) );
188             my $name = $In{"fcb$i"};
189             my $backupno = $In{"backup$i"};
190             next unless defined $backupno; # ASA - skip hosts without backups checked
191             push(@HostList, $name);
192             push(@BackupList, $backupno);
193             $hiddenStr .= <<EOF;
194 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
195 <input type="hidden" name="backup$i" value="$In{'backup' . $i}">
196 EOF
197             $HostListStr .= <<EOF;
198 <li> ${EscHTML($name)} $i
199 EOF
200         }
201         $hiddenStr .= <<EOF;
202 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
203 EOF
204         $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
205         if ( @HostList == 0 ) {
206             ErrorExit($Lang->{You_haven_t_selected_any_hosts});
207         }
208         my ($ArchiveDest, $ArchiveCompNone, $ArchiveCompGzip,
209             $ArchiveCompBzip2, $ArchivePar, $ArchiveSplit);
210         $ArchiveDest = $Conf{ArchiveDest};
211         if ( $Conf{ArchiveComp} eq "none" ) {
212             $ArchiveCompNone   = "checked";
213         } else {
214             $ArchiveCompNone   = "";
215         }
216         if ( $Conf{ArchiveComp} eq "gzip" ) {
217             $ArchiveCompGzip   = "checked";
218         } else {
219             $ArchiveCompGzip   = "";
220         }
221         if ( $Conf{ArchiveComp} eq "bzip2" ) {
222             $ArchiveCompBzip2  = "checked";
223         } else {
224             $ArchiveCompBzip2  = "";
225         }
226         $ArchivePar   = $Conf{ArchivePar};
227         $ArchiveSplit = $Conf{ArchiveSplit};
228
229         if ( $In{type} == 1 ) {
230             #
231             # Tell the user what options they have
232             #
233             my $paramStr = "";
234             if ( $Conf{ArchiveClientCmd} =~ /\$archiveloc\b/ ) {
235                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_location}}");
236             }
237             if ( $Conf{ArchiveClientCmd} =~ /\$compression\b/ ) {
238                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_compression}}");
239             }
240             if ( $Conf{ArchiveClientCmd} =~ /\$parfile\b/
241                     && -x $Conf{ParPath} ) {
242                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_parity}}");
243             }
244             if ( $Conf{ArchiveClientCmd} =~ /\$splitsize\b/
245                     && -x $Conf{SplitPath} ) {
246                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_split}}");
247             }
248             my $content = eval("qq{$Lang->{BackupPC_Archive2}}");
249             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
250             Trailer();
251         } elsif ( $In{type} == 2 ) {
252             my $reqFileName;
253             my $archivehost = $1 if ( $In{archivehost} =~ /(.+)/ );
254             for ( my $i = 0 ; ; $i++ ) {
255                 $reqFileName = "archiveReq.$$.$i";
256                 last if ( !-f "$TopDir/pc/$archivehost/$reqFileName" );
257             }
258             my($compname, $compext);
259             if ( $In{compression} == 2 ) {          # bzip2 compression
260                 $compname = $Conf{Bzip2Path};
261                 $compext = '.bz2';
262             } elsif ( $In{compression} == 1 ) {     # gzip compression
263                 $compname = $Conf{GzipPath};
264                 $compext = '.gz';
265             } else { # No Compression
266                 $compname = $Conf{CatPath};
267                 $compext = '.raw';
268             }
269             my $fullsplitsize = $In{splitsize} . '000000';
270             my %ArchiveReq = (
271                 # parameters for the archive
272                 archiveloc  => $In{archive_device},
273                 archtype    => $In{archive_type},
274                 compression => $compname,
275                 compext     => $compext,
276                 parfile     => $In{par},
277                 splitsize   => $fullsplitsize,
278                 host        => $archivehost,
279
280                 # list of hosts to restore
281                 HostList    => \@HostList,
282                 BackupList  => \@BackupList,
283
284                 # other info
285                 user        => $User,
286                 reqTime     => time,
287             );
288             my($archive) = Data::Dumper->new(
289                             [  \%ArchiveReq],
290                             [qw(*ArchiveReq)]);
291             $archive->Indent(1);
292             my $openPath = "$TopDir/pc/$archivehost/$reqFileName";
293             if ( open(REQ, ">", $openPath) ) {
294                 binmode(REQ);
295                 print(REQ $archive->Dump);
296                 close(REQ);
297             } else {
298                 ErrorExit(eval("qq{$Lang->{Can_t_open_create__openPath}}"));
299             }
300             $reply = $bpc->ServerMesg("archive $User $archivehost $reqFileName");
301             $str = eval("qq{$Lang->{Archive_requested}}");
302
303             my $content = eval("qq{$Lang->{BackupPC_Archive_Reply_from_server}}");
304             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
305             Trailer();
306         }
307     }
308 }
309
310 1;