Skip to content

Instantly share code, notes, and snippets.

@manuelmeurer
Created July 7, 2014 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelmeurer/c0acda13e6f7fb350c1e to your computer and use it in GitHub Desktop.
Save manuelmeurer/c0acda13e6f7fb350c1e to your computer and use it in GitHub Desktop.
Download backup containing Postgres and Redis dumps from S3 and import into local db
desc 'Download and import the lastest backup from S3'
task import_backup: :environment do
# Configure AWS
AWS.config \
access_key_id: Settings.aws.backup.key,
secret_access_key: Settings.aws.backup.secret,
region: Settings.aws.backup.region
# Download and extract backup file
bucket_name = 'kc-backups'
bucket = AWS::S3.new.buckets[bucket_name]
last_backup = bucket.objects.with_prefix('productwidgets/').map(&:key).last
puts "Last backup: #{last_backup}"
tmp_file = Rails.root.join('tmp', [bucket_name, last_backup.gsub(%r(/), '_')].join('_'))
if File.exists?(tmp_file)
puts 'Already downloaded.'
else
puts 'Downloading...'
open tmp_file, 'w' do |file|
file.write bucket.objects[last_backup].read.force_encoding(Encoding::UTF_8)
end
end
extract_folder = File.join(File.dirname(tmp_file), File.basename(tmp_file).gsub(/[^0-9a-z]/, '_'))
if File.exists?(extract_folder)
puts 'Already extracted.'
else
puts 'Extracting...'
%x(
mkdir -p #{extract_folder} &&
cd #{extract_folder} &&
tar xvzf #{tmp_file}
)
end
# Import Postgres
postgres_backup_gz = "#{extract_folder}/backup/databases/PostgreSQL.sql.gz"
raise 'No gzipped Postgres backup file found.' unless File.exist?(postgres_backup_gz)
postgres_backup = postgres_backup_gz.sub(/\.gz\z/, '')
if File.exists?(postgres_backup)
puts 'Postgres backup already extracted.'
else
puts 'Extracting Postgres backup...'
%x(gunzip -fc #{postgres_backup_gz} > #{postgres_backup})
end
raise "Could not extract gzipped Postgres backup file #{postgres_backup_gz} to #{postgres_backup}." unless File.exists?(postgres_backup)
db_config = ActiveRecord::Base.configurations[Rails.env].symbolize_keys
puts 'Importing Postgres backup...'
%x(
bundle exec rake db:drop &&
bundle exec rake db:create &&
psql #{db_config[:database]} < #{postgres_backup}
)
# Import Redis
redis_backup_gz = "#{extract_folder}/backup/databases/Redis.rdb.gz"
raise 'No gzipped Redis backup file found.' unless File.exist?(redis_backup_gz)
redis_backup = redis_backup_gz.sub(/\.gz\z/, '')
if File.exist?(redis_backup)
puts 'Redis backup already extracted.'
else
puts 'Extracting Redis backup...'
%x(gunzip -c #{redis_backup_gz} > #{redis_backup})
end
puts 'Importing Redis backup...'
# TODO: Check if Redis is running using `redis-cli ping`. If it is, raise error saying it has to be stopped before importing backup
%x(cp #{redis_backup} #{Rails.root.join('tmp', 'redis.rdb')})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment