Skip to content
Brian D. Burns edited this page May 4, 2013 · 13 revisions

Generator

The Backup generator is a very useful little tool to help you set up backups faster.

To bring up the help screen, run the following command:

$ backup help generate:model

It'll display something like this:

Usage:
  backup generate:model -t, --trigger=TRIGGER

Options:
  -t, --trigger=TRIGGER  
      [--config-path=CONFIG_PATH]  # Path to your Backup configuration directory
      [--databases=DATABASES]      # (mongodb, mysql, postgresql, redis, riak)
      [--storages=STORAGES]        # (cloud_files, dropbox, ftp, local, ninefold, rsync, s3, scp, sftp)
      [--syncers=SYNCERS]          # (cloud_files, rsync_local, rsync_pull, rsync_push, s3)
      [--encryptors=ENCRYPTORS]    # (gpg, openssl)
      [--compressors=COMPRESSORS]  # (bzip2, custom, gzip, lzma, pbzip2)
      [--notifiers=NOTIFIERS]      # (campfire, hipchat, mail, prowl, pushover, twitter)
      [--archives]  
      [--splitter]                 # use `--no-splitter` to disable
                                   # Default: true

The options is what makes setting up a Backup configuration file a breeze.

Example

Say you have two databases, a MongoDB and a PostgreSQL database. You want to backup these two databases and compress them with Gzip. You then want to package them up, encrypt them with GPG, and store the backup to Amazon S3. Additionally, you have around 50GB of "user-uploaded-content" in /var/apps/my_app/public/uploads you would like to keep a mirror of on Amazon S3. And finally, you want to be notified by email if there are any problems.

To get up and running quickly, issue the following command:

$ backup generate:model --trigger my_backup \
    --databases="mongodb, postgresql" --storages="s3" --syncers="s3" \
    --encryptors="gpg" --compressors="gzip" --notifiers="mail"

This will create a new file: ~/Backup/models/my_backup.rb (the default location), and the file will look like this:

##
# Backup Generated: my_backup
# Once configured, you can run the backup with the following command:
#
# $ backup perform -t my_backup [-c <path_to_configuration_file>]
#
Backup::Model.new(:my_backup, 'Description for my_backup') do
  ##
  # Split [Splitter]
  #
  # Split the backup file in to chunks of 250 megabytes
  # if the backup file size exceeds 250 megabytes
  #
  split_into_chunks_of 250


  ##
  # MongoDB [Database]
  #
  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 = []
    db.lock               = false
    # Optional: Use to set the location of these utilities
    #   if they cannot be found by their name in your $PATH
    # db.mongodump_utility = "/opt/local/bin/mongodump"
    # db.mongo_utility     = "/opt/local/bin/mongo"
  end

  ##
  # PostgreSQL [Database]
  #
  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 = ["-xc", "-E=utf8"]
    # Optional: Use to set the location of this utility
    #   if it cannot be found by name in your $PATH
    # db.pg_dump_utility = "/opt/local/bin/pg_dump"
  end

  ##
  # Amazon Simple Storage Service [Storage]
  #
  # Available Regions:
  #
  #  - ap-northeast-1
  #  - ap-southeast-1
  #  - eu-west-1
  #  - us-east-1
  #  - us-west-1
  #
  store_with S3 do |s3|
    s3.access_key_id     = "my_access_key_id"
    s3.secret_access_key = "my_secret_access_key"
    s3.region            = "us-east-1"
    s3.bucket            = "bucket-name"
    s3.path              = "/path/to/my/backups"
    s3.keep              = 10
  end

  ##
  # Amazon S3 [Syncer]
  #
  # Available Regions:
  #
  #   - ap-northeast-1
  #   - ap-southeast-1
  #   - eu-west-1
  #   - us-east-1
  #   - us-west-1
  #
  # Mirroring:
  #
  #   When enabled it will keep an exact mirror of your filesystem on S3.
  #   This means that when you remove a file from the filesystem,
  #   it will also remote it from S3.
  #
  # Concurrency:
  #
  #   `concurrency_type` may be set to:
  #
  #     - false (default)
  #     - :threads
  #     - :processes
  #
  #   Set `concurrency_level` to the number of threads/processes to use.
  #   Defaults to 2.
  #
  sync_with Cloud::S3 do |s3|
    s3.access_key_id     = "my_access_key_id"
    s3.secret_access_key = "my_secret_access_key"
    s3.bucket            = "my-bucket"
    s3.region            = "us-east-1"
    s3.path              = "/backups"
    s3.mirror            = true
    s3.concurrency_type  = false
    s3.concurrency_level = 2

    s3.directories do |directory|
      directory.add "/path/to/directory/to/sync"
      directory.add "/path/to/other/directory/to/sync"
    end
  end

  ##
  # GPG [Encryptor]
  #
  # Setting up #keys, as well as #gpg_homedir and #gpg_config,
  # would be best set in config.rb using Encryptor::GPG.defaults
  #
  encrypt_with GPG do |encryption|
    # Setup public keys for #recipients
    encryption.keys = {}
    encryption.keys['[email protected]'] = <<-KEY
      -----BEGIN PGP PUBLIC KEY BLOCK-----
      Version: GnuPG v1.4.11 (Darwin)

          <Your GPG Public Key Here>
      -----END PGP PUBLIC KEY BLOCK-----
    KEY

    # Specify mode (:asymmetric, :symmetric or :both)
    encryption.mode = :both # defaults to :asymmetric

    # Specify recipients from #keys (for :asymmetric encryption)
    encryption.recipients = ['[email protected]']

    # Specify passphrase or passphrase_file (for :symmetric encryption)
    encryption.passphrase = 'a secret'
    # encryption.passphrase_file = '~/backup_passphrase'
  end

  ##
  # Gzip [Compressor]
  #
  compress_with Gzip

  ##
  # Mail [Notifier]
  #
  # The default delivery method for Mail Notifiers is 'SMTP'.
  # See the Wiki for other delivery options.
  # https://github.com/meskyanichi/backup/wiki/Notifiers
  #
  notify_by Mail do |mail|
    mail.on_success           = true
    mail.on_warning           = true
    mail.on_failure           = true

    mail.from                 = "[email protected]"
    mail.to                   = "[email protected]"
    mail.address              = "smtp.gmail.com"
    mail.port                 = 587
    mail.domain               = "your.host.name"
    mail.user_name            = "[email protected]"
    mail.password             = "my_password"
    mail.authentication       = "plain"
    mail.encryption           = :starttls
  end

