cbf5f6308c3241e2eb832aa7a05c28f4a36777a0
[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             for ( my $i = 0 ; $i < @Backups ; $i++ ) {
65                 if ( $Backups[$i]{type} eq "full" ) {
66                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
67                     $incrSizeTot = 0;
68                 } else {
69                     $incrSizeTot = $Backups[$i]{size} / (1024 * 1024);
70                 }
71                 $backupnumber = $Backups[$i]{num};
72             }
73             $fullSizeTot += $fullSize + $incrSizeTot;
74             $fullSize = sprintf("%.2f", ($fullSize + $incrSizeTot) / 1000);
75
76                 $bpc->ConfigRead($archHost);
77                 %Conf = $bpc->Conf();
78
79                 my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
80                 my $backup_nums = $dbh->selectcol_arrayref(qq{
81                         select
82                                 backups.num as num
83                         from backups
84                         join hosts on hosts.id = hostid
85                         where hosts.name = ? and inc_size < 0 and size > 0 and not inc_deleted
86                 }, {}, $host);
87
88                 my $checkboxes;
89                 foreach my $backupnumber ( sort @$backup_nums ) {
90                         $checkboxes .= qq|
91 <input type="hidden" name="fcb$checkBoxCnt" value="$host">
92 <input type="checkbox" name="backup$checkBoxCnt" value="$backupnumber">$backupnumber
93                         |;
94                         $checkBoxCnt++;
95                 }
96
97 =for ASA extension removed this
98             $str = <<EOF;
99 <tr>
100 <td class="border"><input type="hidden" name="backup$checkBoxCnt" value="$backupnumber"><input type="checkbox" name="fcb$checkBoxCnt" value="$host">&nbsp;${HostLink($host)} </td>
101 <td align="center" class="border"> ${UserLink($Hosts->{$host}{user})} </td>
102 <td align="center" class="border"> $fullSize </td>
103 EOF
104             $checkBoxCnt++;
105 =cut
106
107             $str = <<EOF;
108 <tr>
109 <td class="border">
110 ${HostLink($host)}
111 $checkboxes
112 </td>
113 <td align="center" class="border">
114 ${UserLink($Hosts->{$host}{user})}
115 </td>
116 <td align="center" class="border"> $fullSize </td>
117 EOF
118
119             if ( @Backups == 0 ) {
120                 $hostCntNone++;
121                 $strNone .= $str;
122             } else {
123                 $hostCntGood++;
124                 $strGood .= $str;
125             }
126         }
127         $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
128         my $now      = timeStamp2(time);
129         my $checkAllHosts = $Lang->{checkAllHosts};
130         $strGood .= <<EOF;
131 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
132 EOF
133         my $content = eval("qq{$Lang->{BackupPC_Archive}}");
134         Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
135         Trailer();
136     } else {
137         my(@HostList, @BackupList, $HostListStr, $hiddenStr, $pathHdr,
138            $badFileCnt, $reply, $str);
139
140         #
141         # Pick up the archive host's config file
142         #
143         $bpc->ConfigRead($archHost);
144         %Conf = $bpc->Conf();
145
146         my $args = {
147             SplitPath    => $Conf{SplitPath},
148             ParPath      => $Conf{ParPath},
149             CatPath      => $Conf{CatPath},
150             GzipPath     => $Conf{GzipPath},
151             Bzip2Path    => $Conf{Bzip2Path},
152             ArchiveDest  => $Conf{ArchiveDest},
153             ArchiveComp  => $Conf{ArchiveComp},
154             ArchivePar   => $Conf{ArchivePar},
155             ArchiveSplit => $Conf{ArchiveSplit},
156             topDir       => $bpc->{TopDir},
157         };
158
159         ServerConnect();
160
161         for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
162             next if ( !defined($In{"fcb$i"}) );
163             my $name = $In{"fcb$i"};
164             my $backupno = $In{"backup$i"};
165             push(@HostList, $name);
166             push(@BackupList, $backupno);
167             $hiddenStr .= <<EOF;
168 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
169 <input type="hidden" name="backup$i" value="$In{'backup' . $i}">
170 EOF
171             $HostListStr .= <<EOF;
172 <li> ${EscHTML($name)} $i
173 EOF
174         }
175         $hiddenStr .= <<EOF;
176 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
177 EOF
178         $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
179         if ( @HostList == 0 ) {
180             ErrorExit($Lang->{You_haven_t_selected_any_hosts});
181         }
182         my ($ArchiveDest, $ArchiveCompNone, $ArchiveCompGzip,
183             $ArchiveCompBzip2, $ArchivePar, $ArchiveSplit);
184         $ArchiveDest = $Conf{ArchiveDest};
185         if ( $Conf{ArchiveComp} eq "none" ) {
186             $ArchiveCompNone   = "checked";
187         } else {
188             $ArchiveCompNone   = "";
189         }
190         if ( $Conf{ArchiveComp} eq "gzip" ) {
191             $ArchiveCompGzip   = "checked";
192         } else {
193             $ArchiveCompGzip   = "";
194         }
195         if ( $Conf{ArchiveComp} eq "bzip2" ) {
196             $ArchiveCompBzip2  = "checked";
197         } else {
198             $ArchiveCompBzip2  = "";
199         }
200         $ArchivePar   = $Conf{ArchivePar};
201         $ArchiveSplit = $Conf{ArchiveSplit};
202
203         if ( $In{type} == 1 ) {
204             #
205             # Tell the user what options they have
206             #
207             my $paramStr = "";
208             if ( $Conf{ArchiveClientCmd} =~ /\$archiveloc\b/ ) {
209                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_location}}");
210             }
211             if ( $Conf{ArchiveClientCmd} =~ /\$compression\b/ ) {
212                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_compression}}");
213             }
214             if ( $Conf{ArchiveClientCmd} =~ /\$parfile\b/
215                     && -x $Conf{ParPath} ) {
216                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_parity}}");
217             }
218             if ( $Conf{ArchiveClientCmd} =~ /\$splitsize\b/
219                     && -x $Conf{SplitPath} ) {
220                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_split}}");
221             }
222             my $content = eval("qq{$Lang->{BackupPC_Archive2}}");
223             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
224             Trailer();
225         } elsif ( $In{type} == 2 ) {
226             my $reqFileName;
227             my $archivehost = $1 if ( $In{archivehost} =~ /(.+)/ );
228             for ( my $i = 0 ; ; $i++ ) {
229                 $reqFileName = "archiveReq.$$.$i";
230                 last if ( !-f "$TopDir/pc/$archivehost/$reqFileName" );
231             }
232             my($compname, $compext);
233             if ( $In{compression} == 2 ) {          # bzip2 compression
234                 $compname = $Conf{Bzip2Path};
235                 $compext = '.bz2';
236             } elsif ( $In{compression} == 1 ) {     # gzip compression
237                 $compname = $Conf{GzipPath};
238                 $compext = '.gz';
239             } else { # No Compression
240                 $compname = $Conf{CatPath};
241                 $compext = '.raw';
242             }
243             my $fullsplitsize = $In{splitsize} . '000000';
244             my %ArchiveReq = (
245                 # parameters for the archive
246                 archiveloc  => $In{archive_device},
247                 archtype    => $In{archive_type},
248                 compression => $compname,
249                 compext     => $compext,
250                 parfile     => $In{par},
251                 splitsize   => $fullsplitsize,
252                 host        => $archivehost,
253
254                 # list of hosts to restore
255                 HostList    => \@HostList,
256                 BackupList  => \@BackupList,
257
258                 # other info
259                 user        => $User,
260                 reqTime     => time,
261             );
262             my($archive) = Data::Dumper->new(
263                             [  \%ArchiveReq],
264                             [qw(*ArchiveReq)]);
265             $archive->Indent(1);
266             my $openPath = "$TopDir/pc/$archivehost/$reqFileName";
267             if ( open(REQ, ">", $openPath) ) {
268                 binmode(REQ);
269                 print(REQ $archive->Dump);
270                 close(REQ);
271             } else {
272                 ErrorExit(eval("qq{$Lang->{Can_t_open_create__openPath}}"));
273             }
274             $reply = $bpc->ServerMesg("archive $User $archivehost $reqFileName");
275             $str = eval("qq{$Lang->{Archive_requested}}");
276
277             my $content = eval("qq{$Lang->{BackupPC_Archive_Reply_from_server}}");
278             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
279             Trailer();
280         }
281     }
282 }
283
284 1;