Created
December 16, 2015 17:27
-
-
Save trkrameshkumar/ad2dee781b9f8c966b93 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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 = Time.now.strftime("%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 | |
end | |
def send_to_amazon(file_path) | |
s3_config = YAML.load_file Rails.root.join("config/aws.yml") | |
file_name = File.basename(file_path) | |
s3 = AWS::S3.new(:access_key_id => 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"] | |
bucket.objects.create(file_name, File.open("#{file_path}")) | |
end | |
end | |
#Add the following line to config/schedule.rb | |
every :day, :at => '12:30am' do | |
rake "pg:backup" | |
end | |
#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 | |
http://blog.biasedwalk.com/2013/07/backing-up-postgres-to-amazon-s3-with.html?showComment=1450286410055 | |
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