Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Migrate uploads to S3 during restore

Gerhard Schlagerf7a2648694d9
diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb
index b7f7cdd1e30..a5ea6a03163 100644
--- a/lib/backup_restore/restorer.rb
+++ b/lib/backup_restore/restorer.rb
@@ -448,19 +448,38 @@ module BackupRestore
DbHelper.remap("uploads/#{previous_db_name}", "uploads/#{current_db_name}")
end
+ if SiteSetting.Upload.enable_s3_uploads
+ migrate_to_s3
+ remove_local_uploads(File.join(public_uploads_path, "uploads/#{current_db_name}"))
+ end
+
generate_optimized_images unless optimized_images_exist
end
end
end
+ def migrate_to_s3
+ log "Migrating uploads to S3..."
+ ENV["SKIP_FAILED"] = "1"
+ ENV["MIGRATE_TO_MULTISITE"] = "1" if Rails.configuration.multisite
+ Rake::Task["uploads:migrate_to_s3"].invoke
+ end
+
+ def remove_local_uploads(directory)
+ log "Removing local uploads directory..."
+ FileUtils.rm_rf(directory) if Dir[directory].present?
+ rescue => ex
+ log "Something went wrong while removing the following uploads directory: #{directory}", ex
+ end
+
def generate_optimized_images
log 'Optimizing site icons...'
+ DB.exec("TRUNCATE TABLE optimized_images")
SiteIconManager.ensure_optimized!
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
diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake
index dcd8c305244..64c1a75f8ec 100644
--- a/lib/tasks/uploads.rake
+++ b/lib/tasks/uploads.rake
@@ -345,6 +345,7 @@ def migrate_to_s3
end
bucket_has_folder_path = true if ENV["DISCOURSE_S3_BUCKET"].include? "/"
+ public_directory = Rails.root.join("public").to_s
opts = {
region: ENV["DISCOURSE_S3_REGION"],
@@ -369,7 +370,7 @@ def migrate_to_s3
print " - Listing local files"
local_files = []
- IO.popen("cd public && find uploads/#{db}/original -type f").each do |file|
+ IO.popen("cd #{public_directory} && find uploads/#{db}/original -type f").each do |file|
local_files << file.chomp
putc "." if local_files.size % 1000 == 0
end
@@ -398,7 +399,7 @@ def migrate_to_s3
skip_etag_verify = ENV["SKIP_ETAG_VERIFY"].present?
local_files.each do |file|
- path = File.join("public", file)
+ path = File.join(public_directory, file)
name = File.basename(path)
etag = Digest::MD5.file(path).hexdigest unless skip_etag_verify
key = file[file.index(prefix)..-1]
@@ -534,7 +535,7 @@ def migrate_to_s3
.where("u.id IS NOT NULL AND u.url LIKE '//%' AND optimized_images.url NOT LIKE '//%'")
.delete_all
- puts "Flagging all posts containing oneboxes for rebake..."
+ puts "Flagging all posts containing lightboxes for rebake..."
count = Post.where("cooked LIKE '%class=\"lightbox\"%'").update_all(baked_version: nil)
puts "#{count} posts were flagged for a rebake"