6be934ca1c88f8919537da8bb72dca97e1167727
[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             push(@HostList, $name);
191             push(@BackupList, $backupno);
192             $hiddenStr .= <<EOF;
193 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
194 <input type="hidden" name="backup$i" value="$In{'backup' . $i}">
195 EOF
196             $HostListStr .= <<EOF;
197 <li> ${EscHTML($name)} $i
198 EOF
199         }
200         $hiddenStr .= <<EOF;
201 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
202 EOF
203         $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
204         if ( @HostList == 0 ) {
205             ErrorExit($Lang->{You_haven_t_selected_any_hosts});
206         }
207         my ($ArchiveDest, $ArchiveCompNone, $ArchiveCompGzip,
208             $ArchiveCompBzip2, $ArchivePar, $ArchiveSplit);
209         $ArchiveDest = $Conf{ArchiveDest};
210         if ( $Conf{ArchiveComp} eq "none" ) {
211             $ArchiveCompNone   = "checked";
212         } else {
213             $ArchiveCompNone   = "";
214         }
215         if ( $Conf{ArchiveComp} eq "gzip" ) {
216             $ArchiveCompGzip   = "checked";
217         } else {
218             $ArchiveCompGzip   = "";
219         }
220         if ( $Conf{ArchiveComp} eq "bzip2" ) {
221             $ArchiveCompBzip2  = "checked";
222         } else {
223             $ArchiveCompBzip2  = "";
224         }
225         $ArchivePar   = $Conf{ArchivePar};
226         $ArchiveSplit = $Conf{ArchiveSplit};
227
228         if ( $In{type} == 1 ) {
229             #
230             # Tell the user what options they have
231             #
232             my $paramStr = "";
233             if ( $Conf{ArchiveClientCmd} =~ /\$archiveloc\b/ ) {
234                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_location}}");
235             }
236             if ( $Conf{ArchiveClientCmd} =~ /\$compression\b/ ) {
237                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_compression}}");
238             }
239             if ( $Conf{ArchiveClientCmd} =~ /\$parfile\b/
240                     && -x $Conf{ParPath} ) {
241                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_parity}}");
242             }
243             if ( $Conf{ArchiveClientCmd} =~ /\$splitsize\b/
244                     && -x $Conf{SplitPath} ) {
245                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_split}}");
246             }
247             my $content = eval("qq{$Lang->{BackupPC_Archive2}}");
248             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
249             Trailer();
250         } elsif ( $In{type} == 2 ) {
251             my $reqFileName;
252             my $archivehost = $1 if ( $In{archivehost} =~ /(.+)/ );
253             for ( my $i = 0 ; ; $i++ ) {
254                 $reqFileName = "archiveReq.$$.$i";
255                 last if ( !-f "$TopDir/pc/$archivehost/$reqFileName" );
256             }
257             my($compname, $compext);
258             if ( $In{compression} == 2 ) {          # bzip2 compression
259                 $compname = $Conf{Bzip2Path};
260                 $compext = '.bz2';
261             } elsif ( $In{compression} == 1 ) {     # gzip compression
262                 $compname = $Conf{GzipPath};
263                 $compext = '.gz';
264             } else { # No Compression
265                 $compname = $Conf{CatPath};
266                 $compext = '.raw';
267             }
268             my $fullsplitsize = $In{splitsize} . '000000';
269             my %ArchiveReq = (
270                 # parameters for the archive
271                 archiveloc  => $In{archive_device},
272                 archtype    => $In{archive_type},
273                 compression => $compname,
274                 compext     => $compext,
275                 parfile     => $In{par},
276                 splitsize   => $fullsplitsize,
277                 host        => $archivehost,
278
279                 # list of hosts to restore
280                 HostList    => \@HostList,
281                 BackupList  => \@BackupList,
282
283                 # other info
284                 user        => $User,
285                 reqTime     => time,
286             );
287             my($archive) = Data::Dumper->new(
288                             [  \%ArchiveReq],
289                             [qw(*ArchiveReq)]);
290             $archive->Indent(1);
291             my $openPath = "$TopDir/pc/$archivehost/$reqFileName";
292             if ( open(REQ, ">", $openPath) ) {
293                 binmode(REQ);
294                 print(REQ $archive->Dump);
295                 close(REQ);
296             } else {
297                 ErrorExit(eval("qq{$Lang->{Can_t_open_create__openPath}}"));
298             }
299             $reply = $bpc->ServerMesg("archive $User $archivehost $reqFileName");
300             $str = eval("qq{$Lang->{Archive_requested}}");
301
302             my $content = eval("qq{$Lang->{BackupPC_Archive_Reply_from_server}}");
303             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
304             Trailer();
305         }
306     }
307 }
308
309 1;