1 #============================================================= -*-perl-*-
3 # BackupPC::CGI::Restore package
7 # This module implements the Restore action for the CGI interface.
10 # Craig Barratt <cbarratt@users.sourceforge.net>
13 # Copyright (C) 2003 Craig Barratt
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.
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.
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
29 #========================================================================
31 # Version 2.1.0beta2, released 23 May 2004.
33 # See http://backuppc.sourceforge.net.
35 #========================================================================
37 package BackupPC::CGI::Restore;
40 use BackupPC::CGI::Lib qw(:all);
46 my($str, $reply, $content);
47 my $Privileged = CheckPermission($In{host});
49 ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
53 my $share = $In{share};
54 my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
55 my @Backups = $bpc->BackupInfoRead($host);
58 if ( !defined($Hosts->{$host}) ) {
59 ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
61 for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
62 next if ( !defined($In{"fcb$i"}) );
63 (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
64 $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
65 if ( @fileList == 0 ) {
66 $pathHdr = substr($name, 0, rindex($name, "/"));
68 while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
69 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
72 push(@fileList, $name);
74 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
76 $fileListStr .= <<EOF;
77 <li> ${EscHTML($name)}
80 $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
81 $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
82 $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
83 $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
84 if ( @fileList == 0 ) {
85 ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
88 ErrorExit($Lang->{Nice_try__but_you_can_t_put});
90 $pathHdr = "/" if ( $pathHdr eq "" );
91 if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
93 # All the files in the list were selected, so just restore the
94 # entire parent directory
96 @fileList = ( $pathHdr );
98 if ( $In{type} == 0 ) {
100 # Build list of hosts
104 foreach my $h ( GetUserHosts(1) ) {
105 my $sel = " selected" if ( $h eq $In{host} );
106 $hostDestSel .= "<option value=\"$h\"$sel>${EscHTML($h)}</option>";
111 # Tell the user what options they have
113 $content .= eval("qq{$Lang->{Restore_Options_for__host2}}");
116 # If there is a single host, make sure direct restore is enabled
120 # Pick up the host's config file
122 $bpc->ConfigRead($hosts[0]);
123 %Conf = $bpc->Conf();
126 # Decide if option 1 (direct restore) is available based
127 # on whether the restore command is set.
129 my $cmd = $Conf{XferMethod} eq "smb" ? $Conf{SmbClientRestoreCmd}
130 : $Conf{XferMethod} eq "tar" ? $Conf{TarClientRestoreCmd}
131 : $Conf{XferMethod} eq "archive" ? undef
132 : $Conf{RsyncRestoreArgs};
133 if ( defined($cmd) ) {
135 "qq{$Lang->{Restore_Options_for__host_Option1}}");
137 my $hostDest = $hosts[0];
139 "qq{$Lang->{Restore_Options_for__host_Option1_disabled}}");
142 $content .= eval("qq{$Lang->{Restore_Options_for__host_Option1}}");
146 # Verify that Archive::Zip is available before showing the
149 if ( eval { require Archive::Zip } ) {
150 $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive}}");
152 $content .= eval("qq{$Lang->{Option_2__Download_Zip_archive2}}");
154 $content .= eval("qq{$Lang->{Option_3__Download_Zip_archive}}");
155 Header(eval("qq{$Lang->{Restore_Options_for__host}}"), $content);
157 } elsif ( $In{type} == 1 ) {
159 # Provide the selected files via a tar archive.
161 my @fileListTrim = @fileList;
162 if ( @fileListTrim > 10 ) {
163 @fileListTrim = (@fileListTrim[0..9], '...');
165 $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
166 . " backup $num; files were: "
167 . join(", ", @fileListTrim));
170 if ( $In{relative} ) {
171 @pathOpts = ("-r", $pathHdr, "-p", "");
174 Content-Type: application/x-gtar
175 Content-Transfer-Encoding: binary
176 Content-Disposition: attachment; filename=\"restore.tar\"
180 # Fork the child off and manually copy the output to our stdout.
181 # This is necessary to ensure the output gets to the correct place
184 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
193 } elsif ( $In{type} == 2 ) {
195 # Provide the selected files via a zip archive.
197 my @fileListTrim = @fileList;
198 if ( @fileListTrim > 10 ) {
199 @fileListTrim = (@fileListTrim[0..9], '...');
201 $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
202 . " backup $num; files were: "
203 . join(", ", @fileListTrim));
206 if ( $In{relative} ) {
207 @pathOpts = ("-r", $pathHdr, "-p", "");
210 Content-Type: application/zip
211 Content-Transfer-Encoding: binary
212 Content-Disposition: attachment; filename=\"restore.zip\"
215 $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
217 # Fork the child off and manually copy the output to our stdout.
218 # This is necessary to ensure the output gets to the correct place
221 $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
224 "-c", $In{compressLevel},
231 } elsif ( $In{type} == 3 ) {
233 # Do restore directly onto host
235 if ( !defined($Hosts->{$In{hostDest}}) ) {
236 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
238 if ( !CheckPermission($In{hostDest}) ) {
239 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
242 # Pick up the destination host's config file
244 my $hostDest = $1 if ( $In{hostDest} =~ /(.*)/ );
245 $bpc->ConfigRead($hostDest);
246 %Conf = $bpc->Conf();
249 # Decide if option 1 (direct restore) is available based
250 # on whether the restore command is set.
252 my $cmd = $Conf{XferMethod} eq "smb" ? $Conf{SmbClientRestoreCmd}
253 : $Conf{XferMethod} eq "tar" ? $Conf{TarClientRestoreCmd}
254 : $Conf{XferMethod} eq "archive" ? undef
255 : $Conf{RsyncRestoreArgs};
256 if ( !defined($cmd) ) {
257 ErrorExit(eval("qq{$Lang->{Restore_Options_for__host_Option1_disabled}}"));
261 foreach my $f ( @fileList ) {
263 (my $strippedShare = $share) =~ s/^\///;
264 (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
265 substr($targetFile, 0, length($pathHdr)) = "/$In{pathHdr}/";
266 $targetFile =~ s{//+}{/}g;
267 $fileListStr .= <<EOF;
268 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
271 my $content = eval("qq{$Lang->{Are_you_sure}}");
272 Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"), $content);
274 } elsif ( $In{type} == 4 ) {
275 if ( !defined($Hosts->{$In{hostDest}}) ) {
276 ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
278 if ( !CheckPermission($In{hostDest}) ) {
279 ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
281 my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
282 my $ipAddr = ConfirmIPAddress($hostDest);
284 # Prepare and send the restore request. We write the request
285 # information using Data::Dumper to a unique file,
286 # $TopDir/pc/$hostDest/restoreReq.$$.n. We use a file
287 # in case the list of files to restore is very long.
290 for ( my $i = 0 ; ; $i++ ) {
291 $reqFileName = "restoreReq.$$.$i";
292 last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
294 my $inPathHdr = $In{pathHdr};
295 $inPathHdr = "/$inPathHdr" if ( $inPathHdr !~ m{^/} );
296 $inPathHdr = "$inPathHdr/" if ( $inPathHdr !~ m{/$} );
298 # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
302 pathHdrSrc => $pathHdr,
304 # destination of restore is hostDest:shareDest/pathHdrDest
305 hostDest => $hostDest,
306 shareDest => $In{shareDest},
307 pathHdrDest => $inPathHdr,
309 # list of files to restore
310 fileList => \@fileList,
316 my($dump) = Data::Dumper->new(
320 mkpath("$TopDir/pc/$hostDest", 0, 0777)
321 if ( !-d "$TopDir/pc/$hostDest" );
322 if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
324 print(REQ $dump->Dump);
327 ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
329 $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
330 . " ${EscURI($hostDest)} $User $reqFileName");
331 $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
332 my $content = eval("qq{$Lang->{Reply_from_server_was___reply}}");
333 Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"), $content);