Jamie van Dyke is proficient in Ruby (and Rails). He teaches, he codes and is working for boxedup.
Oh, the Memories
inscribed on 03 May 2008
Let’s pretend that you’re busy making a kick ass template maker, and you’d like to keep backups of the file you generate. You certainly don’t want to write over it every time, in case there’s customer changes that have been made since the last time you did. What if you want to keep 5 copies of it, and only 5?
Rather than code it all yourself, save yourself some trouble and download the Backup gem off github, and you’re ready to go:
Install
$ sudo gem sources -a http://gems.github.com
$ sudo gem install engineyard-backup
You only need to install the source once and then you can install any gem hosted on github, which basically means any repository that has a .gemspec at the root of their repository.
Usage
backup = Backup.new("/the/file/to/backup")
backup.run
So what exactly have we done here? Well, we’ve made a copy of the file we wanted backed up, and we’ve added a timestamp to it. Every time we run this file, Backup will move the current file to a timestamped version, and also ensure that (by default) 5 versions of that file are kept.
Want a different number of ‘releases’?
# Set a different amount of backups on initialize
backup = Backup.new("/the/file/to/backup", 10)
# or set it manually
backup.backups = 15
# Run the backup
backup.run
Some more methods:
backup = Backup.new("/the/file/to/backup")
# which files would we keep in a backup
backup.keep_list
# => ["backup.20080430185243", "backup.20080430185246", "backup.20080430185243", "backup.20080430185250", "backup.20080430185254"]
# which files would we delete in a backup
backup.delete_list
# => ["backup.20080430185221", "backup.20080430185224", "backup.20080430185230"]
# how about skipping the cleanup?
backup.run( :no_delete )
# what if we just want to cleanup the old revisions
backup.cleanup
# and finally, how about we just get a list of the backup files present
backup.find_all_releases
Notes
This library doesn’t care where it’s being used, it could be a Rails application or a command line Ruby script of some sort. It merely handles backups of files in the form of a library. If you just want to backup some files and not programmatically from within your own application, then these are not the droids you are looking for.
Conclusion
Easy peasy, lemon squeezy coded backups. Enjoy.

Recent Comments