From f468efd888cef185bb053ad68aa54f4af2673288 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Thu, 23 Feb 2023 23:42:00 -0800 Subject: [PATCH] Add support for restoring from a compressed file --- postgresql/exec_file.cgi | 24 +++++++++++++++++++++++- postgresql/lang/en | 2 ++ postgresql/postgresql-lib.pl | 15 +++++++++++++++ postgresql/restore.cgi | 22 ++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/postgresql/exec_file.cgi b/postgresql/exec_file.cgi index 90d3f4e58..6b38e3e8f 100755 --- a/postgresql/exec_file.cgi +++ b/postgresql/exec_file.cgi @@ -16,6 +16,7 @@ if ($in{'mode'}) { close(TEMP); &ui_print_header(undef, $text{'exec_title'}, ""); print "$text{'exec_uploadout'}

\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', "$in{'file'}"),"

\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', "$prog")); + $tempfile = &transname(); + $out = &backquote_command( + "$cmd <".quotemeta($file)." 2>&1 >".quotemeta($tempfile)); + if ($?) { + &error(&text('exec_ecompress2', "

$out
")); + } + unlink($file) if ($need_unlink); + $file = $tempfile; + $need_unlink = 1; } # Call the psql program on the file @@ -34,7 +56,7 @@ print "$text{'exec_noout'}\n" if (!$got); print "\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'}, diff --git a/postgresql/lang/en b/postgresql/lang/en index 1579eb87a..1addb2936 100644 --- a/postgresql/lang/en +++ b/postgresql/lang/en @@ -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 diff --git a/postgresql/postgresql-lib.pl b/postgresql/postgresql-lib.pl index b43bf4f56..cac7735b0 100755 --- a/postgresql/postgresql-lib.pl +++ b/postgresql/postgresql-lib.pl @@ -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; diff --git a/postgresql/restore.cgi b/postgresql/restore.cgi index 63ed02d07..cda5f9840 100755 --- a/postgresql/restore.cgi +++ b/postgresql/restore.cgi @@ -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', "$prog")); + $tempfile = &transname(); + $out = &backquote_command( + "$cmd <".quotemeta($path)." 2>&1 >".quotemeta($tempfile)); + if ($?) { + &error(&text('exec_ecompress2', "
$out
")); + } + 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', "
$err
")); }