Skip to content

Commit

Permalink
Add RDS backup script
Browse files Browse the repository at this point in the history
  • Loading branch information
dorner committed Oct 20, 2024
1 parent 64d20c1 commit a600849
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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

0 comments on commit a600849

Please sign in to comment.