From 6873c65c4292a500cd496b65623b63ac09be898c Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sat, 10 Oct 2026 15:37:12 -0700 Subject: [PATCH] If MySQL backups are being done as a non-root user, also run pre and post backup commands as that user --- mysql/backup_db.cgi | 12 ++++++++---- mysql/mysql-lib.pl | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/mysql/backup_db.cgi b/mysql/backup_db.cgi index 1be2fa8f9..309068b5d 100755 --- a/mysql/backup_db.cgi +++ b/mysql/backup_db.cgi @@ -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'},"

\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'},"

\n"; next; @@ -216,7 +218,8 @@ if (!$in{'save'}) { print &text('backup_done', "$db", "$file", int($st[7])),"

\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: } diff --git a/mysql/mysql-lib.pl b/mysql/mysql-lib.pl index abf4146a2..ef4bc22b1 100755 --- a/mysql/mysql-lib.pl +++ b/mysql/mysql-lib.pl @@ -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 ? "

".&html_escape($out)."
" : $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 ? "
".&html_escape($out)."
" : $out;