end

Just omit what you don't need, and change what you do need and you're done.

Note: If you want to change the path where the model file will be generated, use the --config-path option to specify the path to the directory where Backup's main configuration file is located. So, if you have your main configuration file in /path/to/config.rb, then you would generate your models using:

$ backup generate:model --config-path='/path/to/' --trigger (etc...)

Main Configuration File

Generating the model above will also create the main Backup configuration file: ~/Backup/config.rb

This is the first file Backup will load when performing your backup job. This is where you will setup any global configuration and component defaults. After which, it will load all of your backup model files.

##
# Backup
# Generated Main Config Template
#
# For more information:
#
# View the Git repository at https://github.com/meskyanichi/backup
# View the Wiki/Documentation at https://github.com/meskyanichi/backup/wiki
# View the issue log at https://github.com/meskyanichi/backup/issues

##
# Utilities
#
# If you need to use a utility other than the one Backup detects,
# or a utility can not be found in your $PATH.
#
#   Backup::Utilities.configure do
#     tar       '/usr/bin/gnutar'
#     redis_cli '/opt/redis/redis-cli'
#   end

##
# Logging
#
# Logging options may be set on the command line, but certain settings
# may only be configured here.
#
#   Backup::Logger.configure do
#     console.quiet     = true            # Same as command line: --quiet
#     logfile.max_bytes = 2_000_000       # Default: 500_000
#     syslog.enabled    = true            # Same as command line: --syslog
#     syslog.ident      = 'my_app_backup' # Default: 'backup'
#   end
#
# Command line options will override those set here.
# For example, the following would override the example settings above
# to disable syslog and enable console output.
#   backup perform --trigger my_backup --no-syslog --no-quiet

##
# Component Defaults
#
# Set default options to be applied to components in all models.
# Options set within a model will override those set here.
#
#   Backup::Storage::S3.defaults do |s3|
#     s3.access_key_id     = "my_access_key_id"
#     s3.secret_access_key = "my_secret_access_key"
#   end
#
#   Backup::Encryptor::OpenSSL.defaults do |encryption|
#     encryption.password = "my_password"
#     encryption.base64   = true
#     encryption.salt     = true
#   end


# * * * * * * * * * * * * * * * * * * * *
#        Do Not Edit Below Here.
# All Configuration Should Be Made Above.

##
# Load all models from the models directory.
Dir[File.join(File.dirname(Config.config_file), "models", "*.rb")].each do |model|
  instance_eval(File.read(model))
end

For more information on the Utilities configuration, see the Utilities page.
For more information on the Logger configuration, see the Logging page.

By default, Backup will look for this file in ~/Backup/config.rb. If you want to place your configuration files in a different location, use the --config_file option:

$ backup perform --trigger my_backup --config_file '/path/to/config.rb'

If you relocate this file, be sure to move the models directory as well.

If you need to re-generate only this main configuration file, you can do so using:

$ backup generate:config
Clone this wiki locally