rename export/import in favor of backup/restore for better consistency

This commit is contained in:
Régis Hanol 2014-10-10 20:04:07 +02:00
parent bbbec13606
commit 5504622c1b
7 changed files with 50 additions and 48 deletions

View File

@ -1,4 +1,4 @@
require_dependency "backup_restore"
require "backup_restore/backup_restore"
class Admin::BackupsController < Admin::AdminController

View File

@ -1,4 +1,4 @@
require_dependency "backup_restore"
require "backup_restore/backup_restore"
module Jobs
class CreateBackup < Jobs::Scheduled

View File

@ -1383,14 +1383,16 @@ en:
[prefs]: %{user_preferences_url}
export_succeeded:
subject_template: "Export completed successfully"
text_body_template: "The export was successful."
backup_succeeded:
subject_template: "Backup completed successfully"
text_body_template: "The backup was successful.
export_failed:
subject_template: "Export failed"
Visit the [admin > backup section](/admin/backups) to download your new backup."
backup_failed:
subject_template: "Backup failed"
text_body_template: |
The export has failed.
The backup has failed.
Here's the log:

View File

@ -1,5 +1,5 @@
require_dependency "export/exporter"
require_dependency "import/importer"
require "backup_restore/backuper"
require "backup_restore/restorer"
module BackupRestore
@ -10,11 +10,11 @@ module BackupRestore
LOGS_CHANNEL = "/admin/backups/logs"
def self.backup!(user_id, opts={})
start! Export::Exporter.new(user_id, opts)
start! BackupRestore::Backuper.new(user_id, opts)
end
def self.restore!(user_id, filename, publish_to_message_bus=false)
start! Import::Importer.new(user_id, filename, publish_to_message_bus)
start! BackupRestore::Restorer.new(user_id, filename, publish_to_message_bus)
end
def self.rollback!

View File

@ -1,6 +1,6 @@
module Export
module BackupRestore
class Exporter
class Backuper
attr_reader :success
@ -19,7 +19,7 @@ module Export
log "[STARTED]"
log "'#{@user.username}' has started the backup!"
mark_export_as_running
mark_backup_as_running
listen_for_shutdown_signal
@ -95,7 +95,7 @@ module Export
end
end
def mark_export_as_running
def mark_backup_as_running
log "Marking backup as running..."
BackupRestore.mark_as_running!
end
@ -275,9 +275,9 @@ module Export
def notify_user
log "Notifying '#{@user.username}' of the end of the backup..."
if @success
SystemMessage.create_from_system_user(@user, :export_succeeded)
SystemMessage.create_from_system_user(@user, :backup_succeeded)
else
SystemMessage.create_from_system_user(@user, :export_failed, logs: @logs.join("\n"))
SystemMessage.create_from_system_user(@user, :backup_failed, logs: @logs.join("\n"))
end
end
@ -286,7 +286,7 @@ module Export
remove_tmp_directory
unpause_sidekiq
disable_readonly_mode if Discourse.readonly_mode?
mark_export_as_not_running
mark_backup_as_not_running
log "Finished!"
end
@ -310,7 +310,7 @@ module Export
Discourse.disable_readonly_mode
end
def mark_export_as_not_running
def mark_backup_as_not_running
log "Marking backup as finished..."
BackupRestore.mark_as_not_running!
end

View File

