Oh, the memories

Ruby Add comments

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 backups. Enjoy.

7 Responses to “Oh, the memories”

  1. Clemens Kofler Says:

    Sorry, but I don’t think this stuff is really necessary. You can accomplish something quite similar with just Capistrano and some rake task that you run with cron.

    Anyway, it’s always good to have options.

  2. Greg Says:

    “sudo gem install engineyard-backup” doesn’t seem to work. Instead, it should be “sudo gem install fearoffish-backup”

  3. Jamie van Dyke Says:

    Clemens => But that’s not a part of your app, this gem allows you (as part of your own app or gem) to backup a file you generate. What I’m describing here can be done with capistrano, but it’s an ugly hack to a somewhat simple problem.

    Greg => It should have migrated over to the engineyard repository by now, but if it hasn’t and it stops in future, you know why. ;-)

  4. ARJ Says:

    This is kinda convenient for a couple things I’ve got going. Not sure why I’d do some orthogonal capistrano+rake+cron mucking around or whatever; hacky and more work, I agree.

    Um, link to rdocs? Or is this another “download to see if you really want to download” ruby lib?

  5. Jamie van Dyke Says:

    ARJ - Sorry about that. Until we get an Engine Yard ‘home’ for our projects, I’ve temporarily placed the rdoc here: http://blog.fearoffish.com/backup/

  6. Bob Says:

    keep_files and delete_files should be keep_list and delete_list.

  7. Jamie van Dyke Says:

    Bob - thanks for that, silly mistake on my part. I’ve updated the article.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in