2014-10-11 02:04:07 +08:00
|
|
|
module BackupRestore
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2015-06-11 14:42:01 +08:00
|
|
|
class RestoreDisabledError < RuntimeError; end
|
2014-02-13 12:32:58 +08:00
|
|
|
class FilenameMissingError < RuntimeError; end
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
class Restorer
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2014-08-04 23:55:09 +08:00
|
|
|
attr_reader :success
|
|
|
|
|
2015-08-28 02:02:13 +08:00
|
|
|
def initialize(user_id, opts={})
|
|
|
|
@user_id = user_id
|
|
|
|
@client_id = opts[:client_id]
|
|
|
|
@filename = opts[:filename]
|
|
|
|
@publish_to_message_bus = opts[:publish_to_message_bus] || false
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
ensure_restore_is_enabled
|
2014-02-13 12:32:58 +08:00
|
|
|
ensure_no_operation_is_running
|
|
|
|
ensure_we_have_a_user
|
|
|
|
ensure_we_have_a_filename
|
|
|
|
|
|
|
|
initialize_state
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
log "[STARTED]"
|
|
|
|
log "'#{@user_info[:username]}' has started the restore!"
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
mark_restore_as_running
|
2014-02-13 12:32:58 +08:00
|
|
|
|
|
|
|
listen_for_shutdown_signal
|
|
|
|
|
|
|
|
ensure_directory_exists(@tmp_directory)
|
|
|
|
|
|
|
|
copy_archive_to_tmp_directory
|
|
|
|
unzip_archive
|
|
|
|
|
|
|
|
extract_metadata
|
|
|
|
validate_metadata
|
|
|
|
|
|
|
|
extract_dump
|
|
|
|
restore_dump
|
|
|
|
|
2014-04-09 00:06:53 +08:00
|
|
|
### READ-ONLY / START ###
|
|
|
|
enable_readonly_mode
|
|
|
|
|
|
|
|
pause_sidekiq
|
|
|
|
wait_for_sidekiq
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
switch_schema!
|
|
|
|
|
2016-01-19 08:01:17 +08:00
|
|
|
migrate_database
|
2014-02-13 12:32:58 +08:00
|
|
|
reconnect_database
|
2014-04-09 00:06:53 +08:00
|
|
|
reload_site_settings
|
2015-03-18 00:29:18 +08:00
|
|
|
clear_emoji_cache
|
2014-04-09 00:06:53 +08:00
|
|
|
|
|
|
|
disable_readonly_mode
|
|
|
|
### READ-ONLY / END ###
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2016-01-19 08:01:17 +08:00
|
|
|
extract_uploads
|
2014-02-13 12:32:58 +08:00
|
|
|
rescue SystemExit
|
|
|
|
log "Restore process was cancelled!"
|
|
|
|
rollback
|
2014-08-18 14:42:48 +08:00
|
|
|
rescue => ex
|
2014-02-13 12:32:58 +08:00
|
|
|
log "EXCEPTION: " + ex.message
|
|
|
|
log ex.backtrace.join("\n")
|
|
|
|
rollback
|
|
|
|
else
|
|
|
|
@success = true
|
|
|
|
ensure
|
2014-05-13 22:18:08 +08:00
|
|
|
notify_user rescue nil
|
2014-02-13 12:32:58 +08:00
|
|
|
clean_up
|
|
|
|
@success ? log("[SUCCESS]") : log("[FAILED]")
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
def ensure_restore_is_enabled
|
2015-06-11 14:42:01 +08:00
|
|
|
raise BackupRestore::RestoreDisabledError unless Rails.env.development? || SiteSetting.allow_restore?
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_no_operation_is_running
|
|
|
|
raise BackupRestore::OperationRunningError if BackupRestore.is_operation_running?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_we_have_a_user
|
2014-05-06 21:41:59 +08:00
|
|
|
user = User.find_by(id: @user_id)
|
2014-02-13 12:32:58 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:user_id) unless user
|
|
|
|
# keep some user data around to check them against the newly restored database
|
|
|
|
@user_info = { id: user.id, username: user.username, email: user.email }
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_we_have_a_filename
|
2015-06-11 14:42:01 +08:00
|
|
|
raise BackupRestore::FilenameMissingError if @filename.nil?
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_state
|
|
|
|
@success = false
|
2014-08-20 17:53:15 +08:00
|
|
|
@db_was_changed = false
|
2014-02-13 12:32:58 +08:00
|
|
|
@current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
@current_version = BackupRestore.current_version
|
|
|
|
@timestamp = Time.now.strftime("%Y-%m-%d-%H%M%S")
|
|
|
|
@tmp_directory = File.join(Rails.root, "tmp", "restores", @current_db, @timestamp)
|
|
|
|
@archive_filename = File.join(@tmp_directory, @filename)
|
|
|
|
@tar_filename = @archive_filename[0...-3]
|
|
|
|
@meta_filename = File.join(@tmp_directory, BackupRestore::METADATA_FILE)
|
|
|
|
@dump_filename = File.join(@tmp_directory, BackupRestore::DUMP_FILE)
|
2014-03-25 12:15:30 +08:00
|
|
|
@logs = []
|
2014-03-28 19:15:53 +08:00
|
|
|
@readonly_mode_was_enabled = Discourse.readonly_mode?
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def listen_for_shutdown_signal
|
|
|
|
Thread.new do
|
|
|
|
while BackupRestore.is_operation_running?
|
|
|
|
exit if BackupRestore.should_shutdown?
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
def mark_restore_as_running
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Marking restore as running..."
|
|
|
|
BackupRestore.mark_as_running!
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_readonly_mode
|
2014-03-28 19:15:53 +08:00
|
|
|
return if @readonly_mode_was_enabled
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Enabling readonly mode..."
|
|
|
|
Discourse.enable_readonly_mode
|
|
|
|
end
|
|
|
|
|
|
|
|
def pause_sidekiq
|
|
|
|
log "Pausing sidekiq..."
|
|
|
|
Sidekiq.pause!
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_sidekiq
|
|
|
|
log "Waiting for sidekiq to finish running jobs..."
|
2014-03-14 22:49:35 +08:00
|
|
|
iterations = 1
|
|
|
|
while sidekiq_has_running_jobs?
|
|
|
|
log "Waiting for sidekiq to finish running jobs... ##{iterations}"
|
2014-02-13 12:32:58 +08:00
|
|
|
sleep 5
|
|
|
|
iterations += 1
|
2014-03-14 22:49:35 +08:00
|
|
|
raise "Sidekiq did not finish running all the jobs in the allowed time!" if iterations > 6
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-14 22:49:35 +08:00
|
|
|
def sidekiq_has_running_jobs?
|
2014-08-15 05:54:55 +08:00
|
|
|
Sidekiq::Workers.new.each do |_, _, worker|
|
2014-03-14 22:49:35 +08:00
|
|
|
payload = worker.try(:payload)
|
|
|
|
return true if payload.try(:all_sites)
|
|
|
|
return true if payload.try(:current_site_id) == @current_db
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def copy_archive_to_tmp_directory
|
|
|
|
log "Copying archive to tmp directory..."
|
|
|
|
source = File.join(Backup.base_directory, @filename)
|
2016-04-04 23:24:02 +08:00
|
|
|
`cp '#{source}' '#{@archive_filename}'`
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unzip_archive
|
2016-05-23 15:33:29 +08:00
|
|
|
log "Unzipping archive, this may take a while..."
|
2016-04-04 23:24:02 +08:00
|
|
|
FileUtils.cd(@tmp_directory) { `gzip --decompress '#{@archive_filename}'` }
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def extract_metadata
|
|
|
|
log "Extracting metadata file..."
|
2016-04-04 23:24:02 +08:00
|
|
|
FileUtils.cd(@tmp_directory) { `tar --extract --file '#{@tar_filename}' #{BackupRestore::METADATA_FILE}` }
|
2014-02-13 12:32:58 +08:00
|
|
|
@metadata = Oj.load_file(@meta_filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_metadata
|
|
|
|
log "Validating metadata..."
|
|
|
|
log " Current version: #{@current_version}"
|
|
|
|
log " Restored version: #{@metadata["version"]}"
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
error = "You're trying to restore a more recent version of the schema. You should migrate first!"
|
2014-02-13 12:32:58 +08:00
|
|
|
raise error if @metadata["version"] > @current_version
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_dump
|
|
|
|
log "Extracting dump file..."
|
2016-04-04 23:24:02 +08:00
|
|
|
FileUtils.cd(@tmp_directory) { `tar --extract --file '#{@tar_filename}' #{BackupRestore::DUMP_FILE}` }
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore_dump
|
|
|
|
log "Restoring dump file... (can be quite long)"
|
|
|
|
|
|
|
|
logs = Queue.new
|
|
|
|
psql_running = true
|
|
|
|
has_error = false
|
|
|
|
|
|
|
|
Thread.new do
|
2014-04-09 00:06:53 +08:00
|
|
|
RailsMultisite::ConnectionManagement::establish_connection(db: @current_db)
|
2014-02-13 12:32:58 +08:00
|
|
|
while psql_running
|
|
|
|
message = logs.pop.strip
|
|
|
|
has_error ||= (message =~ /ERROR:/)
|
|
|
|
log(message) unless message.blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
IO.popen("#{psql_command} 2>&1") do |pipe|
|
|
|
|
begin
|
|
|
|
while line = pipe.readline
|
|
|
|
logs << line
|
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
# finished reading...
|
|
|
|
ensure
|
|
|
|
psql_running = false
|
|
|
|
logs << ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# psql does not return a valid exit code when an error happens
|
|
|
|
raise "psql failed" if has_error
|
|
|
|
end
|
|
|
|
|
2014-03-12 18:45:55 +08:00
|
|
|
def psql_command
|
2014-02-19 22:25:31 +08:00
|
|
|
db_conf = BackupRestore.database_configuration
|
|
|
|
|
2014-02-19 22:43:59 +08:00
|
|
|
password_argument = "PGPASSWORD=#{db_conf.password}" if db_conf.password.present?
|
2014-02-21 01:42:17 +08:00
|
|
|
host_argument = "--host=#{db_conf.host}" if db_conf.host.present?
|
2014-07-30 23:20:25 +08:00
|
|
|
port_argument = "--port=#{db_conf.port}" if db_conf.port.present?
|
2014-02-21 01:42:17 +08:00
|
|
|
username_argument = "--username=#{db_conf.username}" if db_conf.username.present?
|
2014-02-19 22:25:31 +08:00
|
|
|
|
2014-02-21 01:42:17 +08:00
|
|
|
[ password_argument, # pass the password to psql (if any)
|
2014-02-19 22:25:31 +08:00
|
|
|
"psql", # the psql command
|
|
|
|
"--dbname='#{db_conf.database}'", # connect to database *dbname*
|
|
|
|
"--file='#{@dump_filename}'", # read the dump
|
|
|
|
"--single-transaction", # all or nothing (also runs COPY commands faster)
|
2014-02-21 01:42:17 +08:00
|
|
|
host_argument, # the hostname to connect to (if any)
|
2014-07-30 23:20:25 +08:00
|
|
|
port_argument, # the port to connect to (if any)
|
2014-02-21 01:42:17 +08:00
|
|
|
username_argument # the username to connect as (if any)
|
2014-02-13 12:32:58 +08:00
|
|
|
].join(" ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def switch_schema!
|
2015-03-09 23:11:15 +08:00
|
|
|
log "Switching schemas... try reloading the site in 5 minutes, if successful, then reboot and restore is complete."
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2014-02-19 22:25:31 +08:00
|
|
|
sql = [
|
|
|
|
"BEGIN;",
|
|
|
|
BackupRestore.move_tables_between_schemas_sql("public", "backup"),
|
|
|
|
BackupRestore.move_tables_between_schemas_sql("restore", "public"),
|
|
|
|
"COMMIT;"
|
|
|
|
].join("\n")
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2014-08-20 17:53:15 +08:00
|
|
|
@db_was_changed = true
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
User.exec_sql(sql)
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_database
|
|
|
|
log "Migrating the database..."
|
|
|
|
Discourse::Application.load_tasks
|
|
|
|
ENV["VERSION"] = @current_version.to_s
|
2014-02-21 23:17:00 +08:00
|
|
|
Rake::Task["db:migrate"].invoke
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def reconnect_database
|
|
|
|
log "Reconnecting to the database..."
|
2014-04-09 00:06:53 +08:00
|
|
|
RailsMultisite::ConnectionManagement::establish_connection(db: @current_db)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload_site_settings
|
|
|
|
log "Reloading site settings..."
|
|
|
|
SiteSetting.refresh!
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2015-03-18 00:29:18 +08:00
|
|
|
def clear_emoji_cache
|
|
|
|
log "Clearing emoji cache..."
|
|
|
|
Emoji.clear_cache
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def extract_uploads
|
2016-04-04 23:24:02 +08:00
|
|
|
if `tar --list --file '#{@tar_filename}' | grep 'uploads/'`.present?
|
2014-12-23 08:12:26 +08:00
|
|
|
log "Extracting uploads..."
|
2014-02-13 12:32:58 +08:00
|
|
|
FileUtils.cd(File.join(Rails.root, "public")) do
|
2016-04-04 23:24:02 +08:00
|
|
|
`tar --extract --keep-newer-files --file '#{@tar_filename}' uploads/`
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rollback
|
|
|
|
log "Trying to rollback..."
|
2014-08-20 17:53:15 +08:00
|
|
|
if @db_was_changed && BackupRestore.can_rollback?
|
2014-02-14 07:27:25 +08:00
|
|
|
log "Rolling back..."
|
2014-02-19 22:25:31 +08:00
|
|
|
BackupRestore.move_tables_between_schemas("backup", "public")
|
2014-02-13 12:32:58 +08:00
|
|
|
else
|
2014-02-14 07:27:25 +08:00
|
|
|
log "There was no need to rollback"
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-25 02:34:16 +08:00
|
|
|
def notify_user
|
2014-05-06 21:41:59 +08:00
|
|
|
if user = User.find_by(email: @user_info[:email])
|
2014-03-25 02:34:16 +08:00
|
|
|
log "Notifying '#{user.username}' of the end of the restore..."
|
|
|
|
if @success
|
2014-10-11 02:04:07 +08:00
|
|
|
SystemMessage.create_from_system_user(user, :restore_succeeded)
|
2014-03-25 02:34:16 +08:00
|
|
|
else
|
2014-10-11 02:04:07 +08:00
|
|
|
SystemMessage.create_from_system_user(user, :restore_failed, logs: @logs.join("\n"))
|
2014-03-25 02:34:16 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log "Could not send notification to '#{@user_info[:username]}' (#{@user_info[:email]}), because the user does not exists..."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def clean_up
|
|
|
|
log "Cleaning stuff up..."
|
|
|
|
remove_tmp_directory
|
|
|
|
unpause_sidekiq
|
2014-04-09 00:06:53 +08:00
|
|
|
disable_readonly_mode if Discourse.readonly_mode?
|
2014-10-11 02:04:07 +08:00
|
|
|
mark_restore_as_not_running
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Finished!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_tmp_directory
|
|
|
|
log "Removing tmp '#{@tmp_directory}' directory..."
|
|
|
|
FileUtils.rm_rf(@tmp_directory) if Dir[@tmp_directory].present?
|
|
|
|
rescue
|
|
|
|
log "Something went wrong while removing the following tmp directory: #{@tmp_directory}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpause_sidekiq
|
|
|
|
log "Unpausing sidekiq..."
|
|
|
|
Sidekiq.unpause!
|
2014-05-13 22:18:08 +08:00
|
|
|
rescue
|
|
|
|
log "Something went wrong while unpausing Sidekiq."
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def disable_readonly_mode
|
2014-03-28 19:15:53 +08:00
|
|
|
return if @readonly_mode_was_enabled
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Disabling readonly mode..."
|
|
|
|
Discourse.disable_readonly_mode
|
|
|
|
end
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
def mark_restore_as_not_running
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Marking restore as finished..."
|
|
|
|
BackupRestore.mark_as_not_running!
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_directory_exists(directory)
|
|
|
|
log "Making sure #{directory} exists..."
|
|
|
|
FileUtils.mkdir_p(directory)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log(message)
|
2015-02-09 23:53:28 +08:00
|
|
|
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
2014-02-13 12:32:58 +08:00
|
|
|
puts(message) rescue nil
|
2015-02-09 23:53:28 +08:00
|
|
|
publish_log(message, timestamp) rescue nil
|
|
|
|
save_log(message, timestamp)
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2015-02-09 23:53:28 +08:00
|
|
|
def publish_log(message, timestamp)
|
2014-02-13 12:32:58 +08:00
|
|
|
return unless @publish_to_message_bus
|
2015-02-09 23:53:28 +08:00
|
|
|
data = { timestamp: timestamp, operation: "restore", message: message }
|
2015-08-28 02:02:13 +08:00
|
|
|
MessageBus.publish(BackupRestore::LOGS_CHANNEL, data, user_ids: [@user_id], client_ids: [@client_id])
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2015-02-09 23:53:28 +08:00
|
|
|
def save_log(message, timestamp)
|
|
|
|
@logs << "[#{timestamp}] #{message}"
|
2014-03-25 02:34:16 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|