Creating a Backup Schedule using Backup Gem

1 min read

It’s always a good idea to back up your database or files at least once everday. It’s very dangerous to run a production server without setting up any automatic backups.
I prefer ruby Backup gem. If anything goes wrong, we can easily restore a recent backup.
SSH to your Production server and try the following steps,

1. Installation

To install the latest version, run:
[source]
$ gem install backup
[/source]

2. Generate the Model file

[source]
backup generate:model –trigger production_backup
[/source]
Generated model file stored in the location ‘~/Backup/models/production_backup.rb’.

3. Add the database detail

Here I am using MySQL database connection settings. Please read more about what are all other databases they support
[source]
Model.new(:production_backup, ‘Description for production_backup’) do
compress_with Gzip
##
# MySQL [Database]
#
database MySQL do |db|
# To dump all databases, set `db.name = :all` (or leave blank)
db.name = “productionDB”
db.username = “root”
db.password = “root”
db.host = “localhost”
db.port = 3306
db.additional_options = [“–quick”, “–single-transaction”]
end
end
[/source]

4. Storage

Backup will be uploaded to an Amazon S3 bucket. For this just follow AWS documentation.
[source]
Model.new(:production_backup, ‘Description for production_backup’) do
compress_with Gzip
##
# Store on Amazon S3
#
store_with S3 do |s3|
s3.access_key_id = “XXXX”
s3.secret_access_key = “XXXXXXXXXX”
s3.region = “us-east-1”
s3.bucket = “bucket-name”
s3.path = “”
end
end
[/source]

5. Perform the Backups

[source]backup perform –trigger production_backup[/source]

6. Scheduling the Backup

Use a cron task to invoke the Backup CLI.
cronjob
[source]
# run daily db backup store it into the aws bucket
0 0 * * * /bin/bash -l -c ‘/usr/local/rvm/gems/ruby-2.1.3/bin/backup perform -$
[/source]
To know more about the gem read it here.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *