2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
require "mini_mime"
|
2019-07-01 14:38:36 -04:00
|
|
|
require "file_store/s3_store"
|
2018-07-25 08:26:30 -04:00
|
|
|
|
2014-10-10 14:04:07 -04:00
|
|
|
module BackupRestore
|
|
|
|
class Backuper
|
2023-03-14 12:07:18 -04:00
|
|
|
attr_reader :success, :store
|
2014-08-04 11:55:09 -04:00
|
|
|
|
2014-08-20 12:48:56 -04:00
|
|
|
def initialize(user_id, opts = {})
|
|
|
|
@user_id = user_id
|
2015-08-27 14:02:13 -04:00
|
|
|
@client_id = opts[:client_id]
|
2014-08-20 12:48:56 -04:00
|
|
|
@publish_to_message_bus = opts[:publish_to_message_bus] || false
|
2023-02-08 15:40:15 -05:00
|
|
|
@with_uploads = opts[:with_uploads] == false ? false : include_uploads?
|
2018-10-14 21:43:31 -04:00
|
|
|
@filename_override = opts[:filename]
|
2022-08-17 08:33:23 -04:00
|
|
|
@ticket = opts[:ticket]
|
2014-02-12 23:32:58 -05:00
|
|
|
|
|
|
|
initialize_state
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2022-08-17 08:33:23 -04:00
|
|
|
ensure_no_operation_is_running
|
|
|
|
ensure_we_have_a_user
|
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
log "[STARTED]"
|
|
|
|
log "'#{@user.username}' has started the backup!"
|
|
|
|
|
2014-10-10 14:04:07 -04:00
|
|
|
mark_backup_as_running
|
2014-02-12 23:32:58 -05:00
|
|
|
|
|
|
|
listen_for_shutdown_signal
|
|
|
|
|
2014-03-14 10:53:58 -04:00
|
|
|
ensure_directory_exists(@tmp_directory)
|
|
|
|
ensure_directory_exists(@archive_directory)
|
|
|
|
|
2019-07-09 07:57:31 -04:00
|
|
|
update_metadata
|
2020-07-28 03:28:57 -04:00
|
|
|
dump_public_schema
|
2019-02-11 08:38:42 -05:00
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
log "Finalizing backup..."
|
|
|
|
|
2016-08-01 09:18:42 -04:00
|
|
|
@with_uploads ? create_archive : move_dump_backup
|
2018-10-14 21:43:31 -04:00
|
|
|
upload_archive
|
2014-02-12 23:32:58 -05:00
|
|
|
|
2014-03-12 16:23:47 -04:00
|
|
|
after_create_hook
|
2014-02-12 23:32:58 -05:00
|
|
|
rescue SystemExit
|
|
|
|
log "Backup process was cancelled!"
|
|
|
|
rescue Exception => ex
|
|
|
|
log "EXCEPTION: " + ex.message
|
|
|
|
log ex.backtrace.join("\n")
|
|
|
|
else
|
|
|
|
@success = true
|
2018-10-14 21:43:31 -04:00
|
|
|
@backup_filename
|
2014-02-12 23:32:58 -05:00
|
|
|
ensure
|
2018-10-14 21:43:31 -04:00
|
|
|
delete_old
|
2018-09-19 14:35:43 -04:00
|
|
|
clean_up
|
|
|
|
notify_user
|
|
|
|
log "Finished!"
|
2023-03-14 12:07:18 -04:00
|
|
|
publish_completion
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
2024-01-08 15:41:00 -05:00
|
|
|
def delete_prior_to_n_days
|
|
|
|
return if Rails.env.development?
|
|
|
|
store.delete_prior_to_n_days
|
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while deleting backups prior to n days....", ex
|
|
|
|
end
|
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def ensure_no_operation_is_running
|
|
|
|
raise BackupRestore::OperationRunningError if BackupRestore.is_operation_running?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_we_have_a_user
|
|
|
|
raise Discourse::InvalidParameters.new(:user_id) unless @user
|
|
|
|
end
|
|
|
|
|
2019-07-17 19:07:10 -04:00
|
|
|
def get_parameterized_title
|
2019-07-18 17:49:16 -04:00
|
|
|
SiteSetting.title.parameterize.presence || "discourse"
|
2019-07-17 19:07:10 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
def initialize_state
|
|
|
|
@success = false
|
2022-08-17 08:33:23 -04:00
|
|
|
@user = User.find_by(id: @user_id)
|
|
|
|
@logs = []
|
2018-10-14 21:43:31 -04:00
|
|
|
@store = BackupRestore::BackupStore.create
|
2014-02-12 23:32:58 -05: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-23 22:28:23 -04:00
|
|
|
@dump_filename = File.join(@tmp_directory, BackupRestore::DUMP_FILE)
|
2018-12-04 21:10:39 -05:00
|
|
|
@archive_directory = BackupRestore::LocalBackupStore.base_directory(db: @current_db)
|
2019-07-17 19:07:10 -04:00
|
|
|
filename = @filename_override || "#{get_parameterized_title}-#{@timestamp}"
|
2018-10-14 21:43:31 -04:00
|
|
|
@archive_basename =
|
|
|
|
File.join(
|
|
|
|
@archive_directory,
|
|
|
|
"#{filename}-#{BackupRestore::VERSION_PREFIX}#{BackupRestore.current_version}",
|
|
|
|
)
|
2016-08-01 09:18:42 -04:00
|
|
|
|
|
|
|
@backup_filename =
|
|
|
|
if @with_uploads
|
|
|
|
"#{File.basename(@archive_basename)}.tar.gz"
|
|
|
|
else
|
|
|
|
"#{File.basename(@archive_basename)}.sql.gz"
|
|
|
|
end
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def listen_for_shutdown_signal
|
2020-10-13 10:41:43 -04:00
|
|
|
BackupRestore.clear_shutdown_signal!
|
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
Thread.new do
|
2022-08-23 19:43:42 -04:00
|
|
|
Thread.current.name = "shutdown_wait"
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.with_connection(@current_db) do
|
|
|
|
while BackupRestore.is_operation_running?
|
|
|
|
exit if BackupRestore.should_shutdown?
|
|
|
|
sleep 0.1
|
|
|
|
end
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-10 14:04:07 -04:00
|
|
|
def mark_backup_as_running
|
2014-02-12 23:32:58 -05:00
|
|
|
log "Marking backup as running..."
|
|
|
|
BackupRestore.mark_as_running!
|
|
|
|
end
|
|
|
|
|
2019-07-09 07:57:31 -04: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 10:11:32 -04:00
|
|
|
BackupMetadata.create!(name: "multisite", value: Rails.configuration.multisite)
|
2019-07-09 07:57:31 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:32:58 -05: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-07 13:38:47 -04:00
|
|
|
RailsMultisite::ConnectionManagement.establish_connection(db: @current_db)
|
2014-02-12 23:32:58 -05:00
|
|
|
while pg_dump_running
|
|
|
|
message = logs.pop.strip
|
2024-05-27 06:27:13 -04:00
|
|
|
log(message) if message.present?
|
2014-02-12 23:32:58 -05:00
|
|
|
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 06:45:55 -04:00
|
|
|
def pg_dump_command
|
2014-02-19 09:25:31 -05:00
|
|
|
db_conf = BackupRestore.database_configuration
|
2014-02-12 23:32:58 -05:00
|
|
|
|
2015-03-19 11:39:15 -04:00
|
|
|
password_argument = "PGPASSWORD='#{db_conf.password}'" if db_conf.password.present?
|
2014-02-20 12:42:17 -05:00
|
|
|
host_argument = "--host=#{db_conf.host}" if db_conf.host.present?
|
2014-07-30 11:20:25 -04:00
|
|
|
port_argument = "--port=#{db_conf.port}" if db_conf.port.present?
|
2014-02-20 12:42:17 -05: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 09:07:52 -04:00
|
|
|
"-T public.pg_*", # exclude tables and views whose name starts with "pg_"
|
2014-02-20 12:42:17 -05: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-21 22:45:39 -04:00
|
|
|
"--compress=4", # Compression level of 4
|
2014-02-20 12:42:17 -05:00
|
|
|
host_argument, # the hostname to connect to (if any)
|
2014-07-30 11:20:25 -04:00
|
|
|
port_argument, # the port to connect to (if any)
|
2014-02-20 12:42:17 -05:00
|
|
|
username_argument, # the username to connect as (if any)
|
|
|
|
db_conf.database, # the name of the database to dump
|
2014-02-12 23:32:58 -05:00
|
|
|
].join(" ")
|
|
|
|
end
|
|
|
|
|
2016-08-01 09:18:42 -04:00
|
|
|
def move_dump_backup
|
|
|
|
log "Finalizing database dump file: #{@backup_filename}"
|
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
archive_filename = File.join(@archive_directory, @backup_filename)
|
|
|
|
|
2017-03-17 02:21:30 -04:00
|
|
|
Discourse::Utils.execute_command(
|
2018-10-14 21:43:31 -04:00
|
|
|
"mv",
|
|
|
|
@dump_filename,
|
|
|
|
archive_filename,
|
2016-09-15 22:32:53 -04:00
|
|
|
failure_message: "Failed to move database dump file.",
|
2016-08-01 09:18:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
remove_tmp_directory
|
|
|
|
end
|
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
def create_archive
|
2016-08-01 09:18:42 -04:00
|
|
|
log "Creating archive: #{@backup_filename}"
|
2014-02-12 23:32:58 -05:00
|
|
|
|
|
|
|
tar_filename = "#{@archive_basename}.tar"
|
|
|
|
|
|
|
|
log "Making sure archive does not already exist..."
|
2017-03-17 02:21:30 -04:00
|
|
|
Discourse::Utils.execute_command("rm", "-f", tar_filename)
|
|
|
|
Discourse::Utils.execute_command("rm", "-f", "#{tar_filename}.gz")
|
2014-02-12 23:32:58 -05:00
|
|
|
|
|
|
|
log "Creating empty archive..."
|
2017-03-17 02:21:30 -04:00
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
"tar",
|
|
|
|
"--create",
|
|
|
|
"--file",
|
|
|
|
tar_filename,
|
|
|
|
"--files-from",
|
|
|
|
"/dev/null",
|
|
|
|
)
|
2014-02-12 23:32:58 -05:00
|
|
|
|
|
|
|
log "Archiving data dump..."
|
2019-11-13 10:52:28 -05: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-12 23:32:58 -05:00
|
|
|
|
2019-07-19 09:13:05 -04:00
|
|
|
add_local_uploads_to_archive(tar_filename)
|
|
|
|
add_remote_uploads_to_archive(tar_filename) if SiteSetting.Upload.enable_s3_uploads
|
2019-07-01 14:38:36 -04: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 09:18:42 -04:00
|
|
|
|
2020-07-07 09:39:33 -04: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-01 14:38:36 -04:00
|
|
|
def add_local_uploads_to_archive(tar_filename)
|
2016-08-01 09:18:42 -04:00
|
|
|
log "Archiving uploads..."
|
2019-07-01 14:38:36 -04:00
|
|
|
|
2020-07-07 09:39:33 -04:00
|
|
|
if has_local_uploads?
|
2020-08-21 09:09:50 -04:00
|
|
|
upload_directory = Discourse.store.upload_path
|
|
|
|
|
2020-07-07 09:39:33 -04:00
|
|
|
if SiteSetting.include_thumbnails_in_backups
|
|
|
|
exclude_optimized = ""
|
|
|
|
else
|
2020-08-21 09:09:50 -04:00
|
|
|
optimized_path = File.join(upload_directory, "optimized")
|
2020-07-07 09:39:33 -04:00
|
|
|
exclude_optimized = "--exclude=#{optimized_path}"
|
|
|
|
end
|
2019-02-13 05:10:33 -05:00
|
|
|
|
2019-11-13 10:52:28 -05:00
|
|
|
Discourse::Utils.execute_command(
|
2020-08-21 09:09:50 -04:00
|
|
|
"tar",
|
|
|
|
"--append",
|
|
|
|
"--dereference",
|
|
|
|
exclude_optimized,
|
|
|
|
"--file",
|
|
|
|
tar_filename,
|
|
|
|
upload_directory,
|
2019-11-13 10:52:28 -05: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 03:36:20 -04:00
|
|
|
end
|
2019-07-01 14:38:36 -04:00
|
|
|
end
|
2016-07-11 03:36:20 -04:00
|
|
|
|
2019-07-01 14:38:36 -04: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 08:23:26 -04:00
|
|
|
|
2019-07-01 14:38:36 -04:00
|
|
|
log "Downloading uploads from S3. This may take a while..."
|
|
|
|
|
|
|
|
store = FileStore::S3Store.new
|
2019-12-18 00:51:57 -05:00
|
|
|
upload_directory = Discourse.store.upload_path
|
2019-07-01 14:38:36 -04:00
|
|
|
count = 0
|
|
|
|
|
2019-11-13 10:52:28 -05: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-01 14:38:36 -04:00
|
|
|
|
2019-11-13 10:52:28 -05: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-01 14:38:36 -04:00
|
|
|
|
2019-11-13 10:52:28 -05:00
|
|
|
count += 1
|
|
|
|
log "#{count} files have already been downloaded. Still downloading..." if count % 500 == 0
|
2019-07-01 14:38:36 -04:00
|
|
|
end
|
|
|
|
|
2020-04-03 12:13:34 -04: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 09:13:05 -04:00
|
|
|
log "No uploads found on S3. Skipping archiving of uploads stored on S3..." if count == 0
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
def upload_archive
|
2023-03-14 12:07:18 -04:00
|
|
|
return unless store.remote?
|
2018-10-14 21:43:31 -04:00
|
|
|
|
|
|
|
log "Uploading archive..."
|
|
|
|
content_type = MiniMime.lookup_by_filename(@backup_filename).content_type
|
|
|
|
archive_path = File.join(@archive_directory, @backup_filename)
|
2023-03-14 12:07:18 -04:00
|
|
|
store.upload_file(@backup_filename, archive_path, content_type)
|
2018-10-14 21:43:31 -04:00
|
|
|
end
|
|
|
|
|
2014-03-12 16:23:47 -04:00
|
|
|
def after_create_hook
|
2016-05-23 03:33:29 -04:00
|
|
|
log "Executing the after_create_hook for the backup..."
|
2018-10-14 21:43:31 -04:00
|
|
|
DiscourseEvent.trigger(:backup_created)
|
2014-03-12 16:23:47 -04:00
|
|
|
end
|
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
def delete_old
|
|
|
|
return if Rails.env.development?
|
|
|
|
|
|
|
|
log "Deleting old backups..."
|
2023-03-14 12:07:18 -04:00
|
|
|
store.delete_old
|
2018-09-19 14:35:43 -04:00
|
|
|
rescue => ex
|
2018-10-14 21:43:31 -04:00
|
|
|
log "Something went wrong while deleting old backups.", ex
|
2014-03-11 17:28:12 -04:00
|
|
|
end
|
|
|
|
|
2014-03-24 14:34:16 -04:00
|
|
|
def notify_user
|
2023-03-14 12:07:18 -04:00
|
|
|
return if success && @user.id == Discourse::SYSTEM_USER_ID
|
2018-12-20 06:54:02 -05:00
|
|
|
|
2014-03-24 14:34:16 -04:00
|
|
|
log "Notifying '#{@user.username}' of the end of the backup..."
|
2023-03-14 12:07:18 -04:00
|
|
|
status = success ? :backup_succeeded : :backup_failed
|
2017-03-17 02:21:30 -04:00
|
|
|
|
2021-08-03 13:06:50 -04:00
|
|
|
logs = Discourse::Utils.logs_markdown(@logs, user: @user)
|
|
|
|
post = SystemMessage.create_from_system_user(@user, status, logs: logs)
|
2017-06-14 02:00:43 -04:00
|
|
|
|
|
|
|
post.topic.invite_group(@user, Group[:admins]) if @user.id == Discourse::SYSTEM_USER_ID
|
2018-09-19 14:35:43 -04:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while notifying user.", ex
|
2014-03-24 14:34:16 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
def clean_up
|
|
|
|
log "Cleaning stuff up..."
|
2019-01-24 14:35:36 -05:00
|
|
|
delete_uploaded_archive
|
2015-02-04 05:49:11 -05:00
|
|
|
remove_tar_leftovers
|
2014-10-10 14:04:07 -04:00
|
|
|
mark_backup_as_not_running
|
2023-03-14 12:07:18 -04:00
|
|
|
refresh_disk_space if success
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
2019-01-24 14:35:36 -05:00
|
|
|
def delete_uploaded_archive
|
2023-03-14 12:07:18 -04:00
|
|
|
return unless store.remote?
|
2019-01-24 14:35:36 -05:00
|
|
|
|
|
|
|
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 08:26:30 -04:00
|
|
|
def refresh_disk_space
|
2018-09-19 14:35:43 -04:00
|
|
|
log "Refreshing disk stats..."
|
2023-03-14 12:07:18 -04:00
|
|
|
store.reset_cache
|
2018-09-19 14:35:43 -04:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while refreshing disk stats.", ex
|
2018-07-25 08:26:30 -04:00
|
|
|
end
|
|
|
|
|
2015-02-04 05:49:11 -05:00
|
|
|
def remove_tar_leftovers
|
|
|
|
log "Removing '.tar' leftovers..."
|
2018-07-04 01:57:22 -04:00
|
|
|
Dir["#{@archive_directory}/*.tar"].each { |filename| File.delete(filename) }
|
2018-09-19 14:35:43 -04:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while removing '.tar' leftovers.", ex
|
2015-02-04 05:49:11 -05:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:32:58 -05:00
|
|
|
def remove_tmp_directory
|
|
|
|
log "Removing tmp '#{@tmp_directory}' directory..."
|
|
|
|
FileUtils.rm_rf(@tmp_directory) if Dir[@tmp_directory].present?
|
2018-09-19 14:35:43 -04:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while removing the following tmp directory: #{@tmp_directory}", ex
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
2014-10-10 14:04:07 -04:00
|
|
|
def mark_backup_as_not_running
|
2014-02-12 23:32:58 -05:00
|
|
|
log "Marking backup as finished..."
|
|
|
|
BackupRestore.mark_as_not_running!
|
2018-09-19 14:35:43 -04:00
|
|
|
rescue => ex
|
|
|
|
log "Something went wrong while marking backup as finished.", ex
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_directory_exists(directory)
|
|
|
|
log "Making sure '#{directory}' exists..."
|
|
|
|
FileUtils.mkdir_p(directory)
|
|
|
|
end
|
|
|
|
|
2018-09-19 14:35:43 -04:00
|
|
|
def log(message, ex = nil)
|
2015-02-09 10:53:28 -05:00
|
|
|
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
2022-08-17 08:33:23 -04:00
|
|
|
puts(message) if !Rails.env.test?
|
2018-03-28 04:20:08 -04:00
|
|
|
publish_log(message, timestamp)
|
2015-02-09 10:53:28 -05:00
|
|
|
save_log(message, timestamp)
|
2018-09-19 14:35:43 -04:00
|
|
|
Rails.logger.error("#{ex}\n" + ex.backtrace.join("\n")) if ex
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
2015-02-09 10:53:28 -05:00
|
|
|
def publish_log(message, timestamp)
|
2014-02-12 23:32:58 -05:00
|
|
|
return unless @publish_to_message_bus
|
2015-02-09 10:53:28 -05:00
|
|
|
data = { timestamp: timestamp, operation: "backup", message: message }
|
2015-08-27 14:02:13 -04:00
|
|
|
MessageBus.publish(
|
|
|
|
BackupRestore::LOGS_CHANNEL,
|
|
|
|
data,
|
|
|
|
user_ids: [@user_id],
|
|
|
|
client_ids: [@client_id],
|
|
|
|
)
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
|
2015-02-09 10:53:28 -05:00
|
|
|
def save_log(message, timestamp)
|
|
|
|
@logs << "[#{timestamp}] #{message}"
|
2014-03-24 14:34:16 -04:00
|
|
|
end
|
|
|
|
|
2023-03-14 12:07:18 -04:00
|
|
|
def publish_completion
|
2022-08-17 08:33:23 -04:00
|
|
|
if success
|
|
|
|
log("[SUCCESS]")
|
|
|
|
DiscourseEvent.trigger(:backup_complete, logs: @logs, ticket: @ticket)
|
|
|
|
else
|
|
|
|
log("[FAILED]")
|
|
|
|
DiscourseEvent.trigger(:backup_failed, logs: @logs, ticket: @ticket)
|
|
|
|
end
|
|
|
|
end
|
2014-02-12 23:32:58 -05:00
|
|
|
end
|
|
|
|
end
|