1
+ require 'aws-sdk-s3'
2
+
1
3
desc "Update the development db to what is being used in prod"
2
- BACKUP_CONTAINER_NAME = ' backups'
4
+ BUCKET_NAME = "human-essentials- backups"
3
5
PASSWORD_REPLACEMENT = 'password'
4
6
5
7
task :fetch_latest_db do
@@ -13,7 +15,7 @@ task :fetch_latest_db do
13
15
system ( "bin/rails db:environment:set RAILS_ENV=development" )
14
16
system ( "bin/rails db:drop db:create" )
15
17
16
- puts "Restoring the database with #{ backup . name } "
18
+ puts "Restoring the database with #{ backup . key } "
17
19
backup_filepath = fetch_file_path ( backup )
18
20
db_username = ENV [ "PG_USERNAME" ] . presence || ENV [ "USER" ] . presence || "postgres"
19
21
db_host = ENV [ "PG_HOST" ] . presence || "localhost"
51
53
private
52
54
53
55
def fetch_latest_backups
54
- backups = blob_client . list_blobs ( BACKUP_CONTAINER_NAME )
56
+ backups = blob_client . list_objects_v2 ( bucket : BUCKET_NAME )
55
57
56
58
#
57
59
# Retrieve the most up to date version of the DB dump
58
60
#
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 )
61
63
end . reverse . first
62
64
63
65
#
64
66
# Download each of the backups onto the local disk in tmp
65
67
#
66
68
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 )
70
71
71
72
#
72
73
# At this point, the dumps should be stored on the local
73
74
# machine of the user under tmp.
74
75
#
75
- return backup
76
+ backup
76
77
end
77
78
78
79
def 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' )
92
81
end
93
82
94
83
def fetch_file_path ( backup )
95
- File . join ( Rails . root , 'tmp' , backup . name )
84
+ File . join ( Rails . root , 'tmp' , File . basename ( backup . key ) )
96
85
end
97
86
98
87
def replace_user_passwords
0 commit comments