FIX: Remap differently when backup comes from multisite

FIX: Remap differently when backup comes from multisite

diff --git a/lib/backup_restore/backuper.rb b/lib/backup_restore/backuper.rb
index c813d87..5ee1f01 100644
--- a/lib/backup_restore/backuper.rb
+++ b/lib/backup_restore/backuper.rb
@@ -127,6 +127,7 @@ module BackupRestore
       BackupMetadata.create!(name: "s3_base_url", value: SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_base_url : nil)
       BackupMetadata.create!(name: "s3_cdn_url", value: SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_cdn_url : nil)
       BackupMetadata.create!(name: "db_name", value: RailsMultisite::ConnectionManagement.current_db)
+      BackupMetadata.create!(name: "multisite", value: Rails.configuration.multisite)
     end
 
     def enable_readonly_mode
diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb
index 9586e4d..75a1c90 100644
--- a/lib/backup_restore/restorer.rb
+++ b/lib/backup_restore/restorer.rb
@@ -458,19 +458,22 @@ module BackupRestore
     def remap_uploads(previous_db_name, current_db_name)
       log "Remapping uploads..."
 
+      was_multisite = BackupMetadata.value_for("multisite") == "true"
+      uploads_folder = was_multisite ? "/" : "/uploads/#{current_db_name}/"
+
       if (old_base_url = BackupMetadata.value_for("base_url")) && old_base_url != Discourse.base_url
         DbHelper.remap(old_base_url, Discourse.base_url)
       end
 
       current_s3_base_url = SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_base_url : nil
       if (old_s3_base_url = BackupMetadata.value_for("s3_base_url")) && old_base_url != current_s3_base_url
-        DbHelper.remap("#{old_s3_base_url}/", "/uploads/#{current_db_name}/")
+        DbHelper.remap("#{old_s3_base_url}/", uploads_folder)
       end
 
       current_s3_cdn_url = SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_cdn_url : nil
       if (old_s3_cdn_url = BackupMetadata.value_for("s3_cdn_url")) && old_s3_cdn_url != current_s3_cdn_url
         base_url = SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_cdn_url : Discourse.base_url
-        DbHelper.remap("#{old_s3_cdn_url}/", UrlHelper.schemaless("#{base_url}/uploads/#{current_db_name}/"))
+        DbHelper.remap("#{old_s3_cdn_url}/", UrlHelper.schemaless("#{base_url}#{uploads_folder}"))
 
         old_host = URI.parse(old_s3_cdn_url).host
         new_host = URI.parse(base_url.presence || Discourse.base_url).host

GitHub sha: 4c1b8c75

I guess we don’t have any tests for this code? It’s getting quite long and involved now.

No, unfortunately we don’t. Believe me, I wish we did. It took me countless backups and restores to finally get this working.

That got me thinking about writing tests that we can run outside of our usual test suite. Something like our smoke tests in order to check if we are able to create and restore backups in various situations.

Maybe next time you jump in there add a test. Doesn’t have to be complete, but some tests are better than none!

2 Likes