]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/backup.cgi
Merge remote-tracking branch 'origin/master' into next
[ipfire-2.x.git] / html / cgi-bin / backup.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2005-2013 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23 # enable only the following on debugging purpose
24 #use warnings;
25 #use CGI::Carp 'fatalsToBrowser';
26 use File::Copy;
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 my %color = ();
33 my %mainsettings = ();
34 my %cgiparams=();
35 my %checked = ();
36 my $message = "";
37 my $errormessage = "";
38 my @backups = "";
39 my @backupisos = "";
40
41 $a = new CGI;
42
43 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
44 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
45
46 $cgiparams{'ACTION'} = '';
47 $cgiparams{'FILE'} = '';
48 $cgiparams{'UPLOAD'} = '';
49 $cgiparams{'BACKUPLOGS'} = '';
50
51 &Header::getcgihash(\%cgiparams);
52
53 ############################################################################################################################
54 ################################################ Workaround for Directories ################################################
55
56 system("/usr/local/bin/backupctrl makedirs >/dev/null 2>&1 ") unless ( -e '/var/ipfire/backup/addons/backup') ;
57
58 ############################################################################################################################
59 ############################################## System calls ohne Http Header ###############################################
60
61 # Replace slashes from filename
62 $cgiparams{'FILE'} =~ s/\///;
63
64 if ( $cgiparams{'ACTION'} eq "download" )
65 {
66 open(DLFILE, "</var/ipfire/backup/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
67 my @fileholder = <DLFILE>;
68 print "Content-Type:application/x-download\n";
69 my @fileinfo = stat("/var/ipfire/backup/$cgiparams{'FILE'}");
70 print "Content-Length:$fileinfo[7]\n";
71 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
72 print @fileholder;
73 exit (0);
74 }
75 if ( $cgiparams{'ACTION'} eq "downloadiso" )
76 {
77 open(DLFILE, "</var/tmp/backupiso/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
78 my @fileholder = <DLFILE>;
79 print "Content-Type:application/x-download\n";
80 my @fileinfo = stat("/var/tmp/backupiso/$cgiparams{'FILE'}");
81 print "Content-Length:$fileinfo[7]\n";
82 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
83 print @fileholder;
84 exit (0);
85 }
86 if ( $cgiparams{'ACTION'} eq "downloadaddon" )
87 {
88 open(DLFILE, "</var/ipfire/backup/addons/backup/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
89 my @fileholder = <DLFILE>;
90 print "Content-Type:application/x-download\n";
91 my @fileinfo = stat("/var/ipfire/backup/addons/backup/$cgiparams{'FILE'}");
92 print "Content-Length:$fileinfo[7]\n";
93 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
94 print @fileholder;
95 exit (0);
96 }
97 elsif ( $cgiparams{'ACTION'} eq "restore" )
98 {
99 my $upload = $a->param("UPLOAD");
100 open UPLOADFILE, ">/tmp/restore.ipf";
101 binmode $upload;
102 while ( <$upload> ) {
103 print UPLOADFILE;
104 }
105 close UPLOADFILE;
106 system("/usr/local/bin/backupctrl restore >/dev/null 2>&1");
107 }
108 elsif ( $cgiparams{'ACTION'} eq "restoreaddon" )
109 {
110 chomp($cgiparams{'UPLOAD'});
111 # we need to fix cause IE7 gives the full path and FF only the filename
112 my @temp = split(/\\/,$cgiparams{'UPLOAD'});
113 my $upload = $a->param("UPLOAD");
114 open UPLOADFILE, ">/tmp/".$temp[$#temp];
115 binmode $upload;
116 while ( <$upload> ) {
117 print UPLOADFILE;
118 }
119 close UPLOADFILE;
120 system("/usr/local/bin/backupctrl restoreaddon ".$temp[$#temp]." >/dev/null 2>&1");
121 }
122
123 &Header::showhttpheaders();
124
125 sub refreshpage{&Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";&Header::closebox();}
126
127 &Header::openpage($Lang::tr{'backup'}, 1, "");
128 &Header::openbigbox('100%', 'left', '', $errormessage);
129
130 ############################################################################################################################
131 ################################################### Default System calls ###################################################
132
133 if ( $cgiparams{'ACTION'} eq "backup" )
134 {
135 if ( $cgiparams{'BACKUPLOGS'} eq "include" ) {
136 system("/usr/local/bin/backupctrl include >/dev/null 2>&1");
137 } elsif ( $cgiparams{'BACKUPLOGS'} eq "exclude" ) {
138 system("/usr/local/bin/backupctrl exclude >/dev/null 2>&1");
139 } elsif ( $cgiparams{'BACKUPLOGS'} eq "iso" ) {
140 system("/usr/local/bin/backupctrl iso >/dev/null 2>&1");
141 }
142 }
143 if ( $cgiparams{'ACTION'} eq "addonbackup" )
144 {
145 system("/usr/local/bin/backupctrl addonbackup $cgiparams{'ADDON'} >/dev/null 2>&1");
146 }
147 elsif ( $cgiparams{'ACTION'} eq "delete" )
148 {
149 system("/usr/local/bin/backupctrl $cgiparams{'FILE'} >/dev/null 2>&1");
150 }
151
152 ############################################################################################################################
153 ############################################ Backups des Systems erstellen #################################################
154
155 if ( $message ne "" ){
156 &Header::openbox('100%','left',$Lang::tr{'error messages'});
157 print "<font color='red'>$message</font>\n";
158 &Header::closebox();
159 }
160
161 if ( -e "/var/ipfire/backup/" ){
162 @backups = `cd /var/ipfire/backup/ && ls *.ipf 2>/dev/null`;
163 }
164
165 if ( -e "/var/tmp/backupiso/" ){
166 @backupisos = `cd /var/tmp/backupiso/ && ls *.iso 2>/dev/null`;
167 }
168
169 &Header::openbox('100%', 'center', );
170
171 print <<END
172 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
173 <table width='95%' cellspacing='0'>
174 <tr>
175 <td align='left' width='40%'>$Lang::tr{'logs'}</td>
176 <td align='left'>
177 <input type='radio' name='BACKUPLOGS' value='include'/> $Lang::tr{'include logfiles'}<br/>
178 <input type='radio' name='BACKUPLOGS' value='exclude' checked='checked'/> $Lang::tr{'exclude logfiles'}<br/>
179 END
180 ;
181 my $MACHINE=`uname -m`;
182 if ( ! ( $MACHINE =~ "arm" )) {
183 print" <input type='radio' name='BACKUPLOGS' value='iso' /> $Lang::tr{'generate iso'}<br/>"
184 }
185 print <<END
186 </td>
187 </tr>
188 <tr><td align='center' colspan='2'>
189 <input type='hidden' name='ACTION' value='backup' />
190 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
191 </td></tr>
192 </table>
193 </form>
194 END
195 ;
196 &Header::closebox();
197
198 ############################################################################################################################
199 ############################################ Backups des Systems downloaden ################################################
200
201 &Header::openbox('100%', 'center', $Lang::tr{'backups'});
202
203 print <<END
204 <table width='95%' cellspacing='0'>
205 END
206 ;
207 foreach (@backups){
208 if ( $_ !~ /ipf$/){next;}
209 chomp($_);
210 my $Datei = "/var/ipfire/backup/".$_;
211 my @Info = stat($Datei);
212 my $Size = $Info[7] / 1024 / 1024;
213 $Size = sprintf("%0.2f", $Size);
214 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size MB</td><td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='download' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /></form></td>";
215 print "<td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='delete' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /></form></td></tr>";
216 }
217 foreach (@backupisos){
218 if ( $_ !~ /iso$/){next;}
219 chomp($_);
220 my $Datei = "/var/tmp/backupiso/".$_;
221 my @Info = stat($Datei);
222 my $Size = $Info[7] / 1024 / 1024;
223 $Size = sprintf("%0.2f", $Size);
224 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size MB</td><td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='downloadiso' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /></form></td>";
225 print "<td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='delete' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /></form></td></tr>";
226 }
227 print <<END
228 </table>
229 END
230 ;
231 &Header::closebox();
232
233 ############################################################################################################################
234 ############################################# Backups von Addons erstellen #################################################
235
236 &Header::openbox('100%', 'center', $Lang::tr{'addons'});
237
238 my @addonincluds = `ls /var/ipfire/backup/addons/includes/ 2>/dev/null`;
239 my @addons = `ls /var/ipfire/backup/addons/backup/ 2>/dev/null`;
240 my %addons;
241
242 foreach (@addons){
243 my $addon=substr($_,0,length($_)-5);
244 $addons{$addon}='';
245 }
246
247 print "<table width='95%' cellspacing='0'>";
248 foreach (@addonincluds){
249 chomp($_);
250 delete $addons{$_};
251 my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
252 my @Info = stat($Datei);
253 my $Size = $Info[7] / 1024;
254
255 if ( -e $Datei ){
256 if ($Size < 1) {
257 $Size = sprintf("%.2f", $Size);
258 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
259 } else {
260 $Size = sprintf("%2d", $Size);
261 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
262
263 }
264
265 print <<END
266 <td align='right' width='5'>
267 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
268 <input type='hidden' name='ACTION' value='downloadaddon' />
269 <input type='hidden' name='FILE' value='$_.ipf' />
270 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
271 </form>
272 </td>
273 <td align='right' width='5'>
274 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
275 <input type='hidden' name='ACTION' value='delete' />
276 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
277 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
278 </form>
279 </td>
280 END
281 ;
282 }
283 else{
284 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ </td><td width='5' align='right'></td><td width='5' align='right'></td>";
285 }
286 print <<END
287 <td align='right' width='5'>
288 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
289 <input type='hidden' name='ACTION' value='addonbackup' />
290 <input type='hidden' name='ADDON' value='$_' />
291 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
292 </form>
293 </td></tr>
294 END
295 ;
296 }
297 foreach (keys(%addons)){
298 chomp($_);
299 my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
300 my @Info = stat($Datei);
301 my $Size = $Info[7] / 1024;
302 $Size = sprintf("%2d", $Size);
303 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
304 print <<END
305 <td align='right' width='5'>
306 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
307 <input type='hidden' name='ACTION' value='downloadaddon' />
308 <input type='hidden' name='FILE' value='$_.ipf' />
309 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
310 </form>
311 </td>
312 <td align='right' width='5'>
313 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
314 <input type='hidden' name='ACTION' value='delete' />
315 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
316 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
317 </form>
318 </td>
319 <td align='right' width='5'></td></tr>
320 END
321 ;
322 }
323
324 print "</table>";
325 &Header::closebox();
326
327 ############################################################################################################################
328 ####################################### Backups des Systems wiederherstellen ###############################################
329
330 &Header::openbox('100%', 'center', $Lang::tr{'restore'});
331
332 print <<END
333 <table width='95%' cellspacing='0'>
334 <tr><td align='center' colspan='2'><font color='red'><br />$Lang::tr{'backupwarning'}</font><br /><br /></td></tr>
335 <tr><td align='left'>$Lang::tr{'backup'}</td><td align='left'><form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'><input type="file" size='50' name="UPLOAD" /><input type='hidden' name='ACTION' value='restore' /><input type='hidden' name='FILE' /><input type='image' alt='$Lang::tr{'restore'}' title='$Lang::tr{'restore'}' src='/images/media-floppy.png' /></form></td></tr>
336 <tr><td align='left'>$Lang::tr{'backupaddon'}</td><td align='left'><form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'><input type="file" size='50' name="UPLOAD" /><input type='hidden' name='ACTION' value='restoreaddon' /><input type='hidden' name='FILE' /><input type='image' alt='$Lang::tr{'restore'}' title='$Lang::tr{'restore'}' src='/images/media-floppy.png' /></form></td></tr>
337 </table>
338 END
339 ;
340 &Header::closebox();
341 &Header::closebigbox();
342 &Header::closepage();