Here are some code examples for using Rubycula scripts.
Note that in order to preserve the order of configuration parameters within the section, we have to use Ruby 1.9. See how to use Ruby 1.8 below.
#!/usr/bin/ruby
load "BaculaBase.rb"
load "BaculaConf.rb"
load "BaculaConfSection.rb"
load "BaculaConfMysql.rb"
baculaMysql = BaculaConfMysql.new({"host" => "localhost", "user" => "bacula-dir", "password" => "", "database" => "bacula-dir"})
baculaMysql = BaculaConfMysql.new({"host" => "localhost", "user" => "bacula-dir", "password" => "", "database" => "bacula-dir"})
baculaMysql.updateTablesSchema()
#BaculaConf::new(configSource = nil, hQuerySections = nil, followForeigns = false, applyTemplates = true, queryTemplatesOnly = false)
dirConfig = BaculaConf.new(baculaMysql, nil, true, true, false)
dirConfig.printSections()
This script can very well be the only thing being in Bacula director configuration. bacula-dir.conf could look like this:
#
# Default Bacula Director Configuration file
#
@"|/etc/bacula/baculaConf_dir.rb"
A more sophisticated example, a full script.
#!/usr/bin/ruby
load "BaculaBase.rb"
load "BaculaConf.rb"
load "BaculaConfSection.rb"
load "BaculaConfMysql.rb"
baculaMysql = BaculaConfMysql.new({"host" => "localhost", "user" => "bacula-dir", "password" => "", "database" => "bacula-dir"})
dirConfig = BaculaConf.new(baculaMysql)
# prepare to add the 1st job
hParamsClient = {"Name" => "Client-1", "Address" => "localhost", "Password" => "changeme", "FD Port" => 9102}
if newClientID = dirConfig.sectionCreate("Client", hParamsClient)
print "Created new Client with ID #{newClientID}!\n"
end
hParamsFileset = {"Name" => "Fileset-1", "Ignore Fileset Changes" => "yes"}
if newFilesetID = dirConfig.sectionCreate("Fileset", hParamsFileset)
print "Created new Fileset with ID #{newFilesetID}!\n"
hParamsFilesetInclude = {"fileset_id" => newFilesetID}
if newFilesetIncludeID = dirConfig.sectionCreate("Include", hParamsFilesetInclude)
print "Created new Fileset-Include with ID #{newFilesetIncludeID}!\n"
end
hParamsFilesetIncludeOptions = {"fileset-include_id" => newFilesetIncludeID, "Compression" => "GZIP", "Signature" => "MD5"}
if newFilesetIncludeOptionsID = dirConfig.sectionCreate("Options", hParamsFilesetIncludeOptions)
print "Created new Fileset-Include-Options with ID #{newFilesetIncludeOptionsID}!\n"
end
hParamsFilesetIncludeUpdate = {"File" => ["/home", "/var"]}
# BaculaConf::sectionUpdate(sSection, sectionID, hSectionParams, updateOnly = false)
# only add new parameters
if dirConfig.sectionUpdate("Include", newFilesetIncludeID, hParamsFilesetIncludeUpdate, true)
print "Updated Fileset-Include with ID #{newFilesetIncludeID}!\n"
end
end
hParamsJob = {"Name" => "Job-1", "Client" => newClientID, "Fileset" => newFilesetID}
if newJobID = dirConfig.sectionCreate("Job", hParamsJob)
print "Created new Job with ID #{newJobID}!\n"
end
# re-read the configuration, use names instead of IDs
# BaculaConf::new(configSource = nil, hQuerySections = nil, followForeigns = false, applyTemplates = true, queryTemplatesOnly = false)
dirConfig = BaculaConf.new(baculaMysql, nil, true)
dirConfig.printSections
#!/usr/bin/ruby
load "BaculaBase.rb"
load "BaculaConf.rb"
load "BaculaConfSection.rb"
dirConfig = BaculaConf.new()
# prepare to add the 1st job
hParamsClient = {"Name" => "Client-1", "Address" => "localhost", "Password" => "changeme", "FD Port" => 9102}
if newClient = dirConfig.sectionCreate("Client", hParamsClient)
print "Created new Client!\n"
end
hParamsFileset = {"Name" => "Fileset-1", "Ignore Fileset Changes" => "yes"}
if newFileset = dirConfig.sectionCreate("Fileset", hParamsFileset)
print "Created new Fileset!\n"
if newFilesetInclude = dirConfig.sectionCreate("Include", {}, false, newFileset)
print "Created new Include under Fileset!\n"
end
hParamsFilesetIncludeOptions = {"Compression" => "GZIP", "Signature" => "MD5"}
if newFilesetIncludeOptions = dirConfig.sectionCreate("Options", hParamsFilesetIncludeOptions, false, newFilesetInclude)
print "Created new Options under Include!\n"
end
hParamsFilesetIncludeUpdate = {"File" => ["/home", "/var"]}
# BaculaConf::sectionUpdate(sSection, sectionID, hSectionParams, updateOnly = false)
# only add new parameters
if dirConfig.sectionUpdate("Include", newFilesetInclude, hParamsFilesetIncludeUpdate, true)
print "Updated section Include!\n"
end
end
hParamsJob = {"Name" => "Job-1", "Client" => newClient.searchParameter("Name"), "Fileset" => newFileset.searchParameter("Name")}
if newJob = dirConfig.sectionCreate("Job", hParamsJob)
print "Created new Job!\n"
end
dirConfig.printSections
Note that using Ruby 1.8 is deprecated - time to move on! :)
To use Ruby 1.8 and rubygem 'mysql', the beginning of the script has to be modified a bit:
#!/usr/bin/ruby
require "BaculaBase.rb"
require "BaculaConf.rb"
require "BaculaConfSection.rb"
require "BaculaConfMysql.rb"
baculaMysql = BaculaConfMysql.new({"host" => "localhost", "user" => "bacula-dir", "password" => "", "database" => "bacula-dir", "rubygem" => "mysql"})
Wiki: GettingStarted
Wiki: Home
Anonymous