Skip to content
meskyanichi edited this page Mar 9, 2011 · 17 revisions

Databases

Currently supported databases:

  • MySQL
  • PostgreSQL
  • MongoDB
  • Redis

Examples

Below are examples you can copy/paste in to your "Backup" configuration file. These blocks should be placed between Backup::Model.new(:my_backup, 'My Backup') do and end.

MySQL

database MySQL do |db|
  db.name               = "my_database_name"
  db.username           = "my_username"
  db.password           = "my_password"
  db.host               = "localhost"
  db.port               = 3306
  db.socket             = "/tmp/mysql.sock"
  db.skip_tables        = ['skip', 'these', 'tables']
  db.only_tables        = ['only', 'these' 'tables']
  db.additional_options = ['--quick', '--single-transaction']
end

PostgreSQL

database PostgreSQL do |db|
  db.name               = "my_database_name"
  db.username           = "my_username"
  db.password           = "my_password"
  db.host               = "localhost"
  db.port               = 5432
  db.socket             = "/tmp/pg.sock"
  db.skip_tables        = ['skip', 'these', 'tables']
  db.only_tables        = ['only', 'these' 'tables']
  db.additional_options = ['--quick', '--single-transaction']
end

MongoDB

database MongoDB do |db|
  db.name               = "my_database_name"
  db.username           = "my_username"
  db.password           = "my_password"
  db.host               = "localhost"
  db.port               = 5432
  db.ipv6               = false
  db.only_collections   = ['only', 'these' 'collections']
  db.additional_options = []
end

Redis

database Redis do |db|
  db.name               = "my_database_name"
  db.path               = "/usr/local/var/db/redis"
  db.password           = "my_password"
  db.host               = "localhost"
  db.port               = 5432
  db.socket             = "/tmp/redis.sock"
  db.additional_options = []
  db.invoke_save        = true
end

Additional Notes

The db.name represents the database dump file on the filesystem in /usr/local/var/db/redis/. It'll be concatenated and it'll append the .rdb (Redis dump extension). So the above example will try to look for the database dump file in /usr/local/var/db/redis/my_database_name.rdb. The default name for the Redis dump file is just dump.rdb unless you change it in the configuration file youself.

The db.invoke_save, when set to true, it'll perform a SAVE command on the Redis process before backing up the dump file, so that the dump file is at it's most recent state.

Default Configuration for each database

If you are backing up multiple databases, you may want to specify default configuration so that you don't have to rewrite the same lines of code for each of the same database types. For example, say that the MySQL database always has the same username, password and additional_options. You could write this above the Model configurations.

Backup::Configuration::Database::MySQL.defaults do |db|
  db.username           = "my_username"
  db.password           = "my_password"
  db.additional_options = ["--single-transaction"]
end

So now for every MySQL database you wish to back up that requires the username, password and additional_options to be filled in with the defaults we just specified above, you may omit them in the actual database block, like so:

database MySQL do |db|
  db.name = "my_database_name"
  # no need to specify username
  # no need to specify password
  # no need to specify additional_options
end

You can set defaults for MongoDB, by changing the Backup::Configuration::Database::MySQL.defaults to Backup::Configuration::Database::MongoDB.defaults, and so forth for every supported database.

Clone this wiki locally