Class: S3Backup::S3
- Inherits:
-
Object
- Object
- S3Backup::S3
- Defined in:
- lib/s3_backup/s3.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #clean!(base_name, bucket_path) ⇒ Object
- #download!(database_name, bucket_path, file_path) ⇒ Object
-
#initialize ⇒ S3
constructor
A new instance of S3.
- #upload!(file_name, bucket_path, file_path) ⇒ Object
Constructor Details
#initialize ⇒ S3
Returns a new instance of S3.
10 11 12 13 14 15 16 17 |
# File 'lib/s3_backup/s3.rb', line 10 def initialize @connection = Fog::Storage.new( provider: 'AWS', aws_access_key_id: Config.aws_access_key_id, aws_secret_access_key: Config.aws_secret_access_key, region: Config.aws_region ) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/s3_backup/s3.rb', line 8 def connection @connection end |
Instance Method Details
#clean!(base_name, bucket_path) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/s3_backup/s3.rb', line 52 def clean!(base_name, bucket_path) prefix = File.join(bucket_path, base_name) directory = connection.directories.get(Config.bucket, prefix: prefix) s3_files = directory.files.sort_by(&:last_modified).reverse files_to_remove = s3_files[Config.s3_keep..-1] return true if files_to_remove.blank? files_to_remove.each(&:destroy) true end |
#download!(database_name, bucket_path, file_path) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/s3_backup/s3.rb', line 31 def download!(database_name, bucket_path, file_path) prefix = File.join(bucket_path, database_name) directory = connection.directories.get(Config.bucket, prefix: prefix) s3_backup_file = directory.files.sort_by(&:last_modified).reverse.first raise "#{database_name} file not found on s3" unless s3_backup_file file = File.open(file_path, 'wb') puts "File size: #{s3_backup_file.content_length / 1024 / 1024}MB, writing to #{file_path}" directory.files.get(s3_backup_file.key) do |chunk, remaining_bytes, total_bytes| (total_bytes, remaining_bytes) file.write chunk end file.close true end |
#upload!(file_name, bucket_path, file_path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/s3_backup/s3.rb', line 19 def upload!(file_name, bucket_path, file_path) directory = @connection.directories.get(Config.bucket) directory.files.create( key: File.join(bucket_path, file_name), body: File.open(file_path), public: false ) true end |