Backing Up PostgreSQL With Backup and Whatever Gems on VPS
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…
The backup gem is one great gem with a lot of
backup options like databases, storages, compressions, encrypt-ors,…
Installation is a straight forward like any Ruby Gem:
$ gem install backup
Backup Model Generator
backup gem have its own backup “model” generator that creates backup config files, this is a comprehensive usage:
# encoding: utf-8### Backup Generated: miguest_backup# Once configured, you can run the backup with the following command:## $ backup perform -t miguest_backup [-c <path_to_configuration_file>]#Backup::Model.new(:miguest_backup,'Description for miguest_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_of250### PostgreSQL [Database]#databasePostgreSQLdo|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.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### FTP (File Transfer Protocol) [Storage]#store_withFTPdo|server|server.username="my_username"server.password="my_password"server.ip="22.214.171.124"server.port=21server.path="~/backups/"server.keep=5server.passive_mode=falseend### Bzip2 [Compressor]#compress_withBzip2end
Fairly straight forward refinements are needed, usernames, passwords,… After adding all
the secret stuff, the backup can be run as easy as:
$ backup perform --trigger miguest_backup
[2012/10/10 12:21:26][message] Performing Backup for'Description for miguest_backup (miguest_backup)'!
[2012/10/10 12:21:26][message][ backup 3.0.25 : ruby 1.9.3p194 (2012-04-20 revision 35410)[x86_64-linux]][2012/10/10 12:21:26][message] Database::PostgreSQL started dumping and archiving 'miguest'.
[2012/10/10 12:21:26][message] Using Compressor::Bzip2 for compression.
[2012/10/10 12:21:26][message] Command: '/bin/bzip2'[2012/10/10 12:21:26][message] Ext: '.bz2'[2012/10/10 12:21:28][message] Database::PostgreSQL Complete!
[2012/10/10 12:21:28][message] Packaging the backup files...
[2012/10/10 12:21:28][message] Splitter configured with a chunk size of 250MB.
[2012/10/10 12:21:28][message] Packaging Complete!
[2012/10/10 12:21:28][message] Cleaning up the temporary files...
[2012/10/10 12:21:29][message] Storage::FTP started transferring '2012.10.10.12.21.26.miguest_backup.tar' to 'ftp.services.buyvm.net'.
[2012/10/10 12:21:29][message] Storage::FTP: Cycling Started...
[2012/10/10 12:21:29][message] Storage::FTP: Cycling Complete!
[2012/10/10 12:21:29][message] Cleaning up the package files...
[2012/10/10 12:21:29][warning] Backup for'Description for miguest_backup (miguest_backup)' Completed Successfully (with Warnings) in 00:00:03
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:
Run whenever with no arguments see the crontab entry this will create:
30 4 * * * /bin/bash -l -c 'backup perform --trigger miguest_backup'## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.## [message] Run `whenever --help' for more options.
To write (or update) this job in your crontab, use:
$ whenever --update-crontab
~ [write] crontab file written
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.
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