]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/backup.cgi
Merge branch 'master' of git://git.ipfire.org/ipfire-2.x
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / backup.cgi
CommitLineData
cf29614f 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
26906d98 5# Copyright (C) 2005-2010 IPFire Team #
70df8302
MT
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###############################################################################
cf29614f
CS
21
22use strict;
23# enable only the following on debugging purpose
36dfbfcf
CS
24#use warnings;
25#use CGI::Carp 'fatalsToBrowser';
8e8bbd9d 26use File::Copy;
cf29614f
CS
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31
32my %color = ();
33my %mainsettings = ();
8e8bbd9d 34my %cgiparams=();
cf29614f
CS
35my %checked = ();
36my $message = "";
37my $errormessage = "";
26906d98
CS
38my @backups = "";
39my @backupisos = "";
cf29614f 40
8e8bbd9d 41$a = new CGI;
cf29614f
CS
42
43&General::readhash("${General::swroot}/main/settings", \%mainsettings);
44&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
45
8e8bbd9d
CS
46$cgiparams{'ACTION'} = '';
47$cgiparams{'FILE'} = '';
48$cgiparams{'UPLOAD'} = '';
49$cgiparams{'BACKUPLOGS'} = '';
69addbb8 50
8e8bbd9d 51&Header::getcgihash(\%cgiparams);
cf29614f 52
a609bfb0
CS
53############################################################################################################################
54################################################ Workaround for Directories ################################################
55
2a1f6aa7 56system("/usr/local/bin/backupctrl makedirs >/dev/null 2>&1 ") unless ( -e '/var/ipfire/backup/addons/backup') ;
a609bfb0 57
cf29614f 58############################################################################################################################
69addbb8 59############################################## System calls ohne Http Header ###############################################
cf29614f 60
b90a7e56
DG
61# Replace slashes from filename
62$cgiparams{'FILE'} =~ s/\///;
69addbb8
CS
63
64if ( $cgiparams{'ACTION'} eq "download" )
8e8bbd9d 65{
901a50cf
CS
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 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
70 print @fileholder;
71 exit (0);
8e8bbd9d 72}
b90a7e56
DG
73if ( $cgiparams{'ACTION'} eq "downloadiso" )
74{
75 open(DLFILE, "</var/tmp/backupiso/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
76 my @fileholder = <DLFILE>;
77 print "Content-Type:application/x-download\n";
78 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
79 print @fileholder;
80 exit (0);
81}
5ad5a6bc
MT
82if ( $cgiparams{'ACTION'} eq "downloadaddon" )
83{
84 open(DLFILE, "</var/ipfire/backup/addons/backup/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
85 my @fileholder = <DLFILE>;
86 print "Content-Type:application/x-download\n";
87 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
88 print @fileholder;
89 exit (0);
90}
8e8bbd9d
CS
91elsif ( $cgiparams{'ACTION'} eq "restore" )
92{
901a50cf
CS
93 my $upload = $a->param("UPLOAD");
94 open UPLOADFILE, ">/tmp/restore.ipf";
95 binmode $upload;
96 while ( <$upload> ) {
97 print UPLOADFILE;
98 }
99 close UPLOADFILE;
100 system("/usr/local/bin/backupctrl restore >/dev/null 2>&1");
8e8bbd9d 101}
5ad5a6bc
MT
102elsif ( $cgiparams{'ACTION'} eq "restoreaddon" )
103{
d2b476e8
CS
104 chomp($cgiparams{'UPLOAD'});
105 # we need to fix cause IE7 gives the full path and FF only the filename
106 my @temp = split(/\\/,$cgiparams{'UPLOAD'});
5ad5a6bc 107 my $upload = $a->param("UPLOAD");
d2b476e8 108 open UPLOADFILE, ">/tmp/".$temp[$#temp];
5ad5a6bc
MT
109 binmode $upload;
110 while ( <$upload> ) {
111 print UPLOADFILE;
112 }
113 close UPLOADFILE;
d2b476e8 114 system("/usr/local/bin/backupctrl restoreaddon ".$temp[$#temp]." >/dev/null 2>&1");
5ad5a6bc 115}
cf29614f 116
8e8bbd9d 117&Header::showhttpheaders();
69addbb8
CS
118
119sub 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();}
120
8e8bbd9d
CS
121&Header::openpage($Lang::tr{'backup'}, 1, "");
122&Header::openbigbox('100%', 'left', '', $errormessage);
123
69addbb8
CS
124############################################################################################################################
125################################################### Default System calls ###################################################
126
127if ( $cgiparams{'ACTION'} eq "backup" )
128{
f31d3c1f
DG
129 if ( $cgiparams{'BACKUPLOGS'} eq "include" ) {
130 system("/usr/local/bin/backupctrl include >/dev/null 2>&1");
131 } elsif ( $cgiparams{'BACKUPLOGS'} eq "exclude" ) {
132 system("/usr/local/bin/backupctrl exclude >/dev/null 2>&1");
133 } elsif ( $cgiparams{'BACKUPLOGS'} eq "iso" ) {
134 system("/usr/local/bin/backupctrl iso >/dev/null 2>&1");
135 }
901a50cf
CS
136}
137if ( $cgiparams{'ACTION'} eq "addonbackup" )
138{
5ad5a6bc 139 system("/usr/local/bin/backupctrl addonbackup $cgiparams{'ADDON'} >/dev/null 2>&1");
69addbb8
CS
140}
141elsif ( $cgiparams{'ACTION'} eq "delete" )
142{
901a50cf 143 system("/usr/local/bin/backupctrl $cgiparams{'FILE'} >/dev/null 2>&1");
69addbb8
CS
144}
145
146############################################################################################################################
901a50cf 147############################################ Backups des Systems erstellen #################################################
69addbb8 148
8e8bbd9d
CS
149if ( $message ne "" ){
150 &Header::openbox('100%','left',$Lang::tr{'error messages'});
151 print "<font color='red'>$message</font>\n";
152 &Header::closebox();
153}
cf29614f 154
26906d98
CS
155if ( -e "/var/ipfire/backup/" ){
156 @backups = `cd /var/ipfire/backup/ && ls *.ipf 2>/dev/null`;
157}
158
159if ( -e "/var/tmp/backupiso/" ){
160 @backupisos = `cd /var/tmp/backupiso/ && ls *.iso 2>/dev/null`;
161}
901a50cf 162
cf29614f
CS
163&Header::openbox('100%', 'center', $Lang::tr{'backup'});
164
165print <<END
166<form method='post' action='$ENV{'SCRIPT_NAME'}'>
167<table width='95%' cellspacing='0'>
f31d3c1f
DG
168<tr>
169 <td align='left' width='40%'>$Lang::tr{'logs'}</td>
170 <td align='left'>
171 <input type='radio' name='BACKUPLOGS' value='include'/> $Lang::tr{'include logfiles'}<br/>
172 <input type='radio' name='BACKUPLOGS' value='exclude' checked='checked'/> $Lang::tr{'exclude logfiles'}<br/>
173 <input type='radio' name='BACKUPLOGS' value='iso' /> $Lang::tr{'generate iso'}
174 </td>
175</tr>
901a50cf
CS
176<tr><td align='center' colspan='2'>
177 <input type='hidden' name='ACTION' value='backup' />
178 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
179</td></tr>
cf29614f
CS
180</table>
181</form>
182END
183;
184&Header::closebox();
185
901a50cf
CS
186############################################################################################################################
187############################################ Backups des Systems downloaden ################################################
188
cf29614f
CS
189&Header::openbox('100%', 'center', $Lang::tr{'backups'});
190
191print <<END
cf29614f
CS
192<table width='95%' cellspacing='0'>
193END
194;
195foreach (@backups){
26906d98 196 chomp($_);
69addbb8 197my $Datei = "/var/ipfire/backup/".$_;
8e8bbd9d 198my @Info = stat($Datei);
5504342a
JPT
199my $Size = $Info[7] / 1024 / 1024;
200$Size = sprintf("%0.2f", $Size);
201print "<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>";
69addbb8 202print "<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>";
cf29614f 203}
b90a7e56
DG
204foreach (@backupisos){
205chomp($_);
206my $Datei = "/var/tmp/backupiso/".$_;
207my @Info = stat($Datei);
5504342a
JPT
208my $Size = $Info[7] / 1024 / 1024;
209$Size = sprintf("%0.2f", $Size);
210print "<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>";
b90a7e56
DG
211print "<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>";
212}
901a50cf 213print <<END
cf29614f 214</table>
8e8bbd9d
CS
215END
216;
217&Header::closebox();
218
901a50cf
CS
219############################################################################################################################
220############################################# Backups von Addons erstellen #################################################
221
222&Header::openbox('100%', 'center', 'addons');
223
224my @addonincluds = `ls /var/ipfire/backup/addons/includes/ 2>/dev/null`;
98ab7f50
CS
225my @addons = `ls /var/ipfire/backup/addons/backup/ 2>/dev/null`;
226my %addons;
227
228foreach (@addons){
229 my $addon=substr($_,0,length($_)-5);
230 $addons{$addon}='';
231}
901a50cf
CS
232
233print "<table width='95%' cellspacing='0'>";
234foreach (@addonincluds){
235chomp($_);
98ab7f50 236delete $addons{$_};
901a50cf
CS
237my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
238my @Info = stat($Datei);
239my $Size = $Info[7] / 1024;
c35ac552 240
901a50cf 241if ( -e $Datei ){
c35ac552
JPT
242 if ($Size < 1) {
243 $Size = sprintf("%.2f", $Size);
244 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
245 } else {
246 $Size = sprintf("%2d", $Size);
247 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
248
249 }
250
901a50cf
CS
251print <<END
252 <td align='right' width='5'>
253 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
5ad5a6bc
MT
254 <input type='hidden' name='ACTION' value='downloadaddon' />
255 <input type='hidden' name='FILE' value='$_.ipf' />
901a50cf
CS
256 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
257 </form>
258 </td>
259 <td align='right' width='5'>
260 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
261 <input type='hidden' name='ACTION' value='delete' />
363a19ee 262 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
901a50cf
CS
263 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
264 </form>
265 </td>
266END
267;
268}
269else{
b8e5a9c5 270 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ </td><td width='5' align='right'></td><td width='5' align='right'></td>";
901a50cf
CS
271}
272print <<END
273 <td align='right' width='5'>
274 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
275 <input type='hidden' name='ACTION' value='addonbackup' />
276 <input type='hidden' name='ADDON' value='$_' />
277 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
278 </form>
279 </td></tr>
280END
281;
282}
b8e5a9c5
CS
283foreach (keys(%addons)){
284chomp($_);
98ab7f50 285my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
b8e5a9c5
CS
286my @Info = stat($Datei);
287my $Size = $Info[7] / 1024;
288$Size = sprintf("%2d", $Size);
98ab7f50 289print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
b8e5a9c5
CS
290print <<END
291 <td align='right' width='5'>
292 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
293 <input type='hidden' name='ACTION' value='downloadaddon' />
98ab7f50 294 <input type='hidden' name='FILE' value='$_.ipf' />
b8e5a9c5
CS
295 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
296 </form>
297 </td>
298 <td align='right' width='5'>
299 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
300 <input type='hidden' name='ACTION' value='delete' />
41708968 301 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
b8e5a9c5
CS
302 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
303 </form>
304 </td>
305 <td align='right' width='5'></td></tr>
306END
307;
308}
309
901a50cf
CS
310print "</table>";
311&Header::closebox();
5ad5a6bc
MT
312
313############################################################################################################################
314####################################### Backups des Systems wiederherstellen ###############################################
315
316&Header::openbox('100%', 'center', $Lang::tr{'restore'});
317
318print <<END
319<table width='95%' cellspacing='0'>
320<tr><td align='center' colspan='2'><font color='red'><br />$Lang::tr{'backupwarning'}</font><br /><br /></td></tr>
d2b476e8
CS
321<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>
322<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>
5ad5a6bc
MT
323</table>
324END
325;
326&Header::closebox();
cf29614f
CS
327&Header::closebigbox();
328&Header::closepage();