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