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