864a2eafdc1783a9e21ff0e96dabeae4a0f2e9e6
[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.0beta0, released 20 Mar 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
47     if ( $In{type} == 0 ) {
48         my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
49            $strNone, $strGood, $hostCntGood, $hostCntNone, $checkBoxCnt,
50            $backupnumber);
51
52         $hostCntGood = $hostCntNone = $checkBoxCnt = $fullSizeTot = 0;
53         GetStatusInfo("hosts");
54         my $Privileged = CheckPermission();
55
56         if ( !$Privileged ) {
57             ErrorExit($Lang->{Only_privileged_users_can_archive} );
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 $Privileged = CheckPermission();
102         my $args = {
103             SplitPath    => $bpc->{Conf}{SplitPath},
104             ParPath      => $bpc->{Conf}{ParPath},
105             CatPath      => $bpc->{Conf}{CatPath},
106             GzipPath     => $bpc->{Conf}{GzipPath},
107             Bzip2Path    => $bpc->{Conf}{Bzip2Path},
108             ArchiveDest  => $bpc->{Conf}{ArchiveDest},
109             ArchiveComp  => $bpc->{Conf}{ArchiveComp},
110             ArchivePar   => $bpc->{Conf}{ArchivePar},
111             ArchiveSplit => $bpc->{Conf}{ArchiveSplit},
112             topDir       => $bpc->{TopDir},
113         };
114
115         if ( !$Privileged ) {
116             ErrorExit($Lang->{Only_privileged_users_can_archive} );
117         }
118         ServerConnect();
119
120         for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
121             next if ( !defined($In{"fcb$i"}) );
122             my $name = $In{"fcb$i"};
123             my $backupno = $In{"backup$i"};
124             push(@HostList, $name);
125             push(@BackupList, $backupno);
126             $hiddenStr .= <<EOF;
127 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
128 <input type="hidden" name="backup$i" value="$In{'backup' . $i}">
129 EOF
130             $HostListStr .= <<EOF;
131 <li> ${EscHTML($name)}
132 EOF
133         }
134         $hiddenStr .= <<EOF;
135 <input type="hidden" name="archivehost" value="$In{'archivehost'}">
136 EOF
137         $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
138         if ( @HostList == 0 ) {
139             ErrorExit($Lang->{You_haven_t_selected_any_hosts});
140         }
141         my ($ArchiveDest, $ArchiveCompNone, $ArchiveCompGzip,
142             $ArchiveCompBzip2, $ArchivePar, $ArchiveSplit);
143         $ArchiveDest = $bpc->{Conf}{ArchiveDest};
144         if ( $bpc->{Conf}{ArchiveComp} eq "none" ) {
145             $ArchiveCompNone   = "checked";
146         } else {
147             $ArchiveCompNone   = "";
148         }
149         if ( $bpc->{Conf}{ArchiveComp} eq "gzip" ) {
150             $ArchiveCompGzip   = "checked";
151         } else {
152             $ArchiveCompGzip   = "";
153         }
154         if ( $bpc->{Conf}{ArchiveComp} eq "bzip2" ) {
155             $ArchiveCompBzip2  = "checked";
156         } else {
157             $ArchiveCompBzip2  = "";
158         }
159         $ArchivePar   = $bpc->{Conf}{ArchivePar};
160         $ArchiveSplit = $bpc->{Conf}{ArchiveSplit};
161
162         if ( $In{type} == 1 ) {
163             #
164             # Tell the user what options they have
165             #
166             my $paramStr = "";
167             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$archiveloc\b/ ) {
168                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_location}}");
169             }
170             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$compression\b/ ) {
171                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_compression}}");
172             }
173             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$parfile\b/
174                     && -x $bpc->{Conf}{ParPath} ) {
175                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_parity}}");
176             }
177             if ( $bpc->{Conf}{ArchiveClientCmd} =~ /\$splitsize\b/
178                     && -x $bpc->{Conf}{SplitPath} ) {
179                 $paramStr .= eval("qq{$Lang->{BackupPC_Archive2_split}}");
180             }
181             my $content = eval("qq{$Lang->{BackupPC_Archive2}}");
182             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
183             Trailer();
184         } elsif ( $In{type} == 2 ) {
185             my $reqFileName;
186             my $archivehost = $1 if ( $In{archivehost} =~ /(.+)/ );
187             for ( my $i = 0 ; ; $i++ ) {
188                 $reqFileName = "archiveReq.$$.$i";
189                 last if ( !-f "$TopDir/pc/$archivehost/$reqFileName" );
190             }
191             my($compname, $compext);
192             if ( $In{compression} == 2 ) {          # bzip2 compression
193                 $compname = $Conf{Bzip2Path};
194                 $compext = '.bz2';
195             } elsif ( $In{compression} == 1 ) {     # gzip compression
196                 $compname = $Conf{GzipPath};
197                 $compext = '.gz';
198             } else { # No Compression
199                 $compname = $Conf{CatPath};
200                 $compext = '.raw';
201             }
202             my $fullsplitsize = $In{splitsize} . '000000';
203             my %ArchiveReq = (
204                 # parameters for the archive
205                 archiveloc  => $In{archive_device},
206                 archtype    => $In{archive_type},
207                 compression => $compname,
208                 compext     => $compext,
209                 parfile     => $In{par},
210                 splitsize   => $fullsplitsize,
211                 host        => $archivehost,
212
213                 # list of hosts to restore
214                 HostList    => \@HostList,
215                 BackupList  => \@BackupList,
216
217                 # other info
218                 user        => $User,
219                 reqTime     => time,
220             );
221             my($archive) = Data::Dumper->new(
222                             [  \%ArchiveReq],
223                             [qw(*ArchiveReq)]);
224             $archive->Indent(1);
225             if ( open(REQ, ">$TopDir/pc/$archivehost/$reqFileName") ) {
226                 binmode(REQ);
227                 print(REQ $archive->Dump);
228                 close(REQ);
229             } else {
230                 ErrorExit($Lang->{Can_t_open_create} );
231             }
232             $reply = $bpc->ServerMesg("archive $User $archivehost $reqFileName");
233             $str = eval("qq{$Lang->{Archive_requested}}");
234
235             my $content = eval("qq{$Lang->{BackupPC_Archive_Reply_from_server}}");
236             Header(eval("qq{$Lang->{BackupPC__Archive}}"), $content, 1);
237             Trailer();
238         }
239     }
240 }
241
242 1;