Skip to content

Commit

Permalink
Merge pull request #4725 from rubyforgood/clock-backup
Browse files Browse the repository at this point in the history
Add RDS backup script
  • Loading branch information
awwaiid authored Oct 26, 2024
2 parents 247c977 + 029565e commit 0995810
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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

0 comments on commit 0995810

Please sign in to comment.