Add support for restoring from a compressed file

This commit is contained in:
Jamie Cameron
2023-02-23 23:42:00 -08:00
parent 744edd8911
commit f468efd888
4 changed files with 62 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ if ($in{'mode'}) {
close(TEMP);
&ui_print_header(undef, $text{'exec_title'}, "");
print "$text{'exec_uploadout'}<p>\n";
$need_unlink = 1;
}
else {
# From local file
@@ -23,6 +24,27 @@ else {
$file = $in{'file'};
&ui_print_header(undef, $text{'exec_title'}, "");
print &text('exec_fileout', "<tt>$in{'file'}</tt>"),"<p>\n";
$need_unlink = 0;
}
# Un-compress file if needed
$cf = &compression_format($file);
$cmd = $cf == 1 ? "gunzip -c" :
$cf == 2 ? "uncompress -C" :
$cf == 3 ? "bunzip2 -c" : undef;
if ($cmd) {
($prog, @args) = split(/\s+/, $cmd);
&has_command($prog) ||
&error(&text('exec_ecompress', "<tt>$prog</tt>"));
$tempfile = &transname();
$out = &backquote_command(
"$cmd <".quotemeta($file)." 2>&1 >".quotemeta($tempfile));
if ($?) {
&error(&text('exec_ecompress2', "<pre>$out</pre>"));
}
unlink($file) if ($need_unlink);
$file = $tempfile;
$need_unlink = 1;
}
# Call the psql program on the file
@@ -34,7 +56,7 @@ print "<i>$text{'exec_noout'}</i>\n" if (!$got);
print "</pre>\n";
&webmin_log("execfile", undef, $in{'db'}, { 'mode' => $in{'mode'},
'file' => $in{'file'} });
unlink($file) if ($in{'mode'});
unlink($file) if ($need_unlink);
&ui_print_footer("exec_form.cgi?db=$in{'db'}&mode=file", $text{'exec_return'},
"edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},

View File

@@ -157,6 +157,8 @@ exec_tabexec=Execute SQL
exec_tabfile=Run SQL from file
exec_tabimport=Import text file
exec_return=execute SQL form
exec_ecompress=The SQL file is compressed, but the program $1 needed to un-compress it is not installed.
exec_ecompress2=Un-compression failed : $1
stop_err=Failed to stop database server
stop_epidfile=Failed to open PID file $1

View File

@@ -1304,5 +1304,20 @@ else {
}
}
# compression_format(file)
# Returns 0 if uncompressed, 1 for gzip, 2 for compress, 3 for bzip2 or
# 4 for zip
sub compression_format
{
open(BACKUP, "<".$_[0]);
local $two;
read(BACKUP, $two, 2);
close(BACKUP);
return $two eq "\037\213" ? 1 :
$two eq "\037\235" ? 2 :
$two eq "PK" ? 4 :
$two eq "BZ" ? 3 : 0;
}
1;

View File

@@ -21,6 +21,7 @@ else {
&open_tempfile(DATA, ">$path");
&print_tempfile(DATA, $in{'data'});
&close_tempfile(DATA);
$need_unlink = 1;
}
# Validate tables list
@@ -36,7 +37,28 @@ else {
&indexof($in{'db'}, &list_databases()) >= 0 ||
&error(&text('restore_edb'));
# Un-compress file if needed
$cf = &compression_format($path);
$cmd = $cf == 1 ? "gunzip -c" :
$cf == 2 ? "uncompress -C" :
$cf == 3 ? "bunzip2 -c" : undef;
if ($cmd) {
($prog, @args) = split(/\s+/, $cmd);
&has_command($prog) ||
&error(&text('exec_ecompress', "<tt>$prog</tt>"));
$tempfile = &transname();
$out = &backquote_command(
"$cmd <".quotemeta($path)." 2>&1 >".quotemeta($tempfile));
if ($?) {
&error(&text('exec_ecompress2', "<pre>$out</pre>"));
}
unlink($path) if ($need_unlink);
$path = $tempfile;
$need_unlink = 1;
}
$err = &restore_database($in{'db'}, $path, $in{'only'}, $in{'clean'}, $tables);
unlink($file) if ($need_unlink);
if ($err) {
&error(&text('restore_failed', "<pre>$err</pre>"));
}