From a6008499ad36c1c24b6f48a33bbee40800ca2825 Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Sun, 20 Oct 2024 10:39:23 -0400 Subject: [PATCH 1/3] Add RDS backup script --- clock.rb | 7 +++++++ lib/tasks/backup_db_rds.rake | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 lib/tasks/backup_db_rds.rake diff --git a/clock.rb b/clock.rb index 9c5fb0d81d..28c98d8874 100644 --- a/clock.rb +++ b/clock.rb @@ -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 diff --git a/lib/tasks/backup_db_rds.rake b/lib/tasks/backup_db_rds.rake new file mode 100644 index 0000000000..26e2579a3e --- /dev/null +++ b/lib/tasks/backup_db_rds.rake @@ -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 From ba0875314d9850e8e4751236621f7e08bb3ee8ad Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Sun, 20 Oct 2024 10:40:51 -0400 Subject: [PATCH 2/3] Use new filename --- lib/tasks/fetch_latest_db.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/fetch_latest_db.rake b/lib/tasks/fetch_latest_db.rake index fab4e2fd6a..394087a141 100644 --- a/lib/tasks/fetch_latest_db.rake +++ b/lib/tasks/fetch_latest_db.rake @@ -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 From 029565e068d7744f7244ebe6f7686f2cb5876679 Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Sun, 20 Oct 2024 10:46:09 -0400 Subject: [PATCH 3/3] fix lint --- clock.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clock.rb b/clock.rb index 28c98d8874..7b87290673 100644 --- a/clock.rb +++ b/clock.rb @@ -30,7 +30,7 @@ module Clockwork end end - every(4.hours, "Backup prod DB to Azure blob storage", :if => lambda { |_| Rails.env.production? }) do + every(4.hours, "Backup prod DB to Azure blob storage", if: lambda { |_| Rails.env.production? }) do rake = Rake.application rake.init rake.load_rakefile