Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ module Clockwork
rake["reset_demo"].invoke
end
end

every(4.hours, "Backup prod DB to Azure blob storage", if: lambda { |_| Rails.env.production? }) do
rake = Rake.application
rake.init
rake.load_rakefile
rake["backup_db"].invoke
end
end
21 changes: 21 additions & 0 deletions lib/tasks/backup_db_rds.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
desc "Update the development db to what is being used in prod"
task :backup_db => :environment do
system("echo Performing dump of the database.")

current_time = Time.current.strftime("%Y%m%d%H%M%S")

system("echo Copying of the database...")
backup_filename = "#{current_time}.rds.dump"
system("PGPASSWORD=#{ENV["DIAPER_DB_PASSWORD"]} pg_dump -Fc -v --host=#{ENV["DIAPER_DB_HOST"]} --username=#{ENV["DIAPER_DB_USERNAME"]} --dbname=#{ENV["DIAPER_DB_DATABASE"]} -f #{backup_filename}")

account_name = ENV["AZURE_STORAGE_ACCOUNT_NAME"]
account_key = ENV["AZURE_STORAGE_ACCESS_KEY"]

blob_client = Azure::Storage::Blob::BlobService.create(
storage_account_name: account_name,
storage_access_key: account_key
)

system("echo Uploading #{backup_filename}")
blob_client.create_block_blob("backups", backup_filename, File.read(backup_filename))
end
2 changes: 1 addition & 1 deletion lib/tasks/fetch_latest_db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def fetch_latest_backups
#
# Retrieve the most up to date version of the DB dump
#
backup = backups.select { |b| b.name.match?(".dump") }.sort do |a,b|
backup = backups.select { |b| b.name.match?(".rds.dump") }.sort do |a,b|
Time.parse(a.properties[:last_modified]) <=> Time.parse(b.properties[:last_modified])
end.reverse.first

Expand Down