Skip to content

Instantly share code, notes, and snippets.

Created February 19, 2013 10:09
Show Gist options
  • Save anonymous/4984592 to your computer and use it in GitHub Desktop.
Save anonymous/4984592 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Feb 19, 2013.
    7 changes: 7 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #...
    gem 'backup', require: false,
    github: 'kavu/backup',
    branch: 'bump_fog_version',
    ref: 'c3fd8e6eb4f464de1c8'
    gem 'whenever', require: false
    #...
    49 changes: 49 additions & 0 deletions config_backup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    # encoding: utf-8

    require 'backup'

    RAILS_ENV = ENV['RAILS_ENV'] || 'development'

    config = YAML.load_file File.expand_path('../database.yml', __FILE__)

    Backup::Storage::S3.defaults do |s3|
    s3.access_key_id = '...'
    s3.secret_access_key = '...'
    s3.region = '...'
    s3.bucket = '...'
    s3.keep = 25
    end

    Backup::Model.new(:postgres, 'Backup PostgreSQL database to S3') do
    database PostgreSQL do |db|
    db.name = config[RAILS_ENV]["database"]
    db.username = config[RAILS_ENV]["username"]
    db.password = config[RAILS_ENV]["password"]
    db.host = config[RAILS_ENV]["host"]
    db.port = config[RAILS_ENV]["port"]
    db.skip_tables = []
    end

    store_with S3 do |s3|
    s3.path = config[RAILS_ENV]["database"]
    end

    compress_with Bzip2

    split_into_chunks_of 250
    end

    Backup::Model.new(:redis, 'Backup Redis database to S3') do
    database Redis do |db|
    db.path = "..."
    db.invoke_save = true
    end

    store_with S3 do |s3|
    s3.path = "myapp_#{RAILS_ENV}"
    end

    compress_with Bzip2

    split_into_chunks_of 250
    end
    27 changes: 27 additions & 0 deletions config_deploy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #...
    require 'whenever/capistrano'
    #...

    set :whenever_command, "bundle exec whenever"

    set :bundle_flags, "--deployment --quiet --binstubs"

    set :default_environment, {
    'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
    }

    #...

    namespace :rbenv do
    task :rehash do
    run 'rbenv rehash'
    end
    end

    task :backup do
    run "cd #{current_path} && bundle exec rake backup RAILS_ENV=#{rails_env}"
    end

    #...

    after "bundle:install", "rbenv:rehash"
    7 changes: 7 additions & 0 deletions config_schedule.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # encoding: utf-8

    set :output, "log/cron.log"

    every 1.day, at: '04:00' do
    rake 'backup'
    end
    5 changes: 5 additions & 0 deletions lib_tasks_backup.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    desc "Back Postgres and Redis"
    task :backup do
    # Yeah, it's an ugly looking command
    sh "bundle exec backup perform -q -t postgres,redis --config-file config/backup.rb --root-path . --tmp-path public/system/backup/tmp --cache-path public/system/backup/cache --data-path public/system/backup/data"
    end
    1 change: 1 addition & 0 deletions post.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # How to make Rails + Backup + Whenever + Capistrano (and rbenv) work.