* Revision update for 2.1.0beta2.
[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  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 2.1.0beta2, released 23 May 2004.
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
43 sub action
44 {
45     my $archHost = $In{host};
46     my $Privileged = CheckPermission();
47
48     if ( !$Privileged ) {
49         ErrorExit($Lang->{Only_privileged_users_can_archive} );
50     }
51     if ( $In{type} == 0 ) {
52         my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
53            $strNone, $strGood, $hostCntGood, $hostCntNone, $checkBoxCnt,
54            $backupnumber);
55
56         $hostCntGood = $hostCntNone = $checkBoxCnt = $fullSizeTot = 0;
57         GetStatusInfo("hosts");
58
59         foreach my $host ( sort(keys(%Status)) ) {
60             my($fullDur, $incrCnt, $fullSize, $fullRate);
61             my @Backups = $bpc->BackupInfoRead($host);
62             my $fullCnt = $incrCnt = 0;
63             for ( my $i = 0 ; $i < @Backups ; $i++ ) {
64                 if ( $Backups[$i]{type} eq "full" ) {
65                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
66                     $incrSizeTot = 0;
67                 } else {
68                     $incrSizeTot = $Backups[$i]{size} / (1024 * 1024);
69                 }
70                 $backupnumber = $Backups[$i]{num};
71             }
72             $fullSizeTot += $fullSize + $incrSizeTot;
73             $fullSize = sprintf("%.2f", ($fullSize + $incrSizeTot) / 1000);
74             $str = <<EOF;
75 <tr>
76 <td class="border"><input type="hidden" name="backup$checkBoxCnt" value="$backupnumber"><input type="checkbox" name="fcb$checkBoxCnt" value="$host">&nbsp;${HostLink($host)} </td>
77 <td align="center" class="border"> ${UserLink($Hosts->{$host}{user})} </td>
78 <td align="center" class="border"> $fullSize </td>
79 EOF
80             $checkBoxCnt++;
81             if ( @Backups == 0 ) {
82                 $hostCntNone++;
83                 $strNone .= $str;
84             } else {
85                 $hostCntGood++;
86                 $strGood .= $str;
87             }
88         }
89         $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
90         my $now      = timeStamp2(time);
91         my $checkAllHosts = $Lang->{checkAllHosts};
92         $strGood .= <<EOF;
93 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
94 EOF
95         my $content = eval("qq{$Lang->{BackupPC_Archive}}");
96         Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
97         Trailer();
98     } else {
99         my(@HostList, @BackupList, $HostListStr, $hiddenStr, $pathHdr,
100            $badFileCnt, $reply, $str);
101         my $args = {
102             SplitPath    => $bpc->{Conf}{SplitPath},
103             ParPath      => $bpc->{Conf}{ParPath},
104             CatPath      => $bpc->{Conf}{CatPath},
105             GzipPath     => $bpc->{Conf}{GzipPath},
106             Bzip2Path    => $bpc->{Conf}{Bzip2Path},
107             ArchiveDest  => $bpc->{Conf}{ArchiveDest},
108             ArchiveComp  => $bpc->{Conf}{ArchiveComp},
109             ArchivePar   => $bpc->{Conf}{ArchivePar},
110             ArchiveSplit => $bpc->{Conf}{ArchiveSplit},
111             topDir       => $bpc->{TopDir},
112         };
113
114         ServerConnect();
115
116         for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
117             next if ( !defined($In{"fcb$i"}) );
118             my $name = $In{"fcb$i"};
119             my $backupno = $In{"backup$i"};
120             push(@HostList, $name);
121             push(@BackupList, $backupno);
122             $hiddenStr .= <<EOF;
123 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
124 <input type="hidden" name="backup$i" value="$In{'backup' . $i}">
125 EOF
126             $HostListStr .= <<EOF;
127 <li> ${EscHTML($name)}
128 EOF
129         }
130         $hiddenStr .= <<EOF;
131 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
132 EOF
133         $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
134         if ( @HostList == 0 ) {
135             ErrorExit($Lang->{You_haven_t_selected_any_hosts});
136         }
137         my ($ArchiveDest, $ArchiveCompNone, $ArchiveCompGzip,
138             $ArchiveCompBzip2, $ArchivePar, $ArchiveSplit);
139         $ArchiveDest = $bpc->{Conf}{ArchiveDest};
140         if ( $bpc->{Conf}{ArchiveComp} eq "none" ) {
141             $ArchiveCompNone   = "checked";
142         } else {
143             $ArchiveCompNone   = "";
144         }
145         if ( $bpc->{Conf}{ArchiveComp} eq "gzip" ) {
146             $ArchiveCompGzip   = "checked";
147         } else {
148             $ArchiveCompGzip   = "";
149         }
150         if ( $bpc->{Conf}{ArchiveComp} eq "bzip2" ) {
151             $ArchiveCompBzip2  = "checked";
152         } else {
153             $ArchiveCompBzip2  = "";
154         }
155         $ArchivePar   = $bpc->{Conf}{ArchivePar};
156         $ArchiveSplit = $bpc->{Conf}{ArchiveSplit};
157
158         if ( $In{type} == 1 ) {
159             #
160             # Tell the user what options they have
161             #
162             my $paramStr = "";
163             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$archiveloc\b/ ) {
164                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_location}}");
165             }
166             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$compression\b/ ) {
167                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_compression}}");
168             }
169             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$parfile\b/
170                     && -x $bpc->{Conf}{ParPath} ) {
171                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_parity}}");
172             }
173             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$splitsize\b/
174                     && -x $bpc->{Conf}{SplitPath} ) {
175                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_split}}");
176             }
177             my $content = eval("qq{$Lang->{BackupPC_Archive2}}");
178             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
179             Trailer();
180         } elsif ( $In{type} == 2 ) {
181             my $reqFileName;
182             my $archivehost = $1 if ( $In{archivehost} =~ /(.+)/ );
183             for ( my $i = 0 ; ; $i++ ) {
184                 $reqFileName = "archiveReq.$$.$i";
185                 last if ( !-f "$TopDir/pc/$archivehost/$reqFileName" );
186             }
187             my($compname, $compext);
188             if ( $In{compression} == 2 ) {          # bzip2 compression
189                 $compname = $Conf{Bzip2Path};
190                 $compext = '.bz2';
191             } elsif ( $In{compression} == 1 ) {     # gzip compression
192                 $compname = $Conf{GzipPath};
193                 $compext = '.gz';
194             } else { # No Compression
195                 $compname = $Conf{CatPath};
196                 $compext = '.raw';
197             }
198             my $fullsplitsize = $In{splitsize} . '000000';
199             my %ArchiveReq = (
200                 # parameters for the archive
201                 archiveloc  => $In{archive_device},
202                 archtype    => $In{archive_type},
203                 compression => $compname,
204                 compext     => $compext,
205                 parfile     => $In{par},
206                 splitsize   => $fullsplitsize,
207                 host        => $archivehost,
208
209                 # list of hosts to restore
210                 HostList    => \@HostList,
211                 BackupList  => \@BackupList,
212
213                 # other info
214                 user        => $User,
215                 reqTime     => time,
216             );
217             my($archive) = Data::Dumper->new(
218                             [  \%ArchiveReq],
219                             [qw(*ArchiveReq)]);
220             $archive->Indent(1);
221             if ( open(REQ, ">$TopDir/pc/$archivehost/$reqFileName") ) {
222                 binmode(REQ);
223                 print(REQ $archive->Dump);
224                 close(REQ);
225             } else {
226                 ErrorExit($Lang->{Can_t_open_create} );
227             }
228             $reply = $bpc->ServerMesg("archive $User $archivehost $reqFileName");
229             $str = eval("qq{$Lang->{Archive_requested}}");
230
231             my $content = eval("qq{$Lang->{BackupPC_Archive_Reply_from_server}}");
232             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
233             Trailer();
234         }
235     }
236 }
237
238 1;