2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 09:43:31 +08:00
|
|
|
require "mini_mime"
|
2019-07-02 02:38:36 +08:00
|
|
|
require "file_store/s3_store"
|
2018-07-25 20:26:30 +08:00
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
module BackupRestore
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
class Backuper
|
2014-08-04 23:55:09 +08:00
|
|
|
attr_reader :success
|
|
|
|
|
2014-08-21 00:48:56 +08:00
|
|
|
def initialize(user_id, opts = {})
|
|
|
|
@user_id = user_id
|
2015-08-28 02:02:13 +08:00
|
|
|
@client_id = opts[:client_id]
|
2014-08-21 00:48:56 +08:00
|
|
|
@publish_to_message_bus = opts[:publish_to_message_bus] || false
|
2020-07-07 21:39:33 +08:00
|
|
|
@with_uploads = opts[:with_uploads].nil? ? include_uploads? : opts[:with_uploads]
|
2018-10-15 09:43:31 +08:00
|
|
|
@filename_override = opts[:filename]
|
2014-02-13 12:32:58 +08:00
|
|
|
|
|
|
|
ensure_no_operation_is_running
|
|
|
|
ensure_we_have_a_user
|
|
|
|
|
|
|
|
initialize_state
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
log "[STARTED]"
|
|
|
|
log "'#{@user.username}' has started the backup!"
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
mark_backup_as_running
|
2014-02-13 12:32:58 +08:00
|
|
|
|
|
|
|
listen_for_shutdown_signal
|
|
|
|
|
2014-03-14 22:53:58 +08:00
|
|
|
ensure_directory_exists(@tmp_directory)
|
|
|
|
ensure_directory_exists(@archive_directory)
|
|
|
|
|
2019-07-09 19:57:31 +08:00
|
|
|
update_metadata
|
2020-07-28 15:28:57 +08:00
|
|
|
dump_public_schema
|
2019-02-11 21:38:42 +08:00
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Finalizing backup..."
|
|
|
|
|
2016-08-01 21:18:42 +08:00
|
|
|
@with_uploads ? create_archive : move_dump_backup
|
2018-10-15 09:43:31 +08:00
|
|
|
upload_archive
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2014-03-13 04:23:47 +08:00
|
|
|
after_create_hook
|
2014-02-13 12:32:58 +08:00
|
|
|
rescue SystemExit
|
|
|
|
log "Backup process was cancelled!"
|
|
|
|
rescue Exception => ex
|
|
|
|
log "EXCEPTION: " + ex.message
|
|
|
|
log ex.backtrace.join("\n")
|
2016-07-15 11:36:47 +08:00
|
|
|
@success = false
|
2014-02-13 12:32:58 +08:00
|
|
|
else
|
|
|
|
@success = true
|
2018-10-15 09:43:31 +08:00
|
|
|
@backup_filename
|
2014-02-13 12:32:58 +08:00
|
|
|
ensure
|
2018-10-15 09:43:31 +08:00
|
|
|
delete_old
|
2018-09-20 02:35:43 +08:00
|
|
|
clean_up
|
|
|
|
notify_user
|
|
|
|
log "Finished!"
|
2016-07-15 11:36:47 +08:00
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
@success ? log("[SUCCESS]") : log("[FAILED]")
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2019-07-18 07:07:10 +08:00
|
|
|
def get_parameterized_title
|
2019-07-19 05:49:16 +08:00
|
|
|
SiteSetting.title.parameterize.presence || "discourse"
|
2019-07-18 07:07:10 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def initialize_state
|
|
|
|
@success = false
|
2018-10-15 09:43:31 +08:00
|
|
|
@store = BackupRestore::BackupStore.create
|
2014-02-13 12:32:58 +08:00
|
|
|
@current_db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
@timestamp = Time.now.strftime("%Y-%m-%d-%H%M%S")
|
|
|
|
@tmp_directory = File.join(Rails.root, "tmp", "backups", @current_db, @timestamp)
|
2016-08-24 10:28:23 +08:00
|
|
|
@dump_filename = File.join(@tmp_directory, BackupRestore::DUMP_FILE)
|
2018-12-05 10:10:39 +08:00
|
|
|
@archive_directory = BackupRestore::LocalBackupStore.base_directory(db: @current_db)
|
2019-07-18 07:07:10 +08:00
|
|
|
filename = @filename_override || "#{get_parameterized_title}-#{@timestamp}"
|
2018-10-15 09:43:31 +08:00
|
|
|
@archive_basename = File.join(@archive_directory, "#{filename}-#{BackupRestore::VERSION_PREFIX}#{BackupRestore.current_version}")
|
2016-08-01 21:18:42 +08:00
|
|
|
|
|
|
|
@backup_filename =
|
|
|
|
if @with_uploads
|
|
|
|
"#{File.basename(@archive_basename)}.tar.gz"
|
|
|
|
else
|
|
|
|
"#{File.basename(@archive_basename)}.sql.gz"
|
|
|
|
end
|
|
|
|
|
2014-03-25 02:34:16 +08:00
|
|
|
@logs = []
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def listen_for_shutdown_signal
|
2020-10-13 22:41:43 +08:00
|
|
|
BackupRestore.clear_shutdown_signal!
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
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_backup_as_running
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Marking backup as running..."
|
|
|
|
BackupRestore.mark_as_running!
|
|
|
|
end
|
|
|
|
|
2019-07-09 19:57:31 +08:00
|
|
|
def update_metadata
|
|
|
|
log "Updating metadata..."
|
|
|
|
BackupMetadata.delete_all
|
|
|
|
BackupMetadata.create!(name: "base_url", value: Discourse.base_url)
|
|
|
|
BackupMetadata.create!(name: "cdn_url", value: Discourse.asset_host)
|
|
|
|
BackupMetadata.create!(name: "s3_base_url", value: SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_base_url : nil)
|
|
|
|
BackupMetadata.create!(name: "s3_cdn_url", value: SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_cdn_url : nil)
|
|
|
|
BackupMetadata.create!(name: "db_name", value: RailsMultisite::ConnectionManagement.current_db)
|
2019-07-09 22:11:32 +08:00
|
|
|
BackupMetadata.create!(name: "multisite", value: Rails.configuration.multisite)
|
2019-07-09 19:57:31 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def dump_public_schema
|
|
|
|
log "Dumping the public schema of the database..."
|
|
|
|
|
|
|
|
logs = Queue.new
|
|
|
|
pg_dump_running = true
|
|
|
|
|
|
|
|
Thread.new do
|
2014-04-08 01:38:47 +08:00
|
|
|
RailsMultisite::ConnectionManagement::establish_connection(db: @current_db)
|
2014-02-13 12:32:58 +08:00
|
|
|
while pg_dump_running
|
|
|
|
message = logs.pop.strip
|
|
|
|
log(message) unless message.blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
IO.popen("#{pg_dump_command} 2>&1") do |pipe|
|
|
|
|
begin
|
|
|
|
while line = pipe.readline
|
|
|
|
logs << line
|
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
# finished reading...
|
|
|
|
ensure
|
|
|
|
pg_dump_running = false
|
|
|
|
logs << ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raise "pg_dump failed" unless $?.success?
|
|
|
|
end
|
|
|
|
|
2014-03-12 18:45:55 +08:00
|
|
|
def pg_dump_command
|
2014-02-19 22:25:31 +08:00
|
|
|
db_conf = BackupRestore.database_configuration
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2015-03-19 23:39:15 +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?
|
|
|
|
|
|
|
|
[ password_argument, # pass the password to pg_dump (if any)
|
|
|
|
"pg_dump", # the pg_dump command
|
|
|
|
"--schema=public", # only public schema
|
2020-03-31 21:07:52 +08:00
|
|
|
"-T public.pg_*", # exclude tables and views whose name starts with "pg_"
|
2014-02-21 01:42:17 +08:00
|
|
|
"--file='#{@dump_filename}'", # output to the dump.sql file
|
|
|
|
"--no-owner", # do not output commands to set ownership of objects
|
|
|
|
"--no-privileges", # prevent dumping of access privileges
|
|
|
|
"--verbose", # specifies verbose mode
|
2016-07-22 10:45:39 +08:00
|
|
|
"--compress=4", # Compression level of 4
|
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)
|
|
|
|
db_conf.database # the name of the database to dump
|
2014-02-13 12:32:58 +08:00
|
|
|
].join(" ")
|
|
|
|
end
|
|
|
|
|
2016-08-01 21:18:42 +08:00
|
|
|
def move_dump_backup
|
|
|
|
log "Finalizing database dump file: #{@backup_filename}"
|
|
|
|
|
2018-10-15 09:43:31 +08:00
|
|
|
archive_filename = File.join(@archive_directory, @backup_filename)
|
|
|
|
|
2017-03-17 14:21:30 +08:00
|
|
|
Discourse::Utils.execute_command(
|
2018-10-15 09:43:31 +08:00
|
|
|
'mv', @dump_filename, archive_filename,
|
2016-09-16 10:32:53 +08:00
|
|
|
failure_message: "Failed to move database dump file."
|
2016-08-01 21:18:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
remove_tmp_directory
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def create_archive
|
2016-08-01 21:18:42 +08:00
|
|
|
log "Creating archive: #{@backup_filename}"
|
2014-02-13 12:32:58 +08:00
|
|
|
|
|
|
|
tar_filename = "#{@archive_basename}.tar"
|
|
|
|
|
|
|
|
log "Making sure archive does not already exist..."
|
2017-03-17 14:21:30 +08:00
|
|
|
Discourse::Utils.execute_command('rm', '-f', tar_filename)
|
|
|
|
Discourse::Utils.execute_command('rm', '-f', "#{tar_filename}.gz")
|
2014-02-13 12:32:58 +08:00
|
|
|
|
|
|
|
log "Creating empty archive..."
|
2017-03-17 14:21:30 +08:00
|
|
|
Discourse::Utils.execute_command('tar', '--create', '--file', tar_filename, '--files-from', '/dev/null')
|
2014-02-13 12:32:58 +08:00
|
|
|
|
|
|
|
log "Archiving data dump..."
|
2019-11-13 23:52:28 +08:00
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
'tar', '--append', '--dereference', '--file', tar_filename, File.basename(@dump_filename),
|
|
|
|
failure_message: "Failed to archive data dump.",
|
|
|
|
chdir: File.dirname(@dump_filename)
|
|
|
|
)
|
2014-02-13 12:32:58 +08:00
|
|
|
|
2019-07-19 21:13:05 +08:00
|
|
|
add_local_uploads_to_archive(tar_filename)
|
|
|
|
add_remote_uploads_to_archive(tar_filename) if SiteSetting.Upload.enable_s3_uploads
|
2019-07-02 02:38:36 +08:00
|
|
|
|
|
|
|
remove_tmp_directory
|
|
|
|
|
|
|
|
log "Gzipping archive, this may take a while..."
|
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
'gzip', "-#{SiteSetting.backup_gzip_compression_level_for_uploads}", tar_filename,
|
|
|
|
failure_message: "Failed to gzip archive."
|
|
|
|
)
|
|
|
|
end
|
2016-08-01 21:18:42 +08:00
|
|
|
|
2020-07-07 21:39:33 +08:00
|
|
|
def include_uploads?
|
|
|
|
has_local_uploads? || SiteSetting.include_s3_uploads_in_backups
|
|
|
|
end
|
|
|
|
|
|
|
|
def local_uploads_directory
|
|
|
|
@local_uploads_directory ||= File.join(Rails.root, "public", Discourse.store.upload_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_local_uploads?
|
|
|
|
File.directory?(local_uploads_directory) && !Dir.empty?(local_uploads_directory)
|
|
|
|
end
|
|
|
|
|
2019-07-02 02:38:36 +08:00
|
|
|
def add_local_uploads_to_archive(tar_filename)
|
2016-08-01 21:18:42 +08:00
|
|
|
log "Archiving uploads..."
|
2019-07-02 02:38:36 +08:00
|
|
|
|
2020-07-07 21:39:33 +08:00
|
|
|
if has_local_uploads?
|
2020-08-21 21:09:50 +08:00
|
|
|
upload_directory = Discourse.store.upload_path
|
|
|
|
|
2020-07-07 21:39:33 +08:00
|
|
|
if SiteSetting.include_thumbnails_in_backups
|
|
|
|
exclude_optimized = ""
|
|
|
|
else
|
2020-08-21 21:09:50 +08:00
|
|
|
optimized_path = File.join(upload_directory, 'optimized')
|
2020-07-07 21:39:33 +08:00
|
|
|
exclude_optimized = "--exclude=#{optimized_path}"
|
|
|
|
end
|
2019-02-13 18:10:33 +08:00
|
|
|
|
2019-11-13 23:52:28 +08:00
|
|
|
Discourse::Utils.execute_command(
|
2020-08-21 21:09:50 +08:00
|
|
|
'tar', '--append', '--dereference', exclude_optimized, '--file', tar_filename, upload_directory,
|
2019-11-13 23:52:28 +08:00
|
|
|
failure_message: "Failed to archive uploads.", success_status_codes: [0, 1],
|
|
|
|
chdir: File.join(Rails.root, "public")
|
|
|
|
)
|
|
|
|
else
|
|
|
|
log "No local uploads found. Skipping archiving of local uploads..."
|
2016-07-11 15:36:20 +08:00
|
|
|
end
|
2019-07-02 02:38:36 +08:00
|
|
|
end
|
2016-07-11 15:36:20 +08:00
|
|
|
|
2019-07-02 02:38:36 +08:00
|
|
|
def add_remote_uploads_to_archive(tar_filename)
|
|
|
|
if !SiteSetting.include_s3_uploads_in_backups
|
|
|
|
log "Skipping uploads stored on S3."
|
|
|
|
return
|
|
|
|
end
|
2016-07-12 20:23:26 +08:00
|
|
|
|
2019-07-02 02:38:36 +08:00
|
|
|
log "Downloading uploads from S3. This may take a while..."
|
|
|
|
|
|
|
|
store = FileStore::S3Store.new
|
2019-12-18 13:51:57 +08:00
|
|
|
upload_directory = Discourse.store.upload_path
|
2019-07-02 02:38:36 +08:00
|
|
|
count = 0
|
|
|
|
|
2019-11-13 23:52:28 +08:00
|
|
|
Upload.find_each do |upload|
|
|
|
|
next if upload.local?
|
|
|
|
filename = File.join(@tmp_directory, upload_directory, store.get_path_for_upload(upload))
|
2019-07-02 02:38:36 +08:00
|
|
|
|
2019-11-13 23:52:28 +08:00
|
|
|
begin
|
|
|
|
FileUtils.mkdir_p(File.dirname(filename))
|
|
|
|
store.download_file(upload, filename)
|
|
|
|
rescue StandardError => ex
|
|
|
|
log "Failed to download file with upload ID #{upload.id} from S3", ex
|
|
|
|
end
|
2019-07-02 02:38:36 +08:00
|
|
|
|
2019-11-13 23:52:28 +08:00
|
|
|
count += 1
|
|
|
|
log "#{count} files have already been downloaded. Still downloading..." if count % 500 == 0
|
2019-07-02 02:38:36 +08:00
|
|
|
end
|
|
|
|
|
2020-04-04 00:13:34 +08:00
|
|
|
log "Appending uploads to archive..."
|
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
'tar', '--append', '--file', tar_filename, upload_directory,
|
|
|
|
failure_message: "Failed to append uploads to archive.", success_status_codes: [0, 1],
|
|
|
|
chdir: @tmp_directory
|
|
|
|
)
|
|
|
|
|
2019-07-19 21:13:05 +08:00
|
|
|
log "No uploads found on S3. Skipping archiving of uploads stored on S3..." if count == 0
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2018-10-15 09:43:31 +08:00
|
|
|
def upload_archive
|
|
|
|
return unless @store.remote?
|
|
|
|
|
|
|
|
log "Uploading archive..."
|
|
|
|
content_type = MiniMime.lookup_by_filename(@backup_filename).content_type
|
|
|
|
archive_path = File.join(@archive_directory, @backup_filename)
|
|
|
|
@store.upload_file(@backup_filename, archive_path, content_type)
|
|
|
|
end
|
|
|
|
|
2014-03-13 04:23:47 +08:00
|
|
|
def after_create_hook
|
2016-05-23 15:33:29 +08:00
|
|
|
log "Executing the after_create_hook for the backup..."
|
2018-10-15 09:43:31 +08:00
|
|
|
DiscourseEvent.trigger(:backup_created)
|
2014-03-13 04:23:47 +08:00
|
|
|
end
|
|
|
|
|
2018-10-15 09:43:31 +08:00
|
|
|
def delete_old
|
|
|
|
return if Rails.env.development?
|
|
|
|
|
|
|
|
log "Deleting old backups..."
|
|
|
|
@store.delete_old
|
2018-09-20 02:35:43 +08:00
|
|
|
rescue => ex
|
2018-10-15 09:43:31 +08:00
|
|
|
log "Something went wrong while deleting old backups.", ex
|
2014-03-12 05:28:12 +08:00
|
|
|
end
|
|
|
|
|
2014-03-25 02:34:16 +08:00
|
|
|
def notify_user
|
2018-12-20 19:54:02 +08:00
|
|
|
return if @success && @user.id == Discourse::SYSTEM_USER_ID
|
|
|
|
|
2014-03-25 02:34:16 +08:00
|
|
|
log "Notifying '#{@user.username}' of the end of the backup..."
|
2017-03-17 14:21:30 +08:00
|
|
|
status = @success ? :backup_succeeded : :backup_failed
|
|
|
|
|
2018-12-20 19:54:02 +08:00
|
|
|
post = SystemMessage.create_from_system_user(
|
|
|
|
@user, status, logs: Discourse::Utils.pretty_logs(@logs)
|
2017-03-17 14:21:30 +08:00
|
|
|
)
|
2017-06-14 14:00:43 +08:00
|
|
|
|
2018-12-20 19:54:02 +08:00
|
|
|
if @user.id == Discourse::SYSTEM_USER_ID
|
2017-06-14 14:00:43 +08:00
|
|
|
post.topic.invite_group(@user, Group[:admins])
|
|
|
|
end
|
2018-09-20 02:35:43 +08:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while notifying user.", ex
|
2014-03-25 02:34:16 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def clean_up
|
|
|
|
log "Cleaning stuff up..."
|
2019-01-25 03:35:36 +08:00
|
|
|
delete_uploaded_archive
|
2015-02-04 18:49:11 +08:00
|
|
|
remove_tar_leftovers
|
2014-10-11 02:04:07 +08:00
|
|
|
mark_backup_as_not_running
|
2018-07-25 20:26:30 +08:00
|
|
|
refresh_disk_space
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2019-01-25 03:35:36 +08:00
|
|
|
def delete_uploaded_archive
|
|
|
|
return unless @store.remote?
|
|
|
|
|
|
|
|
archive_path = File.join(@archive_directory, @backup_filename)
|
|
|
|
|
|
|
|
if File.exist?(archive_path)
|
|
|
|
log "Removing archive from local storage..."
|
|
|
|
File.delete(archive_path)
|
|
|
|
end
|
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while deleting uploaded archive from local storage.", ex
|
|
|
|
end
|
|
|
|
|
2018-07-25 20:26:30 +08:00
|
|
|
def refresh_disk_space
|
2018-09-20 02:35:43 +08:00
|
|
|
log "Refreshing disk stats..."
|
2018-12-15 06:14:46 +08:00
|
|
|
@store.reset_cache
|
2018-09-20 02:35:43 +08:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while refreshing disk stats.", ex
|
2018-07-25 20:26:30 +08:00
|
|
|
end
|
|
|
|
|
2015-02-04 18:49:11 +08:00
|
|
|
def remove_tar_leftovers
|
|
|
|
log "Removing '.tar' leftovers..."
|
2018-07-04 13:57:22 +08:00
|
|
|
Dir["#{@archive_directory}/*.tar"].each { |filename| File.delete(filename) }
|
2018-09-20 02:35:43 +08:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while removing '.tar' leftovers.", ex
|
2015-02-04 18:49:11 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:32:58 +08:00
|
|
|
def remove_tmp_directory
|
|
|
|
log "Removing tmp '#{@tmp_directory}' directory..."
|
|
|
|
FileUtils.rm_rf(@tmp_directory) if Dir[@tmp_directory].present?
|
2018-09-20 02:35:43 +08:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while removing the following tmp directory: #{@tmp_directory}", ex
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
def mark_backup_as_not_running
|
2014-02-13 12:32:58 +08:00
|
|
|
log "Marking backup as finished..."
|
|
|
|
BackupRestore.mark_as_not_running!
|
2018-09-20 02:35:43 +08:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while marking backup as finished.", ex
|
2014-02-13 12:32:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_directory_exists(directory)
|
|
|
|
log "Making sure '#{directory}' exists..."
|
|
|
|
FileUtils.mkdir_p(directory)
|
|
|
|
end
|
|
|
|
|
2018-09-20 02:35:43 +08:00
|
|
|
def log(message, ex = nil)
|
2015-02-09 23:53:28 +08:00
|
|
|
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
2018-03-28 16:20:08 +08:00
|
|
|
puts(message)
|
|
|
|
publish_log(message, timestamp)
|
2015-02-09 23:53:28 +08:00
|
|
|
save_log(message, timestamp)
|
2018-09-20 02:35:43 +08:00
|
|
|
Rails.logger.error("#{ex}\n" + ex.backtrace.join("\n")) if ex
|
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: "backup", 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
|