Monday, November 22, 2010

Build Your Own Backup System on Linode

I have recently finished moving couple of our virtual servers instances from Slicehost to Linode, and I'm quite happy with them so far. Very flexible with their Web interface, decent support and about two times less expensive than Slicehost (which adds up pretty quickly if you run multiple instances). One more good thing is that you can actually build _your own_ backup subsystem using their API and allocated storage space, without paying anything extra (while you can still pay to use pre-built one :). E.g. here is a quick Ruby script that, run weekly, creates a copy of a disk. You need to keep enough of free space though, however, this wasn't an issue for me (I had about twice as much free for every instance).


require 'rubygems'

require 'linode'


api_key = 'SecretKeyGoesHere'

linode_id = 12345

disk_id = 67890

backup_disk_id = 0

backup_label = 'weekly'


l = =>; api_key)


# 1. Find the backup named 'XXX' and remove it first

result = l.linode.disk.list(:LinodeId => linode_id)

result.each do |i|

    backup_disk_id = i.diskid if i.label == backup_label


p "Backup disk named #{backup_label} # #{backup_disk_id}"


# 2. Remove old backup

remove_job = l.linode.disk.delete(:LinodeId => linode_id, :DiskId => backup_disk_id) if backup_disk_id != 0

p "Remove job status: #{remove_job}"


# 3. Duplicate disk

duplicate_job = l.linode.disk.duplicate(:LinodeId => linode_id, :DiskId => disk_id)

p "Duplicate job status: #{duplicate_job}"


# 4. Rename duplicated disk as 'XXX'

rename_job = l.linode.disk.update(:LinodeId => linode_id, :DiskId => duplicate_job.diskid, :Label => backup_label)

p "Rename job status: #{rename_job}"


P.S. I am so pissed off with broken markdown support at Posterous, that I am ready to give up and move my blog away from it.

# Posted via email from opportunity__cost