DEV: Restoring backups didn't work on macOS

DEV: Restoring backups didn’t work on macOS

This is a follow-up for f51ccea02816c768863b1073977db219f4b66ff3 because specs were failing on macOS. macOS uses bsdtar by default and the --transform option isn’t supported by bsdtar. But the -s option is and it works the same.

diff --git a/lib/backup_restore/backup_file_handler.rb b/lib/backup_restore/backup_file_handler.rb
index e0244b4..8610366 100644
--- a/lib/backup_restore/backup_file_handler.rb
+++ b/lib/backup_restore/backup_file_handler.rb
@@ -64,12 +64,19 @@ module BackupRestore
     def decompress_archive
       return if !@is_archive
 
+      # the transformation is a workaround for a bug which existed between v2.6.0.beta1 and v2.6.0.beta2
+      path_transformation =
+        case tar_implementation
+        when :gnu
+          ['--transform', 's|var/www/discourse/public/uploads/|uploads/|']
+        when :bsd
+          ['-s', '|var/www/discourse/public/uploads/|uploads/|']
+        end
+
       log "Unzipping archive, this may take a while..."
       Discourse::Utils.execute_command(
         'tar', '--extract', '--gzip', '--file', @archive_path, '--directory', @tmp_directory,
-        '--transform', 's|var/www/discourse/public/uploads/|uploads/|',
-        # the transformation is a workaround for a bug which existed between v2.6.0.beta1 and v2.6.0.beta2
-        failure_message: "Failed to decompress archive."
+        *path_transformation, failure_message: "Failed to decompress archive."
       )
     end
 
@@ -95,5 +102,19 @@ module BackupRestore
     def available_size
       SiteSetting.decompressed_backup_max_file_size_mb
     end
+
+    def tar_implementation
+      @tar_version ||= begin
+        tar_version = Discourse::Utils.execute_command('tar', '--version')
+
+        if tar_version.include?("GNU tar")
+          :gnu
+        elsif tar_version.include?("bsdtar")
+          :bsd
+        else
+          raise "Unknown tar implementation: #{tar_version}"
+        end
+      end
+    end
   end
 end

GitHub sha: c0cd69d2

@CvX This should fix it for you. Can you give it a try please?

Works perfectly! Thank you! :tada:

❯ b rspec spec/lib/backup_restore/backup_file_handler_spec.rb

Randomized with seed 53256
....

Finished in 0.2712 seconds (files took 4.3 seconds to load)
4 examples, 0 failures

Randomized with seed 53256
2 Likes