mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
AC 120-78B chapter 3:
- Every change to logbook entries, sub-components, signatures, inspection
records, AD compliance, major records, W&B, components, squawks and
documents is kept as a RecordVersion (who, IP, when, field changes),
hash-chained per aircraft; versions can't be edited or deleted. Shown on
each logbook entry and in Activity -> Record history
- Nightly job: SQLite online-backup copy to storage/backups, verified
(integrity_check + record counts), pruned to the configured number; then
an integrity check of the history chains and every signature's seal and
fingerprint. Results on Admin -> About ("Back up and check now"); admins
are emailed on failure
- Software versions recorded (first/last seen) on Admin -> About
- Records package (Access & Sharing): logbook entries PDF with signatures,
signatures.csv, record-history.csv, self-contained status report HTML,
the .sam.zip, and a README, for the FAA/NTSB or a buyer
- Legacy (paper) records: documents can be marked as legacy records;
aircraft have an "electronic records since" date (status report, package)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
2.4 KiB
Ruby
58 lines
2.4 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
|
|
"Verified: SQLite integrity check ok; #{counts['logbook_entries']} logbook entries, #{counts['entry_signatures']} 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
|