Examples

Silver

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.

These lines have to be included at the beginning of every 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"})


Update table's reference on the first run (has to be done after updating database structure)

baculaMysql = BaculaConfMysql.new({"host" => "localhost", "user" => "bacula-dir", "password" => "", "database" => "bacula-dir"})
baculaMysql.updateTablesSchema()

Printing all sections from a DB (eg. for using in bacula-dir.conf)

#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"


Adding new client, fileset, job

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


The same script, but without any SQL

#!/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

Using Ruby 1.8

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"})

Related

Wiki: GettingStarted
Wiki: Home

Discussion


Anonymous

Cancel  Add attachments





Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks