Skip to content

Commit 0995810

Browse files
authored
Merge pull request #4725 from rubyforgood/clock-backup
Add RDS backup script
2 parents 247c977 + 029565e commit 0995810

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

clock.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ module Clockwork
2929
rake["reset_demo"].invoke
3030
end
3131
end
32+
33+
every(4.hours, "Backup prod DB to Azure blob storage", if: lambda { |_| Rails.env.production? }) do
34+
rake = Rake.application
35+
rake.init
36+
rake.load_rakefile
37+
rake["backup_db"].invoke
38+
end
3239
end

lib/tasks/backup_db_rds.rake

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
desc "Update the development db to what is being used in prod"
2+
task :backup_db => :environment do
3+
system("echo Performing dump of the database.")
4+
5+
current_time = Time.current.strftime("%Y%m%d%H%M%S")
6+
7+
system("echo Copying of the database...")
8+
backup_filename = "#{current_time}.rds.dump"
9+
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}")
10+
11+
account_name = ENV["AZURE_STORAGE_ACCOUNT_NAME"]
12+
account_key = ENV["AZURE_STORAGE_ACCESS_KEY"]
13+
14+
blob_client = Azure::Storage::Blob::BlobService.create(
15+
storage_account_name: account_name,
16+
storage_access_key: account_key
17+
)
18+
19+
system("echo Uploading #{backup_filename}")
20+
blob_client.create_block_blob("backups", backup_filename, File.read(backup_filename))
21+
end

lib/tasks/fetch_latest_db.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def fetch_latest_backups
5656
#
5757
# Retrieve the most up to date version of the DB dump
5858
#
59-
backup = backups.select { |b| b.name.match?(".dump") }.sort do |a,b|
59+
backup = backups.select { |b| b.name.match?(".rds.dump") }.sort do |a,b|
6060
Time.parse(a.properties[:last_modified]) <=> Time.parse(b.properties[:last_modified])
6161
end.reverse.first
6262

0 commit comments

Comments
 (0)