Skip to content

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

FEATURE: Add utilities for importing and exporting backups (#32992)

Bianca Nenciu1f2efa795460
diff --git a/lib/backup_restore/backup_file_handler.rb b/lib/backup_restore/backup_file_handler.rb
index 19d79f20e99..57098f99b4d 100644
--- a/lib/backup_restore/backup_file_handler.rb
+++ b/lib/backup_restore/backup_file_handler.rb
@@ -17,13 +17,24 @@ module BackupRestore
def decompress
create_tmp_directory
+
+ if @filename.start_with?("http://", "https://")
+ @url = @filename
+ @filename = File.basename(URI.parse(@url).path)
+ end
+
@archive_path = File.join(@tmp_directory, @filename)
- copy_archive_to_tmp_directory
+ if @url.present?
+ download_archive_to_tmp_directory
+ else
+ copy_archive_to_tmp_directory
+ end
+
decompress_archive
extract_db_dump
- [@tmp_directory, @db_dump_path]
+ [@filename, @tmp_directory, @db_dump_path]
end
def clean_up
@@ -35,6 +46,18 @@ module BackupRestore
log "Something went wrong while removing the following tmp directory: #{@tmp_directory}", ex
end
+ def self.download(url)
+ FileHelper.download(
+ url,
+ max_file_size: Float::INFINITY,
+ tmp_file_name: File.basename(URI.parse(url).path),
+ follow_redirect: true,
+ skip_rate_limit: true,
+ validate_uri: false,
+ verbose: true,
+ )
+ end
+
protected
def create_tmp_directory
@@ -48,6 +71,14 @@ module BackupRestore
FileUtils.mkdir_p(directory)
end
+ def download_archive_to_tmp_directory
+ log "Downloading archive from URL to tmp directory..."
+ tmpfile = self.class.download(@url)
+ Discourse::Utils.execute_command("mv", tmpfile.path, @archive_path)
+ ensure
+ tmpfile&.unlink
+ end
+
def copy_archive_to_tmp_directory
store = BackupRestore::BackupStore.create(location: @store_location)
diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb
index 8d0ae8a7d30..3c99cd86da6 100644
--- a/lib/backup_restore/restorer.rb
+++ b/lib/backup_restore/restorer.rb
@@ -49,7 +49,7 @@ module BackupRestore
@system.listen_for_shutdown_signal
- @tmp_directory, db_dump_path = @backup_file_handler.decompress
+ @filename, @tmp_directory, db_dump_path = @backup_file_handler.decompress
validate_backup_metadata
@system.enable_readonly_mode
diff --git a/script/discourse b/script/discourse
index c1246297acf..95e3dbaed15 100755
--- a/script/discourse
+++ b/script/discourse
@@ -140,6 +140,58 @@ class DiscourseCLI < Thor
end
end
+ desc "backup_url", "Get the URL of a backup file"
+ def backup_url(filename = nil)
+ load_rails
+
+ store = BackupRestore::BackupStore.create
+ raise "Backup URLs are only available for S3 backups" if !store.remote?
+
+ file = store.file(filename, include_download_source: true) if filename
+
+ if !file
+ discourse = File.exist?("/usr/local/bin/discourse") ? "discourse" : "./script/discourse"
+ puts "You must provide a valid filename. Did you mean one of the following?\n\n"
+ store.files.each { |f| puts "#{discourse} backup_url #{f.filename}" }
+ return
+ end
+
+ puts file.source
+ end
+
+ desc "import_backup_url",
+ "Downloads a backup file from a URL and stores it in the current backup store"
+ def import_backup_url(url = nil)
+ load_rails
+
+ store = BackupRestore::BackupStore.create
+ filename = File.basename(URI.parse(url).path)
+
+ file = store.file(filename)
+ if file
+ puts "Backup file already exists in the store"
+ return
+ end
+
+ puts "Downloading backup file from #{url}..."
+ tmpfile = BackupRestore::BackupFileHandler.download(url)
+
+ if store.remote?
+ puts "Uploading backup file to remote store..."
+ content_type = MiniMime.lookup_by_filename(filename).content_type
+ store.upload_file(filename, tmpfile.path, content_type)
+ else
+ puts "Saving backup file to local store..."
+ Discourse::Utils.execute_command(
+ "mv",
+ tmpfile.path,
+ File.join(BackupRestore::LocalBackupStore.base_directory, filename),
+ )
+ end
+ ensure
+ tmpfile&.unlink
+ end
+
desc "export", "Backup a Discourse forum"
option :s3_uploads,
type: :boolean,
diff --git a/spec/lib/backup_restore/backup_file_handler_spec.rb b/spec/lib/backup_restore/backup_file_handler_spec.rb
index 5f649ad06df..0fdafed6efd 100644
--- a/spec/lib/backup_restore/backup_file_handler_spec.rb
+++ b/spec/lib/backup_restore/backup_file_handler_spec.rb
@@ -13,6 +13,15 @@ RSpec.describe BackupRestore::BackupFileHandler do
)
end
+ it "works with URLs" do
+ expect_decompress_and_clean_up_to_work(
+ backup_filename: "backup_since_v1.6.tar.gz",
+ url: "https://example.com/backups/backup_since_v1.6.tar.gz",
+ require_metadata_file: false,
+ require_uploads: true,
+ )
+ end
+
it "works with SQL only backup file" do
expect_decompress_and_clean_up_to_work(
backup_filename: "sql_only_backup.sql.gz",
diff --git a/spec/lib/backup_restore/shared_context_for_backup_restore.rb b/spec/lib/backup_restore/shared_context_for_backup_restore.rb
index 06bf51a4c6d..edff7de3202 100644
--- a/spec/lib/backup_restore/shared_context_for_backup_restore.rb
+++ b/spec/lib/backup_restore/shared_context_for_backup_restore.rb
@@ -65,6 +65,7 @@ RSpec.shared_context "with shared backup restore context" do
def expect_decompress_and_clean_up_to_work(
backup_filename:,
+ url: nil,
expected_dump_filename: "dump.sql",
require_metadata_file:,
require_uploads:,
@@ -74,22 +75,29 @@ RSpec.shared_context "with shared backup restore context" do
freeze_time(DateTime.parse("2019-12-24 14:31:48"))
source_file = File.join(Rails.root, "spec/fixtures/backups", backup_filename)
- target_directory = BackupRestore::LocalBackupStore.base_directory
- target_file = File.join(target_directory, backup_filename)
- FileUtils.copy_file(source_file, target_file)
+ if url
+ stub_request(:get, "https://example.com/backups/backup_since_v1.6.tar.gz").to_return(
+ body: File.new(source_file),
+ status: 200,
+ )
+ else
+ target_directory = BackupRestore::LocalBackupStore.base_directory
+ target_file = File.join(target_directory, backup_filename)
+ FileUtils.copy_file(source_file, target_file)
+ end
Dir.mktmpdir do |root_directory|
current_db = RailsMultisite::ConnectionManagement.current_db
file_handler =
BackupRestore::BackupFileHandler.new(
logger,
- backup_filename,
+ url || backup_filename,
current_db,
root_tmp_directory: root_directory,
location: location,
)
- tmp_directory, db_dump_path = file_handler.decompress
-
+ filename, tmp_directory, db_dump_path = file_handler.decompress
+ expect(filename).to eq(backup_filename)
expected_tmp_path = File.join(root_directory, "tmp/restores", current_db, "2019-12-24-143148")
expect(tmp_directory).to eq(expected_tmp_path)
expect(db_dump_path).to eq(File.join(expected_tmp_path, expected_dump_filename))
@@ -118,14 +126,16 @@ RSpec.shared_context "with shared backup restore context" do
expect(Dir.exist?(tmp_directory)).to eq(false)
end
ensure
- FileUtils.rm(target_file)
-
- # We don't want to delete the directory unless it is empty, otherwise this could be annoying
- # when tests run for the "default" database in a development environment.
- begin
- FileUtils.rmdir(target_directory)
- rescue StandardError
- nil
+ if !url
+ FileUtils.rm(target_file)
+
+ # We don't want to delete the directory unless it is empty, otherwise this could be annoying
+ # when tests run for the "default" database in a development environment.
+ begin
+ FileUtils.rmdir(target_directory)
+ rescue StandardError
+ nil
+ end
end
end
end