Skip to content

Instantly share code, notes, and snippets.

@robink
Created January 22, 2013 14:07
Show Gist options
  • Select an option

  • Save robink/4594889 to your computer and use it in GitHub Desktop.

Select an option

Save robink/4594889 to your computer and use it in GitHub Desktop.

Revisions

  1. Robin Komiwes created this gist Jan 22, 2013.
    33 changes: 33 additions & 0 deletions DbBackup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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
    4 changes: 4 additions & 0 deletions resque_schedule.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    db_backup:
    cron: "0 */24 * * *"
    class: DbBackup
    description: "Backup the Database each day"