Skip to content

Instantly share code, notes, and snippets.

@robink
Created January 22, 2013 14:07
Show Gist options
  • Save robink/4594889 to your computer and use it in GitHub Desktop.
Save robink/4594889 to your computer and use it in GitHub Desktop.
Resque DbBackup job - A simple mysql database backup job with an upload to a cloud container at the end (rackspace cloud files). Require resque scheduler.
module DbBackup
@queue = :db_backup
def perform( forced_filename = nil )
config = Rails.configuration.database_configuration
database = config[Rails.env]["database"]
username = config[Rails.env]["username"]
password = config[Rails.env]["password"]
dumps_path = Rails.root.join('tmp', 'dumps').to_s
dump_filename = forced_filename || Socket.gethostname.to_s + "-" + Rails.env + "-" + Time.now.strftime("%Y-%m-%d--%H-%M") + ".sql"
dump_path = Rails.root.join('tmp', 'dumps', dump_filename )
Utils.run 'mkdir -p ' + dumps_path
Utils.run "mysqldump -u\"#{username}\" -p\"#{password}\" #{database} -r #{dump_path}"
cloudfile = CloudfilesConnect.cloudfile
container_name = "db_dumps"
if !cloudfile.container_exists? container_name
CloudfilesConnect.cloudfile.create_container( container_name )
end
container = CloudfilesConnect::cloudfile.container( container_name )
o = container.create_object( dump_filename )
o.write( File.open( dump_path ) )
File.delete( dump_path )
end
extend self
end
db_backup:
cron: "0 */24 * * *"
class: DbBackup
description: "Backup the Database each day"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment