File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
3239end
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments