* A failed full dump is now saved as a partial (incomplete) dump,
[BackupPC.git] / lib / BackupPC / CGI / Restore.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::Restore package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the Restore 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::Restore;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41 use Data::Dumper;
42
43 sub action
44 {
45     my($str, $reply);
46     my $Privileged = CheckPermission($In{host});
47     if ( !$Privileged ) {
48         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_restore_backup_files}}"));
49     }
50     my $host  = $In{host};
51     my $num   = $In{num};
52     my $share = $In{share};
53     my(@fileList, $fileListStr, $hiddenStr, $pathHdr, $badFileCnt);
54     my @Backups = $bpc->BackupInfoRead($host);
55
56     ServerConnect();
57     if ( !defined($Hosts->{$host}) ) {
58         ErrorExit(eval("qq{$Lang->{Bad_host_name}}"));
59     }
60     for ( my $i = 0 ; $i < $In{fcbMax} ; $i++ ) {
61         next if ( !defined($In{"fcb$i"}) );
62         (my $name = $In{"fcb$i"}) =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
63         $badFileCnt++ if ( $name =~ m{(^|/)\.\.(/|$)} );
64         if ( @fileList == 0 ) {
65             $pathHdr = $name;
66         } else {
67             while ( substr($name, 0, length($pathHdr)) ne $pathHdr ) {
68                 $pathHdr = substr($pathHdr, 0, rindex($pathHdr, "/"));
69             }
70         }
71         push(@fileList, $name);
72         $hiddenStr .= <<EOF;
73 <input type="hidden" name="fcb$i" value="$In{'fcb' . $i}">
74 EOF
75         $fileListStr .= <<EOF;
76 <li> ${EscHTML($name)}
77 EOF
78     }
79     $hiddenStr .= "<input type=\"hidden\" name=\"fcbMax\" value=\"$In{fcbMax}\">\n";
80     $hiddenStr .= "<input type=\"hidden\" name=\"share\" value=\"${EscHTML($share)}\">\n";
81     $badFileCnt++ if ( $In{pathHdr} =~ m{(^|/)\.\.(/|$)} );
82     $badFileCnt++ if ( $In{num} =~ m{(^|/)\.\.(/|$)} );
83     if ( @fileList == 0 ) {
84         ErrorExit($Lang->{You_haven_t_selected_any_files__please_go_Back_to});
85     }
86     if ( $badFileCnt ) {
87         ErrorExit($Lang->{Nice_try__but_you_can_t_put});
88     }
89     if ( @fileList == 1 ) {
90         $pathHdr =~ s/(.*)\/.*/$1/;
91     }
92     $pathHdr = "/" if ( $pathHdr eq "" );
93     if ( $In{type} != 0 && @fileList == $In{fcbMax} ) {
94         #
95         # All the files in the list were selected, so just restore the
96         # entire parent directory
97         #
98         @fileList = ( $pathHdr );
99     }
100     if ( $In{type} == 0 ) {
101         #
102         # Tell the user what options they have
103         #
104         Header(eval("qq{$Lang->{Restore_Options_for__host}}"));
105         print(eval("qq{$Lang->{Restore_Options_for__host2}}"));
106
107         #
108         # Verify that Archive::Zip is available before showing the
109         # zip restore option
110         #
111         if ( eval { require Archive::Zip } ) {
112             print (eval("qq{$Lang->{Option_2__Download_Zip_archive}}"));
113         } else {
114             print (eval("qq{$Lang->{Option_2__Download_Zip_archive2}}"));
115         }
116         print (eval("qq{$Lang->{Option_3__Download_Zip_archive}}"));
117         Trailer();
118     } elsif ( $In{type} == 1 ) {
119         #
120         # Provide the selected files via a tar archive.
121         #
122         my @fileListTrim = @fileList;
123         if ( @fileListTrim > 10 ) {
124             @fileListTrim = (@fileListTrim[0..9], '...');
125         }
126         $bpc->ServerMesg("log User $User downloaded tar archive for $host,"
127                        . " backup $num; files were: "
128                        . join(", ", @fileListTrim));
129
130         my @pathOpts;
131         if ( $In{relative} ) {
132             @pathOpts = ("-r", $pathHdr, "-p", "");
133         }
134         print(STDOUT <<EOF);
135 Content-Type: application/x-gtar
136 Content-Transfer-Encoding: binary
137 Content-Disposition: attachment; filename=\"restore.tar\"
138
139 EOF
140         #
141         # Fork the child off and manually copy the output to our stdout.
142         # This is necessary to ensure the output gets to the correct place
143         # under mod_perl.
144         #
145         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_tarCreate",
146                  "-h", $host,
147                  "-n", $num,
148                  "-s", $share,
149                  @pathOpts,
150                  @fileList
151             ],
152             sub { print(@_); }
153         );
154     } elsif ( $In{type} == 2 ) {
155         #
156         # Provide the selected files via a zip archive.
157         #
158         my @fileListTrim = @fileList;
159         if ( @fileListTrim > 10 ) {
160             @fileListTrim = (@fileListTrim[0..9], '...');
161         }
162         $bpc->ServerMesg("log User $User downloaded zip archive for $host,"
163                        . " backup $num; files were: "
164                        . join(", ", @fileListTrim));
165
166         my @pathOpts;
167         if ( $In{relative} ) {
168             @pathOpts = ("-r", $pathHdr, "-p", "");
169         }
170         print(STDOUT <<EOF);
171 Content-Type: application/zip
172 Content-Transfer-Encoding: binary
173 Content-Disposition: attachment; filename=\"restore.zip\"
174
175 EOF
176         $In{compressLevel} = 5 if ( $In{compressLevel} !~ /^\d+$/ );
177         #
178         # Fork the child off and manually copy the output to our stdout.
179         # This is necessary to ensure the output gets to the correct place
180         # under mod_perl.
181         #
182         $bpc->cmdSystemOrEval(["$BinDir/BackupPC_zipCreate",
183                  "-h", $host,
184                  "-n", $num,
185                  "-c", $In{compressLevel},
186                  "-s", $share,
187                  @pathOpts,
188                  @fileList
189             ],
190             sub { print(@_); }
191         );
192     } elsif ( $In{type} == 3 ) {
193         #
194         # Do restore directly onto host
195         #
196         if ( !defined($Hosts->{$In{hostDest}}) ) {
197             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
198         }
199         if ( !CheckPermission($In{hostDest}) ) {
200             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
201         }
202         $fileListStr = "";
203         foreach my $f ( @fileList ) {
204             my $targetFile = $f;
205             (my $strippedShare = $share) =~ s/^\///;
206             (my $strippedShareDest = $In{shareDest}) =~ s/^\///;
207             substr($targetFile, 0, length($pathHdr)) = $In{pathHdr};
208             $fileListStr .= <<EOF;
209 <tr><td>$host:/$strippedShare$f</td><td>$In{hostDest}:/$strippedShareDest$targetFile</td></tr>
210 EOF
211         }
212         Header(eval("qq{$Lang->{Restore_Confirm_on__host}}"));
213         print(eval("qq{$Lang->{Are_you_sure}}"));
214         Trailer();
215     } elsif ( $In{type} == 4 ) {
216         if ( !defined($Hosts->{$In{hostDest}}) ) {
217             ErrorExit(eval("qq{$Lang->{Host__doesn_t_exist}}"));
218         }
219         if ( !CheckPermission($In{hostDest}) ) {
220             ErrorExit(eval("qq{$Lang->{You_don_t_have_permission_to_restore_onto_host}}"));
221         }
222         my $hostDest = $1 if ( $In{hostDest} =~ /(.+)/ );
223         my $ipAddr = ConfirmIPAddress($hostDest);
224         #
225         # Prepare and send the restore request.  We write the request
226         # information using Data::Dumper to a unique file,
227         # $TopDir/pc/$hostDest/restoreReq.$$.n.  We use a file
228         # in case the list of files to restore is very long.
229         #
230         my $reqFileName;
231         for ( my $i = 0 ; ; $i++ ) {
232             $reqFileName = "restoreReq.$$.$i";
233             last if ( !-f "$TopDir/pc/$hostDest/$reqFileName" );
234         }
235         my %restoreReq = (
236             # source of restore is hostSrc, #num, path shareSrc/pathHdrSrc
237             num         => $In{num},
238             hostSrc     => $host,
239             shareSrc    => $share,
240             pathHdrSrc  => $pathHdr,
241
242             # destination of restore is hostDest:shareDest/pathHdrDest
243             hostDest    => $hostDest,
244             shareDest   => $In{shareDest},
245             pathHdrDest => $In{pathHdr},
246
247             # list of files to restore
248             fileList    => \@fileList,
249
250             # other info
251             user        => $User,
252             reqTime     => time,
253         );
254         my($dump) = Data::Dumper->new(
255                          [  \%restoreReq],
256                          [qw(*RestoreReq)]);
257         $dump->Indent(1);
258         if ( open(REQ, ">$TopDir/pc/$hostDest/$reqFileName") ) {
259             binmode(REQ);
260             print(REQ $dump->Dump);
261             close(REQ);
262         } else {
263             ErrorExit(eval("qq{$Lang->{Can_t_open_create}}"));
264         }
265         $reply = $bpc->ServerMesg("restore ${EscURI($ipAddr)}"
266                         . " ${EscURI($hostDest)} $User $reqFileName");
267         $str = eval("qq{$Lang->{Restore_requested_to_host__hostDest__backup___num}}");
268         Header(eval("qq{$Lang->{Restore_Requested_on__hostDest}}"));
269         print (eval("qq{$Lang->{Reply_from_server_was___reply}}"));
270         Trailer();
271     }
272 }
273
274 1;