Creating a backup of bacula config files before applying new configs

11 views
Skip to first unread message

Rui Loureiro

unread,
Sep 12, 2024, 11:30:34 AM9/12/24
to bacularis
Hi again Marcin,

I like to modify your code to create a backup of the bacula config files (e.g /etc/bacula/bacula-dir.conf ->  /etc/bacula/bacula-dir-timestamp.conf ) every time we apply a new configuration.

Looking at your code, I thing the best place to do that is the write($source, $config) function at the ConfigBacula.php module.

What do you think about this approach?

Thanks.

Marcin Haba

unread,
Sep 12, 2024, 11:43:33 AM9/12/24
to Rui Loureiro, bacularis
Hi Rui,

I think it is a good approach. This Bacula config class is the only
class used to manage Bacula configuration files directly, so it is a
good place to add something common for all Bacula config files.

Best regards,
Marcin Haba (gani)

Rui Loureiro

unread,
Sep 12, 2024, 3:30:57 PM9/12/24
to bacularis
Hi Marcin,

I'm posting some code here. It can be improved with a limit of files to keep and a option to turn the backup on/off.
I'm changing the group and permissions for convenience because I using VS Code and the SFTP extension to manage the bacula files.
I have a question, the Bacularis runs the reload command after saving the bacula-dir.conf or we need to do it manualy in the bconsole?


/**
* Config extension
*/
public const CONFIG_EXT = '.conf';

/**
* Write config data to file in Bacula format.
*
* @access public
* @param string $source config file path
* @param array $config config data
* @return bool true if config written successfully, otherwise false
*/
public function write($source, $config)
{

$ts = date("YmdHis");
$dir_name = dirname($source);
$base_name = basename($source,self::CONFIG_EXT);
$bk_file_name = $dir_name . DIRECTORY_SEPARATOR . $base_name . '_' . $ts . self::CONFIG_EXT;
$orig_umask = umask(0);
$bk_ok = copy($source,$bk_file_name);

umask(0077);
$content = $this->prepareConfig($config);
$result = file_put_contents($source, $content);
// if nothing write delete backup file
if (is_int($result)) {
if ($bk_ok) {
chgrp($bk_file_name,filegroup($source));
chmod($bk_file_name,fileperms($source));
}
} else {
if ($bk_ok) {
delete($bk_file_name);
}
}
umask($orig_umask);
return is_int($result);
}

Best Regards,
Rui

Marcin Haba

unread,
Sep 12, 2024, 10:00:29 PM9/12/24
to Rui Loureiro, bacularis
On Thu, 12 Sept 2024 at 21:30, Rui Loureiro <ruilou...@gmail.com> wrote:
>
> Hi Marcin,
>
> I'm posting some code here. It can be improved with a limit of files to keep and a option to turn the backup on/off.
> I'm changing the group and permissions for convenience because I using VS Code and the SFTP extension to manage the bacula files.
> I have a question, the Bacularis runs the reload command after saving the bacula-dir.conf or we need to do it manualy in the bconsole?

Hi Rui,

Thanks for sharing with the Community your changes for doing config
backup. That's nice of you.

For the question about reload command, Bacularis runs the reload
automatically after successful director config save. It is done if the
save action is triggered on the Bacularis web interface. If the save
is done directly through the API (ex. called from custom script), then
there is required to call one more endpoint to reload the director
configuration. This action is done automatically in case saving on the
web interface.
Reply all
Reply to author
Forward
0 new messages