Files
simple-aircraft-manager-rails/lib/tasks/offsite.rake
T
rickyandClaude Opus 5.5 7694719582 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>
2026-11-07 13:51:45 -05:00

33 lines
1.3 KiB
Ruby

namespace :offsite do
desc "Restore uploaded files missing from storage, from Google Drive or from a downloaded copy of its files folder: offsite:restore_files[DIR]"
task :restore_files, [ :dir ] => :environment do |_, args|
dir = args[:dir].presence
connection = OffsiteConnection.current
abort "Google Drive isn't connected: download the backup's files folder and pass its path, e.g. offsite:restore_files[/path/to/files]" if dir.nil? && !connection.connected?
if dir.nil?
drive = connection.client
drive_ids = drive.list(connection.files_folder_id).to_h { [ _1["name"], _1["id"] ] }
end
restored = 0
missing = []
ActiveStorage::Blob.find_each do |blob|
next if blob.service.exist?(blob.key)
io = if dir
path = File.join(dir, blob.key)
File.open(path, "rb") if File.file?(path)
elsif (id = drive_ids[blob.key])
drive.download(id, to: Tempfile.new("sam-restore", binmode: true))
end
next missing << "#{blob.key} (#{blob.filename})" unless io
begin
blob.service.upload(blob.key, io, checksum: blob.checksum)
restored += 1
ensure
io.close
end
end
puts "Restored #{restored} #{'file'.pluralize(restored)}."
puts "Not found (#{missing.size}):", missing.first(50) if missing.any?
end
end