1+ require 'aws-sdk-s3'
2+
13desc "Update the development db to what is being used in prod"
2- BACKUP_CONTAINER_NAME = ' backups'
4+ BUCKET_NAME = "human-essentials- backups"
35PASSWORD_REPLACEMENT = 'password'
46
57task :fetch_latest_db do
@@ -13,7 +15,7 @@ task :fetch_latest_db do
1315 system ( "bin/rails db:environment:set RAILS_ENV=development" )
1416 system ( "bin/rails db:drop db:create" )
1517
16- puts "Restoring the database with #{ backup . name } "
18+ puts "Restoring the database with #{ backup . key } "
1719 backup_filepath = fetch_file_path ( backup )
1820 db_username = ENV [ "PG_USERNAME" ] . presence || ENV [ "USER" ] . presence || "postgres"
1921 db_host = ENV [ "PG_HOST" ] . presence || "localhost"
5153private
5254
5355def fetch_latest_backups
54- backups = blob_client . list_blobs ( BACKUP_CONTAINER_NAME )
56+ backups = blob_client . list_objects_v2 ( bucket : BUCKET_NAME )
5557
5658 #
5759 # Retrieve the most up to date version of the DB dump
5860 #
59- backup = backups . select { |b | b . name . match? ( ".rds.dump" ) } . sort do |a , b |
60- Time . parse ( a . properties [ : last_modified] ) <=> Time . parse ( b . properties [ : last_modified] )
61+ backup = backups . contents . select { |b | b . key . match? ( ".rds.dump" ) } . sort do |a , b |
62+ Time . parse ( a . last_modified ) <=> Time . parse ( b . last_modified )
6163 end . reverse . first
6264
6365 #
6466 # Download each of the backups onto the local disk in tmp
6567 #
6668 filepath = fetch_file_path ( backup )
67- puts "\n Downloading blob #{ backup . name } to #{ filepath } "
68- blob , content = blob_client . get_blob ( BACKUP_CONTAINER_NAME , backup . name )
69- File . open ( filepath , "wb" ) { |f | f . write ( content ) }
69+ puts "\n Downloading blob #{ backup . key } to #{ filepath } "
70+ blob_client . get_object ( bucket : BUCKET_NAME , key : backup . key , response_target : filepath )
7071
7172 #
7273 # At this point, the dumps should be stored on the local
7374 # machine of the user under tmp.
7475 #
75- return backup
76+ backup
7677end
7778
7879def blob_client
79- return @blob_client if @blob_client
80-
81- account_name = ENV [ "AZURE_STORAGE_ACCOUNT_NAME" ]
82- account_key = ENV [ "AZURE_STORAGE_ACCESS_KEY" ]
83-
84- if account_name . blank? || account_key . blank?
85- raise "You must have the correct azure credentials in your ENV"
86- end
87-
88- @blob_client = Azure ::Storage ::Blob ::BlobService . create (
89- storage_account_name : account_name ,
90- storage_access_key : account_key
91- )
80+ Aws ::S3 ::Client . new ( region : 'us-east-2' )
9281end
9382
9483def fetch_file_path ( backup )
95- File . join ( Rails . root , 'tmp' , backup . name )
84+ File . join ( Rails . root , 'tmp' , File . basename ( backup . key ) )
9685end
9786
9887def replace_user_passwords
0 commit comments