Skip to content

Instantly share code, notes, and snippets.

@bitkidd
Last active February 18, 2016 00:35
Show Gist options
  • Save bitkidd/c1f996a475c161554f37 to your computer and use it in GitHub Desktop.
Save bitkidd/c1f996a475c161554f37 to your computer and use it in GitHub Desktop.
Rails Backup
# encoding: utf-8
##
# Backup Generated: app_backup
# Once configured, you can run the backup with the following command:
#
# $ backup perform -t app_backup [-c <path_to_configuration_file>]
#
# For more information about Backup's components, see the documentation at:
# http://backup.github.io/backup
#
# load login info
db_config = YAML.load_file('/absolute/path/to/your/database.yml')['production']
Model.new(:app_backup, 'your backup title') do
archive :vave_archive do |archive|
# Docs: http://backup.github.io/backup/v4/archives/
# Run the `tar` command using `sudo`
# archive.use_sudo
archive.add "/add/folders/to/include/in/tar/archive/"
end
##
# PostgreSQL [Database]
#
database PostgreSQL do |db|
# To dump all databases, set `db.name = :all` (or leave blank)
db.name = db_config['database']
db.username = db_config['username']
db.password = db_config['password']
db.host = "localhost"
#db.port = 5432
#db.socket = "/tmp/pg.sock"
# When dumping all databases, `skip_tables` and `only_tables` are ignored.
#db.skip_tables = ["skip", "these", "tables"]
#db.only_tables = ["only", "these", "tables"]
#db.additional_options = ["-xc", "-E=utf8"]
end
##
# Amazon Simple Storage Service [Storage]
#
store_with S3 do |s3|
# AWS Credentials
s3.access_key_id = "YOUR-AWESOME-KEY"
s3.secret_access_key = "YOUR-AWESOME-KEY"
# Or, to use a IAM Profile:
# s3.use_iam_profile = true
s3.storage_class = :reduced_redundancy
s3.region = "eu-central-1"
s3.bucket = "bucket-name"
s3.path = "/path/in/bucket"
s3.keep = 15
# s3.keep = Time.now - 2592000 # Remove all backups older than 1 month.
end
##
# Gzip [Compressor]
#
compress_with Gzip
##
# Mail [Notifier]
#
# The default delivery method for Mail Notifiers is 'SMTP'.
# See the documentation for other delivery options.
#
notify_by Mail do |mail|
mail.on_success = true
mail.on_warning = true
mail.on_failure = true
mail.from = "email-from@email.email"
mail.to = "email-to@email.email"
#mail.cc = "cc@email.com"
#mail.bcc = "bcc@email.com"
#mail.reply_to = "reply_to@email.com"
mail.address = "smtp-host"
mail.port = 587
#mail.domain = "your.host.name"
mail.user_name = "smtp-user"
mail.password = "smtp-password"
mail.authentication = "plain"
#mail.encryption = :starttls
end
end
gem install backup
  • Check available commands for generator
backup help generate:model
  • Generate backup model. I chose backup to s3, you may choose any of available in docs.
backup generate:model --trigger=app_backup --databases='postgresql' --storages='s3' --compressor='gzip' --notifiers='mail'
  • Following backup docs you can configure any kind of options for your specific case. My example is in app_backup.rb file
  • As you setup all the options, check the backup execution backup perform -t app_backup
  • To run backups on a schedule we need to install whenever
gem install whenever
  • When that finishes, create a /config directory for Whenever inside ~/Backup:
cd ~/Backup
mkdir config
  • Then run
wheneverize .
  • This will create a schedule.rb file in ~/Backup/config for writing your cron jobs. Below is the code I added to mine:
every 1.day, :at => '11:30 pm' do
  command "backup perform -t app_backup"
end
  • Cron will run the backup perform -t app_backup command. If you’d like to see this converted to cron syntax, run whenever:
$ whenever
30 23 * * * /bin/bash -l -c 'backup perform -t app_backup >> /home/bob/Backup/config/cron.log 2>&1'
  • To write all the jobs to crontab we need to run:
whenever --update-crontab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment