Add support for compressed backups

This commit is contained in:
Jamie Cameron
2023-02-23 19:52:32 -08:00
parent ff4aacb5bc
commit dee0ad64f9
5 changed files with 41 additions and 12 deletions

View File

@ -81,6 +81,7 @@ else {
$config{'backup_'.$in{'db'}} = $in{'path'};
$config{'backup_mkdir_'.$in{'db'}} = $in{'mkdir'};
$config{'backup_format_'.$in{'db'}} = $in{'format'};
$config{'backup_compress_'.$in{'db'}} = $in{'compress'};
$config{'backup_tables_'.$in{'db'}} = join(" ", @tables);
if ($in{'save'}) {
&save_module_config();
@ -94,6 +95,8 @@ if (!$in{'save'}) {
@dbs = $in{'all'} ? @alldbs : ( $in{'db'} );
$suf = $in{'format'} eq "p" ? "sql" :
$in{'format'} eq "t" ? "tar" : "post";
$suf .= ($in{'compress'} == 1 ? ".gz" :
$in{'compress'} == 2 ? ".bz2" : "");
if ($cmode == 1) {
# Run and check before-backup command (for all DBs)
$bok = &execute_before(undef, STDOUT, 1, $in{'file'}, undef);
@ -123,7 +126,8 @@ if (!$in{'save'}) {
next;
}
}
$err = &backup_database($db, $path, $in{'format'}, \@tables);
$err = &backup_database($db, $path, $in{'format'}, \@tables,
undef, $in{'compress'});
if ($err) {
print "$main::whatfailed : ",
&text('backup_ebackup',"<pre>$err</pre>"),"<p>\n";

View File

@ -32,9 +32,12 @@ foreach $db (@dbs) {
next;
}
$format = $config{'backup_format_'.$sf};
$compress = $config{'backup_compress_'.$sf};
$mkdir = $config{'backup_mkdir_'.$sf};
$suf = $format eq "p" ? "sql" :
$format eq "t" ? "tar" : "post";
$suf .= ($compress == 1 ? ".gz" :
$compress == 2 ? ".bz2" : "");
if ($all) {
$dir = &date_subs($config{'backup_'});
$file = "$dir/$db.$suf";
@ -60,7 +63,7 @@ foreach $db (@dbs) {
}
unlink($file);
$err = &backup_database($db, $file, $format, \@tables);
$err = &backup_database($db, $file, $format, \@tables, undef,$compress);
if ($err) {
print STDERR "Backup of database $db to file $file failed:\n";
print STDERR $err;

View File

@ -63,6 +63,13 @@ print &ui_table_row($text{'backup_format'},
[ 't', $text{'backup_format_t'} ],
[ 'c', $text{'backup_format_c'} ] ]));
# Show compression option
$c = $c{'backup_compress_'.$in{'db'}} || 0;
print &ui_table_row($text{'backup_compress'},
&ui_radio("compress", $c, [ [ 0, $text{'backup_compress_0'} ],
[ 1, $text{'backup_compress_1'} ],
[ 2, $text{'backup_compress_2'} ] ]));
if (!$in{'all'}) {
# Show input to select tables
$t = $c{'backup_tables_'.$in{'db'}};

View File

@ -504,6 +504,10 @@ backup_format=Backup file format
backup_format_p=Plain SQL text
backup_format_t=Tar archive
backup_format_c=Custom archive
backup_compress=Compression type
backup_compress_0=None
backup_compress_1=GZIP
backup_compress_2=BZIP2
backup_ecannot=You are not allowed to create backups
backup_done=Successfully backed up $3 bytes from database $1 to file $2.
backup_notaccept=Database $1 is not accepting connections.

View File

@ -1125,22 +1125,33 @@ return $config{'host'} eq '' || $config{'host'} eq 'localhost' ||
&to_ipaddress($config{'host'}) eq &to_ipaddress(&get_system_hostname());
}
# backup_database(database, dest-path, format, [&only-tables], [run-as-user])
# backup_database(database, dest-path, format, [&only-tables], [run-as-user],
# [compress-mode])
# Executes the pg_dump command to backup the specified database to the
# given destination path. Returns undef on success, or an error message
# on failure.
sub backup_database
{
local ($db, $path, $format, $tables, $user) = @_;
local $tablesarg = join(" ", map { " -t ".quotemeta('"'.$_.'"') } @$tables);
local $cmd = &quote_path($config{'dump_cmd'}).
my ($db, $path, $format, $tables, $user, $compress) = @_;
my $tablesarg = join(" ", map { " -t ".quotemeta('"'.$_.'"') } @$tables);
my $writer;
if ($compress == 0) {
$writer = "cat >".quotemeta($path);
}
elsif ($compress == 1) {
$writer = "gzip -c >".quotemeta($path);
}
elsif ($compress == 2) {
$writer = "bzip2 -c >".quotemeta($path);
}
my $cmd = &quote_path($config{'dump_cmd'}).
(!$postgres_login ? "" :
&supports_pgpass() ? " -U $postgres_login" : " -u").
($config{'host'} ? " -h $config{'host'}" : "").
($config{'port'} ? " -p $config{'port'}" : "").
($format eq 'p' ? "" : " -b").
$tablesarg.
" -F$format -f ".&quote_path($path)." $db";
" -F$format $db | $writer";
if ($postgres_sameunix && defined(getpwnam($postgres_login))) {
# Postgres connections have to be made as the 'postgres' Unix user
$cmd = &command_as_user($postgres_login, 0, $cmd);
@ -1150,7 +1161,7 @@ elsif ($user) {
$cmd = &command_as_user($user, 0, $cmd);
}
$cmd = &command_with_login($cmd);
local $out = &backquote_logged("$cmd 2>&1");
my $out = &backquote_logged("$cmd 2>&1");
if ($? || $out =~ /could not|error|failed/i) {
return $out;
}
@ -1162,9 +1173,9 @@ return undef;
# Returns undef on success, or an error message on failure.
sub restore_database
{
local ($db, $path, $only, $clean, $tables) = @_;
local $tablesarg = join(" ", map { " -t ".quotemeta('"'.$_.'"') } @$tables);
local $cmd = &quote_path($config{'rstr_cmd'}).
my ($db, $path, $only, $clean, $tables) = @_;
my $tablesarg = join(" ", map { " -t ".quotemeta('"'.$_.'"') } @$tables);
my $cmd = &quote_path($config{'rstr_cmd'}).
(!$postgres_login ? "" :
&supports_pgpass() ? " -U $postgres_login" : " -u").
($config{'host'} ? " -h $config{'host'}" : "").
@ -1177,7 +1188,7 @@ if ($postgres_sameunix && defined(getpwnam($postgres_login))) {
$cmd = &command_as_user($postgres_login, 0, $cmd);
}
$cmd = &command_with_login($cmd);
local $out = &backquote_logged("$cmd 2>&1");
my $out = &backquote_logged("$cmd 2>&1");
if ($? || $out =~ /could not|error|failed/i) {
return $out;
}