Created
May 2, 2011 20:04
-
-
Save mdoel/952251 to your computer and use it in GitHub Desktop.
Rake task to capture backup and upload to S3
This file contains 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
require "heroku" | |
require "heroku/command" | |
require 'aws/s3' | |
namespace :backups do | |
desc "create a pg_dump and send to S3" | |
task :backup => :environment do | |
include RRBOUtil | |
HEROKU_USERNAME = heroku_gem_user | |
HEROKU_PASSWORD = heroku_gem_password | |
APP_NAME = heroku_app_name | |
BACKUP_BUCKET = environment == 'staging' ? 'stagingbackups' : 'prodbackups' | |
PATH_INSIDE_BUCKET = '' | |
puts "Backup started @ #{Time.now}" | |
heroku = Heroku::Client.new HEROKU_USERNAME, HEROKU_PASSWORD | |
puts "Capturing new pg_dump" | |
Heroku::Command.run_internal 'pgbackups:capture', ['--expire', '--app', APP_NAME], heroku | |
puts "Opening S3 connection" | |
config = YAML.load(File.open("#{RAILS_ROOT}/config/s3_data_file_config.yml"))[RAILS_ENV] | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => config['s3_credentials']['access_key_id'], | |
:secret_access_key => config['s3_credentials']['secret_access_key'] | |
) | |
begin | |
bucket = AWS::S3::Bucket.find(BACKUP_BUCKET) | |
rescue AWS::S3::NoSuchBucket | |
AWS::S3::Bucket.create(BACKUP_BUCKET) | |
bucket = AWS::S3::Bucket.find(BACKUP_BUCKET) | |
end | |
puts "Opening new pg_dump" | |
pg_backup = Heroku::Command::Pgbackups.new(['--app', APP_NAME], heroku) | |
local_pg_dump = open(pg_backup.pgbackup_client.get_latest_backup['public_url']) | |
puts "Finished opening new pg_dump" | |
puts "Uploading to S3 bucket" | |
AWS::S3::S3Object.store(Time.now.to_s(:number), local_pg_dump, bucket.name + PATH_INSIDE_BUCKET) | |
puts "Backup completed @ #{Time.now}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment