Mission
A few months ago I’ve created a simple Rails app (after 6 years!!!) that essentially stores guests and keeping track of accommodations for my friend’s villa.
After a month in production I’ve started thinking about backup of the database. Code is on the Github so I have only need to worry about database.
My first initial thoughts were on Dropbox, I’ve installed the Dropbox client on my no GUI Ubuntu Server, but when I ran the Dropbox the memory usage got doubled and I remove it all together!
Then I started thinking about good old FTP backup, I was sure that I want backup somewhere else than the VPS where the database is running. I have one account at Arvixe, Unlimited Linux hosting and I’ve created the FTP user and even found this backup gem back then but I just drop all together until today.
I have VPS at BuyVM (ex Frantech) and they are offering a free storage VPS of 5Gb which is more than enough for my backup. I signed for an account and idea came again to use it as backup…
…and the idea expanded to include every day schedule of backup.
Incidentally, to day I finally get started my Octopress blog deployed via Github Pages, so I’m writting all the steps as notes for futre usage, or if someone can be useful…
backup Gem
The backup gem is one great gem with a lot of backup options like databases, storages, compressions, encrypt-ors,…
Install
Installation is a straight forward like any Ruby Gem:
1
|
|
Backup Model Generator
backup gem have its own backup “model” generator that creates backup config files, this is a comprehensive usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Generate a Backup model file
Let’s create simple config file with backup generator:
1 2 |
|
This will create a config.rb
and models/miguest_backup.rb
files in ~/Backup
folder.
Refine backup Configuration
Backup generator will create boilerplate model template needing more refinement. It looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
Fairly straight forward refinements are needed, usernames, passwords,… After adding all the secret stuff, the backup can be run as easy as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
And after the connecting to the storage server via FTP, there is backup:
But there is more to backup gem
Really, what I’m using for backup is just minimal what backup gem can offer, here is detailed impressive list of backup features:
Databases
MySQL, PostgreSQL, MongoDB, Redis, Riak
Compression
bzip2, gzip, lzma, pbzip2
Storages
cloudfiles, Dropbox, FTP, local, ninefold, rsync, S3, scp, SFTP
Notifiers
campfire, hipchat, mail, presently, prowl, twitter
Encryption
GPG, OpenSSL
Scheduling the backups with whenever Gem
Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs. And this is what I need to schedule the backups to run everyday.
Install
1
|
|
Configure (Wheneverize)
Configure the whenever, but first make sure that we are in backup folder ~/Backup
1
|
|
Then create config folder:
1
|
|
And then run the command:
1 2 3 |
|
Open the config/schedule.rb
file and add the following:
1 2 3 |
|
Schedule to crontab
Run whenever
with no arguments see the crontab
entry this will create:
1 2 3 4 5 |
|
To write (or update) this job in your crontab
, use:
1 2 |
|
And that’s it, so what is included:
- Backup of postgres database.
- Sending compressed backup via FTP.
- Scheduled backup every day at 4:30 am.
Update
The scheduled backups are working and here is the picture as proof!
Note that servers are not in same Time Zone hence offset of two hours. I know that one is in Chicago, and second is in California or New York, but either way the scheduled backup works!