Skip to content

Instantly share code, notes, and snippets.

@rposborne
Created October 20, 2015 19:37
Show Gist options
  • Save rposborne/254397d018527afe1ad2 to your computer and use it in GitHub Desktop.
Save rposborne/254397d018527afe1ad2 to your computer and use it in GitHub Desktop.
Spree Sync Production -> Local
# uploads assets to s3 under assets/githash, deletes stale assets
namespace :s3 do
task :download_paperclip_from_s3 do
mkdir_p './public/attachments/development/images/'
`s3cmd sync s3://#{ENV['PRODUCTION_S3_BUCKET_NAME']}/images/ ./public/attachments/development/images/ --acl-public >&2`
end
task :sync_images_to_staging do
mkdir_p './public/attachments/development/'
`s3cmd sync s3://#{ENV['PRODUCTION_S3_BUCKET_NAME']}/images/ s3://#{ENV['STAGING_S3_BUCKET_NAME']}/images/ --acl-public >&2`
end
task :upload_paperclip_to_s3 do
`s3cmd sync ./public/ s3://#{ENV['PRODUCTION_S3_BUCKET_NAME']}/app/public/ --acl-public >&2`
end
task pull_db_backup: :environment do
require 'fog/storage'
# Setup scratch directory
backup_path = setup_backup_folder
# Connect to s3
connection = Fog::Storage.new(
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
)
# Return all files that have keys and get most recent
backup_s3 = connection.directories.get('backups')
last_backup = backup_s3.files.map do |file|
file if file.key.include?('PROJECTNAME')
end.compact.sort_by(&:last_modified).last
# Download and save encrypted file from s3
encrypted_file = File.join(backup_path, 'latest.enc')
File.open(encrypted_file, 'w') do |local_file|
local_file.write(last_backup.body)
end
# Unencrypt file
decrypted_file = File.join(backup_path, 'latest.tar')
decrypt_file(encrypted_file, decrypted_file)
# Find any tar's and unstuff them
unpack_tar(backup_path)
# Find any compressed sql and unstuff it
unpack_gunzip(backup_path)
# Restore DB
restore_from_sql Dir[(backup_path + '/**/*.sql')].last
rm_r(Dir.glob(File.join(backup_path, '*')))
end
end
##
# => Run cx:pull and s3:download_paperclip_from_s3
task sync: ['s3:download_paperclip_from_s3',
's3:pull_db_backup',
'toggle_payment_gateway_keys'] do
puts 'Last Production Backup Downloaded and Restored to local machine'
end
task hard_sync: ['db:drop',
'db:create',
'db:schema:load',
'sync'] do
puts 'Last Production Backup Downloaded and Restored to local machine'
end
task toggle_payment_gateway_keys: :environment do
puts 'Swapping production payment keys'
Spree::PaymentMethod.all.each { |p| p.toggle! :active }
end
def decrypt_file(path, output)
`openssl aes-256-cbc -d -base64 -in #{path} -out #{output} -k #{ENV['OPENSSL_PASSWORD']}`
end
def restore_from_sql(path, env: Rails.env)
db_config = Rails.configuration.database_configuration[env]
database = db_config['database']
username = db_config['username']
system("psql -f #{path} -d #{database} -U #{username}")
end
def restore_from_dump(_path, env: Rails.env)
db_config = Rails.configuration.database_configuration[env]
database = db_config['database']
username = db_config['username']
host = db_config['host']
Bundler.with_clean_env do
`pg_restore --clean --no-acl --no-owner -h #{host} -U #{username} -d #{database} tmp/latest.dump >&2`
end
end
def unpack_gunzip(path)
last_sql_gz = Dir[(path + '/**/*.gz')].last
`cd #{path}; gunzip #{last_sql_gz}`
end
def unpack_tar(path)
last_backup = Dir[(path + '/*.tar')].last
`tar -zxvf #{last_backup} -C #{path}`
end
def setup_backup_folder
backup_path = File.expand_path('tmp/backups/')
tmp_path = File.expand_path('tmp/')
Dir.mkdir tmp_path unless Dir.exist? tmp_path
Dir.mkdir backup_path unless Dir.exist? backup_path
backup_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment