2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
2015-03-26 02:21:27 -04:00
|
|
|
|
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class ScheduleBackup < ::Jobs::Scheduled
|
2015-11-12 10:14:49 -05:00
|
|
|
daily at: 0.hours
|
2015-03-26 02:21:27 -04:00
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def execute(args)
|
2024-01-08 15:41:00 -05:00
|
|
|
delete_prior_to_n_days
|
2017-12-21 15:21:28 -05:00
|
|
|
return unless SiteSetting.enable_backups? && SiteSetting.automatic_backups_enabled?
|
2015-08-07 11:34:58 -04:00
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
store = BackupRestore::BackupStore.create
|
|
|
|
if latest_backup = store.latest_file
|
|
|
|
date = latest_backup.last_modified.to_date
|
2015-11-12 12:35:03 -05:00
|
|
|
return if (date + SiteSetting.backup_frequency.days) > Time.now.utc.to_date
|
2015-08-07 11:34:58 -04:00
|
|
|
end
|
|
|
|
|
2019-10-21 13:25:35 -04:00
|
|
|
::Jobs.cancel_scheduled_job(:create_backup)
|
2015-11-12 10:14:49 -05:00
|
|
|
|
|
|
|
time_of_day = Time.parse(SiteSetting.backup_time_of_day)
|
|
|
|
seconds = time_of_day.hour.hours + time_of_day.min.minutes + rand(10.minutes)
|
|
|
|
|
2019-10-21 13:25:35 -04:00
|
|
|
::Jobs.enqueue_in(seconds, :create_backup)
|
2018-10-02 09:48:16 -04:00
|
|
|
rescue => e
|
|
|
|
notify_user(e)
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
2024-01-08 15:41:00 -05:00
|
|
|
def delete_prior_to_n_days
|
|
|
|
BackupRestore::Backuper.new(Discourse.system_user.id).delete_prior_to_n_days
|
|
|
|
end
|
|
|
|
|
2018-10-02 09:48:16 -04:00
|
|
|
def notify_user(ex)
|
2021-03-02 12:51:50 -05:00
|
|
|
SystemMessage.create_from_system_user(
|
2018-10-02 09:48:16 -04:00
|
|
|
Discourse.system_user,
|
|
|
|
:backup_failed,
|
2021-03-02 12:51:50 -05:00
|
|
|
target_group_names: Group[:admins].name,
|
2018-10-02 09:48:16 -04:00
|
|
|
logs: "#{ex}\n" + ex.backtrace.join("\n"),
|
|
|
|
)
|
2015-03-26 02:21:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|