FIX: Do not show backups stored in subfolder of bucket

FIX: Do not show backups stored in subfolder of bucket

diff --git a/lib/backup_restore/s3_backup_store.rb b/lib/backup_restore/s3_backup_store.rb
index 3ee6388..03edc01 100644
--- a/lib/backup_restore/s3_backup_store.rb
+++ b/lib/backup_restore/s3_backup_store.rb
@@ -59,7 +59,7 @@ module BackupRestore
       objects = []
 
       @s3_helper.list.each do |obj|
-        if obj.key.match?(/\.t?gz$/i)
+        if obj.key.match?(file_regex)
           objects << create_file_from_object(obj)
         end
       end
@@ -106,6 +106,19 @@ module BackupRestore
       end
     end
 
+    def file_regex
+      @file_regex ||= begin
+        path = @s3_helper.s3_bucket_folder_path || ""
+
+        if path.present?
+          path = "#{path}/" unless path.end_with?("/")
+          path = Regexp.quote(path)
+        end
+
+        /^#{path}[^\/]*\.t?gz$/i
+      end
+    end
+
     def free_bytes
       nil
     end
diff --git a/spec/lib/backup_restore/local_backup_store_spec.rb b/spec/lib/backup_restore/local_backup_store_spec.rb
index bd5f627..440e0a6 100644
--- a/spec/lib/backup_restore/local_backup_store_spec.rb
+++ b/spec/lib/backup_restore/local_backup_store_spec.rb
@@ -30,6 +30,7 @@ describe BackupRestore::LocalBackupStore do
     create_file(db_name: "default", filename: "a.tgz", last_modified: "2018-02-11T09:27:00Z", size_in_bytes: 29)
     create_file(db_name: "default", filename: "r.sql.gz", last_modified: "2017-12-20T03:48:00Z", size_in_bytes: 11)
     create_file(db_name: "default", filename: "no-backup.txt", last_modified: "2018-09-05T14:27:00Z", size_in_bytes: 12)
+    create_file(db_name: "default/subfolder", filename: "c.tar.gz", last_modified: "2019-01-24T18:44:00Z", size_in_bytes: 23)
 
     create_file(db_name: "second", filename: "multi-2.tar.gz", last_modified: "2018-11-27T03:16:54Z", size_in_bytes: 19)
     create_file(db_name: "second", filename: "multi-1.tar.gz", last_modified: "2018-11-26T03:17:09Z", size_in_bytes: 22)
diff --git a/spec/lib/backup_restore/s3_backup_store_spec.rb b/spec/lib/backup_restore/s3_backup_store_spec.rb
index 3bba9b9..b17f609 100644
--- a/spec/lib/backup_restore/s3_backup_store_spec.rb
+++ b/spec/lib/backup_restore/s3_backup_store_spec.rb
@@ -101,7 +101,7 @@ describe BackupRestore::S3BackupStore do
     prefix = context.params[:prefix]
 
     if prefix.blank?
-      @objects.reject { |obj| obj[:key].include?("/") }
+      @objects.reject { |obj| obj[:key].include?("backups/") }
     else
       @objects.select { |obj| obj[:key].start_with?(prefix) }
     end
@@ -114,9 +114,11 @@ describe BackupRestore::S3BackupStore do
     @objects << { key: "a.tgz", size: 29, last_modified: Time.parse("2018-02-11T09:27:00Z") }
     @objects << { key: "r.sql.gz", size: 11, last_modified: Time.parse("2017-12-20T03:48:00Z") }
     @objects << { key: "no-backup.txt", size: 12, last_modified: Time.parse("2018-09-05T14:27:00Z") }
+    @objects << { key: "subfolder/c.tar.gz", size: 23, last_modified: Time.parse("2019-01-24T18:44:00Z") }
 
     @objects << { key: "backups/second/multi-2.tar.gz", size: 19, last_modified: Time.parse("2018-11-27T03:16:54Z") }
     @objects << { key: "backups/second/multi-1.tar.gz", size: 22, last_modified: Time.parse("2018-11-26T03:17:09Z") }
+    @objects << { key: "backups/second/subfolder/multi-3.tar.gz", size: 23, last_modified: Time.parse("2019-01-24T18:44:00Z") }
   end
 
   def remove_backups

GitHub sha: 45b056b6

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: