2014-10-11 02:04:07 +08:00
|
|
|
require "backup_restore/backup_restore"
|
2014-02-13 12:33:21 +08:00
|
|
|
|
|
|
|
class Admin::BackupsController < Admin::AdminController
|
|
|
|
|
2017-12-22 04:21:28 +08:00
|
|
|
before_action :ensure_backups_enabled
|
2017-08-31 12:06:56 +08:00
|
|
|
skip_before_action :check_xhr, only: [:index, :show, :logs, :check_backup_chunk, :upload_backup_chunk]
|
2014-02-13 12:33:21 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
store_preloaded("backups", MultiJson.dump(serialize_data(Backup.all, BackupSerializer)))
|
|
|
|
store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status))
|
2014-02-14 02:41:46 +08:00
|
|
|
store_preloaded("logs", MultiJson.dump(BackupRestore.logs))
|
2014-02-13 12:33:21 +08:00
|
|
|
render "default/empty"
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render_serialized(Backup.all, BackupSerializer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
render_json_dump(BackupRestore.operations_status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-08-21 00:48:56 +08:00
|
|
|
opts = {
|
|
|
|
publish_to_message_bus: true,
|
2015-08-28 02:02:13 +08:00
|
|
|
with_uploads: params.fetch(:with_uploads) == "true",
|
|
|
|
client_id: params[:client_id],
|
2014-08-21 00:48:56 +08:00
|
|
|
}
|
|
|
|
BackupRestore.backup!(current_user.id, opts)
|
2014-02-13 12:33:21 +08:00
|
|
|
rescue BackupRestore::OperationRunningError
|
|
|
|
render json: failed_json.merge(message: I18n.t("backup.operation_already_running"))
|
|
|
|
else
|
2017-01-17 02:53:31 +08:00
|
|
|
StaffActionLogger.new(current_user).log_backup_create
|
2014-02-13 12:33:21 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def cancel
|
|
|
|
BackupRestore.cancel!
|
|
|
|
rescue BackupRestore::OperationRunningError
|
|
|
|
render json: failed_json.merge(message: I18n.t("backup.operation_already_running"))
|
|
|
|
else
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2017-03-01 23:26:18 +08:00
|
|
|
def email
|
2017-01-17 02:53:31 +08:00
|
|
|
if backup = Backup[params.fetch(:id)]
|
2017-12-18 11:25:22 +08:00
|
|
|
Jobs.enqueue(:download_backup_email,
|
|
|
|
user_id: current_user.id,
|
|
|
|
backup_file_path: url_for(controller: 'backups', action: 'show')
|
|
|
|
)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2017-03-01 23:26:18 +08:00
|
|
|
else
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 404
|
2017-03-01 23:26:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
|
|
|
if !EmailBackupToken.compare(current_user.id, params.fetch(:token))
|
|
|
|
@error = I18n.t('download_backup_mailer.no_token')
|
|
|
|
end
|
|
|
|
if !@error && backup = Backup[params.fetch(:id)]
|
|
|
|
EmailBackupToken.del(current_user.id)
|
2017-01-17 02:53:31 +08:00
|
|
|
StaffActionLogger.new(current_user).log_backup_download(backup)
|
|
|
|
headers['Content-Length'] = File.size(backup.path).to_s
|
2014-02-13 12:33:21 +08:00
|
|
|
send_file backup.path
|
|
|
|
else
|
2017-03-01 23:26:18 +08:00
|
|
|
if @error
|
2018-01-05 22:09:40 +08:00
|
|
|
render template: 'admin/backups/show.html.erb', layout: 'no_ember', status: 422
|
2017-03-01 23:26:18 +08:00
|
|
|
else
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 404
|
2017-03-01 23:26:18 +08:00
|
|
|
end
|
2014-02-13 12:33:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2017-01-17 02:53:31 +08:00
|
|
|
if backup = Backup[params.fetch(:id)]
|
|
|
|
StaffActionLogger.new(current_user).log_backup_destroy(backup)
|
2014-03-12 05:28:12 +08:00
|
|
|
backup.remove
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-03-12 05:28:12 +08:00
|
|
|
else
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: 404
|
2014-03-12 05:28:12 +08:00
|
|
|
end
|
2014-02-13 12:33:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def logs
|
|
|
|
store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status))
|
2014-02-14 02:41:46 +08:00
|
|
|
store_preloaded("logs", MultiJson.dump(BackupRestore.logs))
|
2014-02-13 12:33:21 +08:00
|
|
|
render "default/empty"
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
2015-08-28 02:02:13 +08:00
|
|
|
opts = {
|
|
|
|
filename: params.fetch(:id),
|
|
|
|
client_id: params.fetch(:client_id),
|
|
|
|
publish_to_message_bus: true,
|
|
|
|
}
|
2016-06-25 05:14:47 +08:00
|
|
|
SiteSetting.set_and_log(:disable_emails, true, current_user)
|
2015-08-28 02:02:13 +08:00
|
|
|
BackupRestore.restore!(current_user.id, opts)
|
2014-02-13 12:33:21 +08:00
|
|
|
rescue BackupRestore::OperationRunningError
|
|
|
|
render json: failed_json.merge(message: I18n.t("backup.operation_already_running"))
|
|
|
|
else
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def rollback
|
|
|
|
BackupRestore.rollback!
|
|
|
|
rescue BackupRestore::OperationRunningError
|
|
|
|
render json: failed_json.merge(message: I18n.t("backup.operation_already_running"))
|
|
|
|
else
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def readonly
|
|
|
|
enable = params.fetch(:enable).to_s == "true"
|
2017-01-11 16:38:07 +08:00
|
|
|
readonly_mode_key = Discourse::USER_READONLY_MODE_KEY
|
|
|
|
|
|
|
|
if enable
|
|
|
|
Discourse.enable_readonly_mode(readonly_mode_key)
|
|
|
|
else
|
|
|
|
Discourse.disable_readonly_mode(readonly_mode_key)
|
|
|
|
end
|
|
|
|
|
2017-01-11 14:46:48 +08:00
|
|
|
StaffActionLogger.new(current_user).log_change_readonly_mode(enable)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-02-13 12:33:21 +08:00
|
|
|
end
|
|
|
|
|
2014-05-28 04:14:37 +08:00
|
|
|
def check_backup_chunk
|
2014-02-22 08:41:01 +08:00
|
|
|
identifier = params.fetch(:resumableIdentifier)
|
|
|
|
filename = params.fetch(:resumableFilename)
|
|
|
|
chunk_number = params.fetch(:resumableChunkNumber)
|
|
|
|
current_chunk_size = params.fetch(:resumableCurrentChunkSize).to_i
|
|
|
|
|
|
|
|
# path to chunk file
|
|
|
|
chunk = Backup.chunk_path(identifier, filename, chunk_number)
|
2014-05-28 04:14:37 +08:00
|
|
|
# check chunk upload status
|
|
|
|
status = HandleChunkUpload.check_chunk(chunk, current_chunk_size: current_chunk_size)
|
2014-02-22 08:41:01 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil, status: status
|
2014-02-22 08:41:01 +08:00
|
|
|
end
|
|
|
|
|
2014-05-28 04:14:37 +08:00
|
|
|
def upload_backup_chunk
|
2014-02-27 02:38:06 +08:00
|
|
|
filename = params.fetch(:resumableFilename)
|
|
|
|
total_size = params.fetch(:resumableTotalSize).to_i
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
return render status: 415, plain: I18n.t("backup.backup_file_should_be_tar_gz") unless /\.(tar\.gz|t?gz)$/i =~ filename
|
|
|
|
return render status: 415, plain: I18n.t("backup.not_enough_space_on_disk") unless has_enough_space_on_disk?(total_size)
|
|
|
|
return render status: 415, plain: I18n.t("backup.invalid_filename") unless !!(/^[a-zA-Z0-9\._\-]+$/ =~ filename)
|
2014-02-22 08:41:01 +08:00
|
|
|
|
|
|
|
file = params.fetch(:file)
|
|
|
|
identifier = params.fetch(:resumableIdentifier)
|
|
|
|
chunk_number = params.fetch(:resumableChunkNumber).to_i
|
|
|
|
chunk_size = params.fetch(:resumableChunkSize).to_i
|
|
|
|
current_chunk_size = params.fetch(:resumableCurrentChunkSize).to_i
|
|
|
|
|
|
|
|
# path to chunk file
|
|
|
|
chunk = Backup.chunk_path(identifier, filename, chunk_number)
|
2014-05-28 04:14:37 +08:00
|
|
|
# upload chunk
|
|
|
|
HandleChunkUpload.upload_chunk(chunk, file: file)
|
2014-02-22 08:41:01 +08:00
|
|
|
|
|
|
|
uploaded_file_size = chunk_number * chunk_size
|
|
|
|
# when all chunks are uploaded
|
|
|
|
if uploaded_file_size + current_chunk_size >= total_size
|
|
|
|
# merge all the chunks in a background thread
|
2016-03-03 19:01:13 +08:00
|
|
|
Jobs.enqueue_in(5.seconds, :backup_chunks_merger, filename: filename, identifier: identifier, chunks: chunk_number)
|
2014-02-22 08:41:01 +08:00
|
|
|
end
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-02-22 08:41:01 +08:00
|
|
|
end
|
|
|
|
|
2014-02-27 02:38:06 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def has_enough_space_on_disk?(size)
|
2015-01-27 05:25:32 +08:00
|
|
|
`df -Pk #{Rails.root}/public/backups | awk 'NR==2 {print $4 * 1024;}'`.to_i > size
|
2014-02-27 02:38:06 +08:00
|
|
|
end
|
|
|
|
|
2017-12-22 04:21:28 +08:00
|
|
|
def ensure_backups_enabled
|
|
|
|
raise Discourse::InvalidAccess.new unless SiteSetting.enable_backups?
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:33:21 +08:00
|
|
|
end
|