Files
simple-aircraft-manager-rails/app/jobs/offsite_backup_job.rb
T
rickyandClaude Opus 5.5 1412470cd5 Maintenance commands: doctor, records check, restore, sign out all, more
- sam:doctor: health report (secret key, address, HTTPS, SMTP and SSO
  reachable, Google Drive, migrations, disk, jobs, backup/check/off-site/
  procedures/audit recency); exits 1 on a failure
- records:check: backup now + history and signature check; exits 1 on a problem
- backups:list / backups:restore[file]: checks the copy (nightly or Drive
  .gz), refuses while the server runs, moves the current DB aside, asks
  (CONFIRM=yes to skip)
- users:sign_out_all, sam:records_package[tail], email:test[address],
  offsite:backup, offsite:restore_test
- Admin guide: maintenance commands table, restore steps use backups:restore

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:42:52 -05:00

24 lines
1.2 KiB
Ruby

# Copies the latest verified database backup and new uploaded files to
# Google Drive (queued by the nightly RecordsIntegrityJob, or from Admin →
# Off-site Backup). Admins are emailed if it fails.
class OffsiteBackupJob < ApplicationJob
queue_as :default
limits_concurrency to: 1, key: "offsite_backup", duration: 12.hours if respond_to?(:limits_concurrency)
def perform(kind = "backup")
return unless OffsiteConnection.connected?
run = kind == "restore_test" ? OffsiteBackup.restore_test! : self.class.backup
return if run.nil? || run.ok || !MailSettings.configured?
User.active.where(admin: true).find_each { AdminMailer.offsite_alert(_1, run).deliver_now }
end
# Copies the latest verified backup (making one if there's none from the
# last day). Returns the OffsiteRun. Also used by bin/rails offsite:backup.
def self.backup
latest = BackupRun.where(ok: true).order(:created_at, :id).last
latest = DatabaseBackup.run! unless latest && latest.created_at > 1.day.ago && latest.path.present? && File.exist?(latest.path)
return OffsiteRun.create!(kind: "backup", ok: false, message: "No verified database backup to copy: #{latest.message}") unless latest.ok
OffsiteBackup.run!(latest.path)
end
end