Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#First you have add the following gems to your Gemfile
gem 'aws-sdk', '< 2.0'
gem 'zip'
gem 'whenever'
#Created a new file at lib/tasks/backup.rake and add the following
desc "PG Backup"
namespace :pg do
task :backup => [:environment] do
#stamp the filename
datestamp ="%Y-%m-%d_%H-%M-%S")
#drop it in the db/backups directory temporarily
app_name = 'marketplace-ui'
backup_file = "#{Dir.home}/db_backups/#{app_name}_#{Rails.env}_#{datestamp}_dump.sql.gz"
#dump the backup and zip it up
user = Rails.application.secrets.pg_user
password = Rails.application.secrets.pg_password
database = Rails.application.secrets.database
sh_cmd = "PGPASSWORD=#{password} pg_dump -h localhost -U #{user} #{database}| gzip -c > #{backup_file}"
sh sh_cmd
send_to_amazon backup_file
#remove the file on completion so we don't clog up our app
File.delete backup_file
def send_to_amazon(file_path)
s3_config = YAML.load_file Rails.root.join("config/aws.yml")
file_name = File.basename(file_path)
s3 = => s3_config[Rails.env]["access_key_id"],:secret_access_key => s3_config[Rails.env]["secret_access_key"])
#push the file up
bucket = s3.buckets[s3_config[Rails.env]["bucket"]+"/db_backups"]
#Add the following line to config/schedule.rb
every :day, :at => '12:30am' do
rake "pg:backup"
#Add the following line to config/deploy.rb
set :whenever_command, "bundle exec whenever"
require 'whenever/capistrano'
Note: This Gist is based on this old post
but the above author uses aws-sdk version 1, here we are using aws-sdk version 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment