Created
February 17, 2012 00:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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