2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
module BackupRestore
|
|
|
|
|
|
|
|
class OperationRunningError < RuntimeError; end
|
|
|
|
|
2020-01-13 07:12:27 +08:00
|
|
|
VERSION_PREFIX = "v"
|
|
|
|
DUMP_FILE = "dump.sql.gz"
|
2014-02-14 02:41:46 +08:00
|
|
|
LOGS_CHANNEL = "/admin/backups/logs"
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.backup!(user_id, opts = {})
|
2017-12-21 06:00:23 +08:00
|
|
|
if opts[:fork] == false
|
|
|
|
BackupRestore::Backuper.new(user_id, opts).run
|
|
|
|
else
|
|
|
|
start! BackupRestore::Backuper.new(user_id, opts)
|
|
|
|
end
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.restore!(user_id, opts = {})
|
2020-01-13 07:12:27 +08:00
|
|
|
restorer = BackupRestore::Restorer.new(
|
|
|
|
user_id: user_id,
|
|
|
|
filename: opts[:filename],
|
|
|
|
factory: BackupRestore::Factory.new(
|
|
|
|
user_id: user_id,
|
|
|
|
client_id: opts[:client_id]
|
2020-03-03 00:44:01 +08:00
|
|
|
),
|
|
|
|
disable_emails: opts.fetch(:disable_emails, true)
|
2020-01-13 07:12:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
start! restorer
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.rollback!
|
|
|
|
raise BackupRestore::OperationRunningError if BackupRestore.is_operation_running?
|
|
|
|
if can_rollback?
|
2014-02-19 22:25:31 +08:00
|
|
|
move_tables_between_schemas("backup", "public")
|
2014-02-14 01:11:44 +08:00
|
|
|
after_fork
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cancel!
|
|
|
|
set_shutdown_signal!
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.mark_as_running!
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.setex(running_key, 60, "1")
|
2014-02-14 02:41:46 +08:00
|
|
|
save_start_logs_message_id
|
2014-05-13 22:18:08 +08:00
|
|
|
keep_it_running
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_operation_running?
|
2019-12-03 17:05:53 +08:00
|
|
|
!!Discourse.redis.get(running_key)
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.mark_as_not_running!
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.del(running_key)
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.should_shutdown?
|
2019-12-03 17:05:53 +08:00
|
|
|
!!Discourse.redis.get(shutdown_signal_key)
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.can_rollback?
|
|
|
|
backup_tables_count > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.operations_status
|
|
|
|
{
|
|
|
|
is_operation_running: is_operation_running?,
|
|
|
|
can_rollback: can_rollback?,
|
2014-08-29 05:02:26 +08:00
|
|
|
allow_restore: Rails.env.development? || SiteSetting.allow_restore
|
2014-02-13 12:32:58 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-02-14 02:41:46 +08:00
|
|
|
def self.logs
|
|
|
|
id = start_logs_message_id
|
2015-05-04 10:21:00 +08:00
|
|
|
MessageBus.backlog(LOGS_CHANNEL, id).map { |m| m.data }
|
2014-02-14 02:41:46 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def self.current_version
|
|
|
|
ActiveRecord::Migrator.current_version
|
|
|
|
end
|
|
|
|
|
2014-02-19 22:25:31 +08:00
|
|
|
def self.move_tables_between_schemas(source, destination)
|
2020-01-13 07:12:27 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
DB.exec(move_tables_between_schemas_sql(source, destination))
|
|
|
|
end
|
2014-02-19 22:25:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.move_tables_between_schemas_sql(source, destination)
|
2020-01-13 07:12:27 +08:00
|
|
|
<<~SQL
|
2014-02-19 22:25:31 +08:00
|
|
|
DO $$DECLARE row record;
|
|
|
|
BEGIN
|
2014-05-13 22:18:08 +08:00
|
|
|
-- create <destination> schema if it does not exists already
|
|
|
|
-- NOTE: DROP & CREATE SCHEMA is easier, but we don't want to drop the public schema
|
2020-01-13 07:12:27 +08:00
|
|
|
-- otherwise extensions (like hstore & pg_trgm) won't work anymore...
|
2014-07-12 00:29:24 +08:00
|
|
|
CREATE SCHEMA IF NOT EXISTS #{destination};
|
2014-05-13 22:18:08 +08:00
|
|
|
-- move all <source> tables to <destination> schema
|
2014-02-19 22:25:31 +08:00
|
|
|
FOR row IN SELECT tablename FROM pg_tables WHERE schemaname = '#{source}'
|
|
|
|
LOOP
|
2014-02-21 01:42:17 +08:00
|
|
|
EXECUTE 'DROP TABLE IF EXISTS #{destination}.' || quote_ident(row.tablename) || ' CASCADE;';
|
2014-02-19 22:25:31 +08:00
|
|
|
EXECUTE 'ALTER TABLE #{source}.' || quote_ident(row.tablename) || ' SET SCHEMA #{destination};';
|
|
|
|
END LOOP;
|
2014-07-12 00:29:24 +08:00
|
|
|
-- move all <source> views to <destination> schema
|
|
|
|
FOR row IN SELECT viewname FROM pg_views WHERE schemaname = '#{source}'
|
|
|
|
LOOP
|
|
|
|
EXECUTE 'DROP VIEW IF EXISTS #{destination}.' || quote_ident(row.viewname) || ' CASCADE;';
|
|
|
|
EXECUTE 'ALTER VIEW #{source}.' || quote_ident(row.viewname) || ' SET SCHEMA #{destination};';
|
|
|
|
END LOOP;
|
2014-02-19 22:25:31 +08:00
|
|
|
END$$;
|
2014-02-13 12:32:58 +08:00
|
|
|
SQL
|
2014-02-19 22:25:31 +08:00
|
|
|
end
|
|
|
|
|
2014-07-30 23:20:25 +08:00
|
|
|
DatabaseConfiguration = Struct.new(:host, :port, :username, :password, :database)
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2014-02-19 22:25:31 +08:00
|
|
|
def self.database_configuration
|
2016-02-13 06:20:38 +08:00
|
|
|
config = ActiveRecord::Base.connection_pool.spec.config
|
2014-02-21 02:11:43 +08:00
|
|
|
config = config.with_indifferent_access
|
|
|
|
|
2020-01-13 07:12:27 +08:00
|
|
|
# credentials for PostgreSQL in CI environment
|
|
|
|
if Rails.env.test?
|
|
|
|
username = ENV["PGUSER"]
|
|
|
|
password = ENV["PGPASSWORD"]
|
|
|
|
end
|
|
|
|
|
2014-02-21 02:11:43 +08:00
|
|
|
DatabaseConfiguration.new(
|
2018-03-09 10:22:29 +08:00
|
|
|
config["backup_host"] || config["host"],
|
|
|
|
config["backup_port"] || config["port"],
|
2020-01-13 07:12:27 +08:00
|
|
|
config["username"] || username || ENV["USER"] || "postgres",
|
|
|
|
config["password"] || password,
|
2014-02-21 02:11:43 +08:00
|
|
|
config["database"]
|
|
|
|
)
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.running_key
|
|
|
|
"backup_restore_operation_is_running"
|
|
|
|
end
|
|
|
|
|
2014-05-13 22:18:08 +08:00
|
|
|
def self.keep_it_running
|
|
|
|
# extend the expiry by 1 minute every 30 seconds
|
|
|
|
Thread.new do
|
|
|
|
# this thread will be killed when the fork dies
|
|
|
|
while true
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.expire(running_key, 1.minute)
|
2014-05-13 22:18:08 +08:00
|
|
|
sleep 30.seconds
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def self.shutdown_signal_key
|
|
|
|
"backup_restore_operation_should_shutdown"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_shutdown_signal!
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.set(shutdown_signal_key, "1")
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_shutdown_signal!
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.del(shutdown_signal_key)
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2014-02-14 02:41:46 +08:00
|
|
|
def self.save_start_logs_message_id
|
2015-05-04 10:21:00 +08:00
|
|
|
id = MessageBus.last_id(LOGS_CHANNEL)
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.set(start_logs_message_id_key, id)
|
2014-02-14 02:41:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.start_logs_message_id
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.get(start_logs_message_id_key).to_i
|
2014-02-14 02:41:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.start_logs_message_id_key
|
|
|
|
"start_logs_message_id"
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def self.start!(runner)
|
|
|
|
child = fork do
|
|
|
|
begin
|
|
|
|
after_fork
|
|
|
|
runner.run
|
|
|
|
rescue Exception => e
|
|
|
|
puts "--------------------------------------------"
|
|
|
|
puts "---------------- EXCEPTION -----------------"
|
|
|
|
puts e.message
|
|
|
|
puts e.backtrace.join("\n")
|
|
|
|
puts "--------------------------------------------"
|
|
|
|
ensure
|
|
|
|
begin
|
|
|
|
clear_shutdown_signal!
|
|
|
|
rescue Exception => e
|
|
|
|
puts "============================================"
|
|
|
|
puts "================ EXCEPTION ================="
|
|
|
|
puts e.message
|
|
|
|
puts e.backtrace.join("\n")
|
|
|
|
puts "============================================"
|
|
|
|
ensure
|
|
|
|
exit!(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Process.detach(child)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_fork
|
2014-03-28 10:48:14 +08:00
|
|
|
Discourse.after_fork
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.backup_tables_count
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.query_single("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = 'backup'").first.to_i
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|