If MySQL backups are being done as a non-root user, also run pre and post backup commands as that user

This commit is contained in:
Jamie Cameron
2026-10-10 15:37:12 -07:00
parent f2b94348dc
commit 6873c65c42
2 changed files with 16 additions and 8 deletions

View File

@@ -151,7 +151,8 @@ if (!$in{'save'}) {
@dbs = $in{'all'} ? @alldbs : ( $in{'db'} );
if ($cmode == 1) {
# Run and check before-backup command (for all DBs)
$bok = &execute_before(undef, STDOUT, 1, $in{'file'}, undef);
$bok = &execute_before(undef, STDOUT, 1, $in{'file'}, undef,
$access{'buser'});
if (!$bok) {
print "$main::whatfailed : ",
$text{'backup_ebefore'},"<p>\n";
@@ -195,7 +196,8 @@ if (!$in{'save'}) {
if ($cron && $cmode == 0) {
# Run and check before-backup command (for one DB)
$bok = &execute_before($db, STDOUT, 1, $file,
$in{'all'} ? undef : $db);
$in{'all'} ? undef : $db,
$access{'buser'});
if (!$bok) {
print $text{'backup_ebefore'},"<p>\n";
next;
@@ -216,7 +218,8 @@ if (!$in{'save'}) {
print &text('backup_done', "<tt>$db</tt>",
"<tt>$file</tt>", int($st[7])),"<p>\n";
}
&execute_after($db, STDOUT, 1, $file, $in{'all'} ? undef : $db)
&execute_after($db, STDOUT, 1, $file, $in{'all'} ? undef : $db,
$access{'buser'})
if ($cron && $cmode == 0);
if ($in{'dest'}) {
@@ -229,7 +232,8 @@ if (!$in{'save'}) {
&unlink_file($file);
}
}
&execute_after(undef, STDOUT, 1, $in{'file'}, undef) if ($cmode == 1);
&execute_after(undef, STDOUT, 1, $in{'file'}, undef,
$access{'buser'}) if ($cmode == 1);
donebackup:
}

View File

@@ -1014,16 +1014,18 @@ if ($config{'webmin_subs'}) {
return $rv;
}
# execute_before(db, handle, escape, path, db-for-config)
# execute_before(db, handle, escape, path, db-for-config, [&as-user])
# Executes the before-backup command for some DB, and sends output to the
# given file handle. Returns 1 if the command succeeds, or 0 on failure
sub execute_before
{
my ($db, $h, $escape, $path, $db) = @_;
my ($db, $h, $escape, $path, $db, $asuser) = @_;
my $cmd = $config{'backup_before_'.$db};
if ($cmd) {
$ENV{'BACKUP_FILE'} = $path;
my $out;
$cmd = &command_as_user($asuser, 0, $cmd)
if ($asuser && $asuser ne "root");
my $rv = &execute_command($cmd, undef, \$out, \$out);
if ($h && $out) {
print $h $escape ? "<pre>".&html_escape($out)."</pre>" : $out;
@@ -1033,16 +1035,18 @@ if ($cmd) {
return 1;
}
# execute_after(db, handle, escape, path, db-for-config)
# execute_after(db, handle, escape, path, db-for-config, [&as-user])
# Executes the after-backup command for some DB, and sends output to the
# given file handle. Returns 1 if the command succeeds, or 0 on failure
sub execute_after
{
my ($db, $h, $escape, $path, $db) = @_;
my ($db, $h, $escape, $path, $db, $asuser) = @_;
my $cmd = $config{'backup_after_'.$_[4]};
if ($cmd) {
$ENV{'BACKUP_FILE'} = $path;
my $out;
$cmd = &command_as_user($asuser, 0, $cmd)
if ($asuser && $asuser ne "root");
my $rv = &execute_command($cmd, undef, \$out, \$out);
if ($h && $out) {
print $h $escape ? "<pre>".&html_escape($out)."</pre>" : $out;