@ -1,16 +1,16 @@
module Import
module BackupRestore
class ImportDisabledError < RuntimeError; end
class RestoreDisabledError < RuntimeError; end
class FilenameMissingError < RuntimeError; end
class Importer
class Restorer
attr_reader :success
def initialize(user_id, filename, publish_to_message_bus = false)
@user_id, @filename, @publish_to_message_bus = user_id, filename, publish_to_message_bus
ensure_import_is_enabled
ensure_restore_is_enabled
ensure_no_operation_is_running
ensure_we_have_a_user
ensure_we_have_a_filename
@ -22,7 +22,7 @@ module Import
log "[STARTED]"
log "'#{@user_info[:username]}' has started the restore!"
mark_import_as_running
mark_restore_as_running
listen_for_shutdown_signal
@ -72,8 +72,8 @@ module Import
protected
def ensure_import_is_enabled
raise Import::ImportDisabledError unless Rails.env.development? || SiteSetting.allow_restore?
def ensure_restore_is_enabled
raise Restore::RestoreDisabledError unless Rails.env.development? || SiteSetting.allow_restore?
end
def ensure_no_operation_is_running
@ -88,7 +88,7 @@ module Import
end
def ensure_we_have_a_filename
raise Import::FilenameMissingError if @filename.nil?
raise Restore::FilenameMissingError if @filename.nil?
end
def initialize_state
@ -115,7 +115,7 @@ module Import
end
end
def mark_import_as_running
def mark_restore_as_running
log "Marking restore as running..."
BackupRestore.mark_as_running!
end
@ -174,7 +174,7 @@ module Import
log " Current version: #{@current_version}"
log " Restored version: #{@metadata["version"]}"
error = "You're trying to import a more recent version of the schema. You should migrate first!"
error = "You're trying to restore a more recent version of the schema. You should migrate first!"
raise error if @metadata["version"] > @current_version
end
@ -290,9 +290,9 @@ module Import
if user = User.find_by(email: @user_info[:email])
log "Notifying '#{user.username}' of the end of the restore..."
if @success
SystemMessage.create_from_system_user(user, :import_succeeded)
SystemMessage.create_from_system_user(user, :restore_succeeded)
else
SystemMessage.create_from_system_user(user, :import_failed, logs: @logs.join("\n"))
SystemMessage.create_from_system_user(user, :restore_failed, logs: @logs.join("\n"))
end
else
log "Could not send notification to '#{@user_info[:username]}' (#{@user_info[:email]}), because the user does not exists..."
@ -304,7 +304,7 @@ module Import
remove_tmp_directory
unpause_sidekiq
disable_readonly_mode if Discourse.readonly_mode?
mark_import_as_not_running
mark_restore_as_not_running
log "Finished!"
end
@ -328,7 +328,7 @@ module Import
Discourse.disable_readonly_mode
end
def mark_import_as_not_running
def mark_restore_as_not_running
log "Marking restore as finished..."
BackupRestore.mark_as_not_running!
end

View File

@ -45,21 +45,21 @@ WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%
desc "backup", "Backup a discourse forum"
def backup(filename = nil)
load_rails
require "backup_restore"
require "export/exporter"
require "backup_restore/backup_restore"
require "backup_restore/backuper"
puts "Starting export..."
exporter = Export::Exporter.new(Discourse.system_user.id)
backup = exporter.run
puts "Starting backup..."
backuper = BackupRestore::Backuper.new(Discourse.system_user.id)
backup = backuper.run
if filename.present?
puts "Moving '#{backup}' to '#{filename}'"
FileUtils.mv(backup, filename)
backup = filename
end
puts "Export done."
puts "Backup done."
puts "Output file is in: #{backup}", ""
exit(1) unless exporter.success
exit(1) unless backuper.success
end
desc "export", "Backup a Discourse forum"
@ -70,23 +70,23 @@ WHERE table_schema='public' and (data_type like 'char%' or data_type like 'text%
desc "restore", "Restore a Discourse backup"
def restore(filename)
load_rails
require "backup_restore"
require "import/importer"
require "backup_restore/backup_restore"
require "backup_restore/restorer"
begin
puts "Starting restore: #{filename}"
importer = Import::Importer.new(Discourse.system_user.id, filename)
importer.run
restorer = BackupRestore::Restorer.new(Discourse.system_user.id, filename)
restorer.run
puts 'Restore done.'
rescue Import::FilenameMissingError
rescue BackupRestore::FilenameMissingError
puts '', 'The filename argument was missing.', ''
usage
rescue Import::ImportDisabledError
rescue BackupRestore::RestoreDisabledError
puts '', 'Restores are not allowed.', 'An admin needs to set allow_restore to true in the site settings before restores can be run.', ''
puts 'Restore cancelled.', ''
end
exit(1) unless importer.try(:success)
exit(1) unless restorer.try(:success)
end
desc "import", "Restore a Discourse backup"