Off-site backup to Google Drive

- Admin → Off-site Backup: connect a Google account with your own OAuth
  client (drive.file scope; refresh token and secret encrypted)
- Nightly after the verified database backup: upload the gzipped snapshot
  (newest N kept, Settings) and every uploaded file not yet copied
- Restore test downloads the newest copy, opens and checks it, and checks
  sampled files against their checksums; failures emailed to admins
- About, procedures document, policy completeness and audits use it
- offsite:restore_files rake task; restore steps in the admin guide

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-07 13:51:45 -05:00
1 parent fe3fba1736
commit 7694719582
31 files changed
+853 -20

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
# Off-site backup to Google Drive (AC 120-78B 3.2.7). One row for the
# installation: the admin's own Google OAuth client and the refresh token
# from connecting an account. The app only has access to files it created
# (the drive.file scope).
class OffsiteConnection < ApplicationRecord
ROOT_FOLDER = "Simple Aircraft Manager backups"
belongs_to :connected_by, class_name: "User", optional: true
encrypts :client_secret, :refresh_token
# Tests swap in a fake Drive.
class_attribute :client_factory, default: ->(conn) { GoogleDriveClient.new(client_id: conn.client_id, client_secret: conn.client_secret, refresh_token: conn.refresh_token) }
def self.current = first || create!
def self.connected? = current.connected?
def configured? = client_id.present? && client_secret.present?
def connected? = configured? && refresh_token.present?
def client = client_factory.call(self)
def redirect_uri = "#{Sso.app_url}/admin/offsite_backup/callback"
def disconnect!
begin
GoogleDriveClient.revoke(refresh_token) if refresh_token.present?
rescue GoogleDriveClient::Error
nil # already revoked or unreachable; forget it anyway
end
update!(refresh_token: nil, account_email: nil, folder_id: nil, database_folder_id: nil, files_folder_id: nil, connected_at: nil)
end
# Folders in Drive, made on first use (again if someone deleted them).
def ensure_folders!(drive)
changes = {}
root = folder_id if folder_id.present? && drive.folder?(folder_id)
root ||= changes[:folder_id] = drive.create_folder("#{ROOT_FOLDER} (#{URI.parse(Sso.app_url).host})")
{ database_folder_id: "database", files_folder_id: "files" }.each do |column, name|
id = self[column]
next if id.present? && !changes.key?(:folder_id) && drive.folder?(id)
changes[column] = drive.create_folder(name, parent: root)
end
update!(changes) if changes.any?
end
end
+3
View File
@@ -0,0 +1,3 @@
class OffsiteFile < ApplicationRecord
belongs_to :blob, class_name: "ActiveStorage::Blob"
end
+11
View File
@@ -0,0 +1,11 @@
class OffsiteRun < ApplicationRecord
KINDS = %w[backup restore_test].freeze
validates :kind, inclusion: { in: KINDS }
scope :backups, -> { where(kind: "backup") }
scope :restore_tests, -> { where(kind: "restore_test") }
def self.latest_backup = backups.order(:created_at, :id).last
def self.latest_restore_test = restore_tests.order(:created_at, :id).last
def recent? = created_at > 2.days.ago
end
+4 -1
View File
@@ -7,5 +7,8 @@ class RecordkeepingPolicy < ApplicationRecord
def self.current = first || create!
def complete? = [ operator_name, responsible_name, hosting_description, offsite_backup_description ].all?(&:present?)
def complete? = [ operator_name, responsible_name, hosting_description ].all?(&:present?) && offsite_backup?
# Off-site backup is described, or done by the app to Google Drive.
def offsite_backup? = offsite_backup_description.present? || OffsiteConnection.connected?
end
+1
View File
@@ -8,6 +8,7 @@ class Setting < ApplicationRecord
INTEGERS = {
"max_upload_mb" => { default: 50, in: 1..1024, label: "Maximum upload size (MB)" },
"backups_kept" => { default: 14, in: 1..365, label: "Database backups kept" },
"offsite_backups_kept" => { default: 30, in: 1..365, label: "Database copies kept in Google Drive" },
"logbook_templates_seeded" => { default: 0, in: 0..1, label: "Built-in logbook templates added" } # internal flag
}.freeze