I’m using meskyanichi’s backup gem. By and large it does what I need it to, but I need to have multiple backups (e.g., hourly, daily, weekly). The configurations are mostly the same but have a few differences, so I need to have multiple configuration files. I’m having trouble finding a sane way to manage the common bits of configurations (i.e., not repeat the common parts).
The configuration files use a lot of block structures, and from what I can tell, each backup needs to have a separate config file (e.g. config/backup/hourly.rb, config/backup/daily.rb, etc). A typical config file looks like this:
Backup::Model.new(:my_backup, 'My Backup') do
database MySQL do |db|
db.name = "my_database"
db.username = "foo"
db.password = "bar"
# etc
end
# similar for other config options
end
Then the backup is executed a la bundle exec backup perform -t my_backup -c path/to/config.rb.
My first swag at enabling a common config was to define methods that I could call from the blocks:
def my_db_config db
db.name = "my_database"
# etc
end
Backup::Model.new(:my_backup, 'My Backup') do
database MySQL do |db|
my_db_config db
end
#etc
end
But this fails with an undefined method 'my_db_config' for #<Backup::Database::MySQL:0x10155adf0>.
My intention was to get this to work and then split the common config functions into another file that I could require in each of my config files. I also tried creating a file with the config code and requireing it into the model definition block:
# common.rb
database MySQL do |db|
db.name = "my_database"
#etc
end
# config.rb
Backup::Model.new(:my_backup, 'My Backup') do
require "common.rb" # with the right path, etc
end
This also doesn’t work, and from subsequent research I’ve discovered that that’s just not the way that require works. Something more in line with the way that C/C++’s #include works (i.e., blindly pasting the contents into whatever scope it is called from) might work.
Any ideas?
The gem seems to modify the execution scope of the config blocks. To work around this, you could wrap your functions in a class:
You could get a bit more fancy and abstract your default config merge: