discourse/app/jobs/scheduled/schedule_backup.rb
Ted Johansson 71ea236a79
FIX: Use a valid value for disabling backups using backup_frequency (#34245)
In #33558 we removed automatic_backups_enabled setting, and instead rely on backup_frequency being blank to disable.

There was a big oversight there with the site setting type system, which will coerce the value to an integer. It also makes it so you can't blank the value out in the UI.

This is a "fix forward" solution where instead of "set to blank to disable" we do "set to 0 to disable". This works along the grain of the site setting type system for a workable fix where we don't have to deal with the irreversible migration in the previous change.

We can potentially go and add in "nullable" to the type system at a later point.
2025-08-12 13:19:56 +08:00

43 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Jobs
class ScheduleBackup < ::Jobs::Scheduled
daily at: 0.hours
sidekiq_options retry: false
def execute(args)
delete_prior_to_n_days
return if !SiteSetting.enable_backups?
return if SiteSetting.backup_frequency.zero?
store = BackupRestore::BackupStore.create
if latest_backup = store.latest_file
date = latest_backup.last_modified.to_date
return if (date + SiteSetting.backup_frequency.days) > Time.now.utc.to_date
end
::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)
rescue => e
notify_user(e)
raise
end
def delete_prior_to_n_days
BackupRestore::Backuper.new(Discourse.system_user.id).delete_prior_to_n_days
end
def notify_user(ex)
SystemMessage.create_from_system_user(
Discourse.system_user,
:backup_failed,
target_group_names: Group[:admins].name,
logs: "#{ex}\n" + ex.backtrace.join("\n"),
)
end
end
end