* Major changes from Ryan Kucera to add style sheets to the CGI
[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 bgcolor="#ffffcc">
74 <td><input type="hidden" name="backup$checkBoxCnt" value="$backupnumber"><input type="checkbox" name="fcb$checkBoxCnt" value="$host">&nbsp;${HostLink($host)} </td>
75 <td align="center"> ${UserLink($Hosts->{$host}{user})} </td>
76 <td align="center"> $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         Header($Lang->{BackupPC__Archive});
94         print eval ("qq{$Lang->{BackupPC_Archive}}");
95
96         Trailer();
97     } else {
98
99         my(@HostList, @BackupList, $HostListStr, $hiddenStr, $pathHdr, $badFileCnt, $reply, $str);
100
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, $ArchiveCompBzip2, $ArchivePar, $ArchiveSplit);
142         $ArchiveDest       = $bpc->{Conf}{ArchiveDest};
143         if ( $bpc->{Conf}{ArchiveComp} eq "none" ) {
144             $ArchiveCompNone   = "checked";
145         } else {
146             $ArchiveCompNone   = "";
147         }
148         if ( $bpc->{Conf}{ArchiveComp} eq "gzip" ) {
149             $ArchiveCompGzip   = "checked";
150         } else {
151             $ArchiveCompGzip   = "";
152         }
153         if ( $bpc->{Conf}{ArchiveComp} eq "bzip2" ) {
154             $ArchiveCompBzip2  = "checked";
155         } else {
156             $ArchiveCompBzip2  = "";
157         }
158         $ArchivePar        = $bpc->{Conf}{ArchivePar};
159         $ArchiveSplit      = $bpc->{Conf}{ArchiveSplit};
160
161         if ( $In{type} == 1 ) {
162             #
163             # Tell the user what options they have
164             #
165
166             Header($Lang->{BackupPC__Archive});
167             print eval ("qq{$Lang->{BackupPC_Archive2}}");
168             Trailer();
169         } elsif ( $In{type} == 2 ) {
170             my $reqFileName;
171             my $archivehost = $1 if ( $In{archivehost} =~ /(.+)/ );
172             for ( my $i = 0 ; ; $i++ ) {
173                 $reqFileName = "archiveReq.$$.$i";
174                 last if ( !-f "$TopDir/pc/$archivehost/$reqFileName" );
175             }
176             my $compname;
177             if ( $In{compression} == 2 ) { # bzip2 compression
178                 $compname = $Conf{Bzip2Path};
179             } elsif ( $In{compression} == 1 ) { # gzip compression
180                 $compname = $Conf{GzipPath};
181             } else { # No Compression
182                 $compname = $Conf{CatPath};
183             }
184             my $compext;
185             if ( $In{compression} == 2 ) { # bzip2 compression
186                 $compext = '.bz2';
187             } elsif ( $In{compression} == 1 ) { # gzip compression
188                 $compext = '.gz';
189             } else { # No Compression
190                 $compext = '.raw';
191             }
192             my $fullsplitsize = $In{splitsize} . '000000';
193             my %ArchiveReq = (
194                 # parameters for the archive
195                 archiveloc  => $In{archive_device},
196                 archtype    => $In{archive_type},
197                 compression => $compname,
198                 compext     => $compext,
199                 parfile     => $In{par},
200                 splitsize   => $fullsplitsize,
201                 host        => $archivehost,
202
203
204                 # list of hosts to restore
205                 HostList    => \@HostList,
206                 BackupList  => \@BackupList,
207
208                 # other info
209                 user        => $User,
210                 reqTime     => time,
211             );
212             my($archive) = Data::Dumper->new(
213                             [  \%ArchiveReq],
214                             [qw(*ArchiveReq)]);
215             $archive->Indent(1);
216             if ( open(REQ, ">$TopDir/pc/$archivehost/$reqFileName") ) {
217                 binmode(REQ);
218                 print(REQ $archive->Dump);
219                 close(REQ);
220             } else {
221                 ErrorExit($Lang->{Can_t_open_create} );
222             }
223         $reply = $bpc->ServerMesg("archive $User $archivehost $reqFileName");
224
225         $str = eval("qq{$Lang->{Archive_requested}}");
226             Header($Lang->{BackupPC__Archive});
227             print eval ("qq{$Lang->{BackupPC_Archive_Reply_from_server}}");
228             Trailer();
229         }
230
231     }
232
233 }
234
235 1;