FEATURE: Setting for excluding optimized images from backups

FEATURE: Setting for excluding optimized images from backups

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 65e0d14..022642e 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1495,6 +1495,7 @@ en:
     backup_with_uploads: "Include uploads in scheduled backups. Disabling this will only backup the database."
     backup_location: "Location where backups are stored. IMPORTANT: S3 requires valid S3 credentials entered in Files settings."
     backup_gzip_compression_level_for_uploads: "Gzip compression level used for compressing uploads."
+    include_thumbnails_in_backups: "Include generated thumbnails in backups. Disabling this will make backups smaller, but requires a rebake of all posts after a restore."
 
     active_user_rate_limit_secs: "How frequently we update the 'last_seen_at' field, in seconds"
     verbose_localization: "Show extended localization tips in the UI"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 5eeb104..2ec91a5 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1534,6 +1534,9 @@ backups:
     min: 1
     max: 9
     shadowed_by_global: true
+  include_thumbnails_in_backups:
+    default: true
+    shadowed_by_global: true
 
 search:
   min_search_term_length:
diff --git a/lib/backup_restore/backuper.rb b/lib/backup_restore/backuper.rb
index 1d3f99f..4b264fe 100644
--- a/lib/backup_restore/backuper.rb
+++ b/lib/backup_restore/backuper.rb
@@ -236,8 +236,10 @@ module BackupRestore
       log "Archiving uploads..."
       FileUtils.cd(File.join(Rails.root, "public")) do
         if File.directory?(upload_directory)
+          exclude_optimized = SiteSetting.include_thumbnails_in_backups ? '' : "--exclude=#{upload_directory}/optimized"
+
           Discourse::Utils.execute_command(
-            'tar', '--append', '--dereference', '--file', tar_filename, upload_directory,
+            'tar', '--append', '--dereference', exclude_optimized, '--file', tar_filename, upload_directory,
             failure_message: "Failed to archive uploads.", success_status_codes: [0, 1]
           )
         else
diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb
index 6c0ee64..30eaa07 100644
--- a/lib/backup_restore/restorer.rb
+++ b/lib/backup_restore/restorer.rb
@@ -425,6 +425,7 @@ module BackupRestore
           tmp_uploads_path = Dir.glob(File.join(@tmp_directory, "uploads", "*")).first
           previous_db_name = File.basename(tmp_uploads_path)
           current_db_name = RailsMultisite::ConnectionManagement.current_db
+          optimized_images_exist = File.exist?(File.join(tmp_uploads_path, 'optimized'))
 
           Discourse::Utils.execute_command(
             'rsync', '-avp', '--safe-links', "#{tmp_uploads_path}/", "uploads/#{current_db_name}/",
@@ -432,12 +433,31 @@ module BackupRestore
           )
 
           if previous_db_name != current_db_name
+            log "Remapping uploads..."
             DbHelper.remap("uploads/#{previous_db_name}", "uploads/#{current_db_name}")
           end
+
+          generate_optimized_images unless optimized_images_exist
         end
       end
     end
 
+    def generate_optimized_images
+      log 'Posts will be rebaked by a background job in sidekiq. You will see missing images until that has completed.'
+      log 'You can expedite the process by manually running "rake posts:rebake_uncooked_posts"'
+
+      DB.exec("TRUNCATE TABLE optimized_images")
+      DB.exec(<<~SQL)
+        UPDATE posts
+        SET baked_version = NULL
+        WHERE id IN (SELECT post_id FROM post_uploads)
+      SQL
+
+      User.where("uploaded_avatar_id IS NOT NULL").find_each do |user|
+        Jobs.enqueue(:create_avatar_thumbnails, upload_id: user.uploaded_avatar_id, user_id: user.id)
+      end
+    end
+
     def rollback
       log "Trying to rollback..."
       if @db_was_changed && BackupRestore.can_rollback?

GitHub sha: b0877193

2 Likes