Files
simple-aircraft-manager-rails/app/services/database_backup.rb
T
rickyandClaude Opus 5.5 89ed0a76f9 Move all user-facing text into config/locales/en.yml for localization
- Views use lazy t(".key") lookups; sentences with values or links stay
  whole (interpolation, _html keys); counts use one/other plurals
- Stored-value labels (statuses, types, priorities...) are LabelSets read
  from labels: in the current locale; field names and validation messages
  under activerecord:; flash and activity text under flash:
- Services, models, helpers, mailer subjects, the logbook PDF, procedures
  templates, records package README and transcription warnings localized;
  the PDF reuses LogbookEntry#certification_statement instead of a copy
- Scripts read js: strings from a JSON tag via app/javascript/i18n.js
- Dates through I18n.l (app, weekday, month_year, long formats); html lang
  follows the locale
- APP_LOCALE picks the language (falls back to English); missing
  translations raise in development and tests
- README: Translating section and APP_LOCALE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:08:20 -05:00

58 lines
2.5 KiB
Ruby

# Built-in backup of the main database (AC 120-78B 3.2.7): a consistent
# snapshot (SQLite's online backup API, safe while the app runs) in storage/backups,
# checked by opening it and running SQLite's integrity check, and the newest
# few kept. Uploaded files aren't copied (they're already in storage/ and,
# being deduplicated, cheap to back up with the rest of the volume).
class DatabaseBackup
DIR = Rails.root.join("storage", "backups")
def self.run! = new.run!
def run!
FileUtils.mkdir_p(DIR)
path = DIR.join("sam-#{Time.current.utc.strftime('%Y%m%d-%H%M%S')}.sqlite3")
copy(path)
message = verify(path)
bytes = File.size(path)
prune(keep_path: path)
BackupRun.create!(ok: true, path: path.to_s, bytes: bytes, message: message)
rescue StandardError => e
Rails.logger.error("[backup] #{e.class}: #{e.message}")
BackupRun.create!(ok: false, path: path&.to_s, message: "#{e.class.name.demodulize}: #{e.message}".truncate(500))
end
private
def copy(path)
source = ActiveRecord::Base.connection.raw_connection
dest = SQLite3::Database.new(path.to_s)
backup = SQLite3::Backup.new(dest, "main", source, "main")
result = backup.step(-1)
backup.finish
raise "the database was busy (SQLite code #{result}); try again" unless result == SQLite3::Constants::ErrorCode::DONE
ensure
dest&.close
end
# Opens the copy and checks it: SQLite's integrity check, and the same
# number of logbook entries and signatures as the live database.
def verify(path)
db = SQLite3::Database.new(path.to_s, readonly: true)
result = db.get_first_value("PRAGMA integrity_check")
raise "integrity check failed: #{result}" unless result == "ok"
counts = %w[logbook_entries entry_signatures].map { |t| [ t, db.get_first_value("SELECT COUNT(*) FROM #{t}") ] }.to_h
live = { "logbook_entries" => LogbookEntry.count, "entry_signatures" => EntrySignature.count }
raise "record counts differ (#{counts} vs #{live})" unless counts == live
I18n.t("services.database_backup.verified_sqlite_integrity_check_ok", logbook_entries: (counts["logbook_entries"]), entry_signatures: (counts["entry_signatures"]))
ensure
db&.close
end
# Keeps the newest few (by file time), always including the one just made.
def prune(keep_path:)
keep = Setting.integer("backups_kept")
older = Dir[DIR.join("sam-*.sqlite3")].reject { _1 == keep_path.to_s }.sort_by { File.mtime(_1) }.reverse
older.drop(keep - 1).each { File.delete(_1) }
end
end