Skip to content

Instantly share code, notes, and snippets.

@handerson
Created February 17, 2012 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handerson/1849229 to your computer and use it in GitHub Desktop.
Save handerson/1849229 to your computer and use it in GitHub Desktop.
Creates a backup SQLite DB and backs it up to Rackspace Cloudfiles and maintains 14 days worth of backups
require 'rubygems'
require 'cloudfiles'
require 'sqlite3'
cf = CloudFiles::Connection.new(:username => "", :api_key => "")
original = = SQLite3::Database.new('/var/www/project/current/db/original.db')
backup = SQLite3::Database.new('/home/deploy/backup.sqlite3')
backup_process = SQLite3::Backup.new(backup, 'main', original, 'main')
backup_process.step(-1)
backup_process.finish
original.close
backup.close
file = File.new("/home/deploy/backup.sqlite3")
if file
container = cf.container('backups')
object = container.create_object "servername_backup_#{file.mtime.strftime("%Y-%m-%d")}.sql", false
object.write file
end
file.close
backups = container.objects.select{|o| o =~ /servername.+/}
if backups.length > 14
backups.sort!
container.delete_object(backups.first)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment