0.0
The project is in a healthy, maintained state
Provides automated db backups for bard projects
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 0
>= 0
~> 3.0
>= 0.11
 Project Readme

Bard::Backup

Bard::Backup handles backups for a bard project:

  1. Takes a database dump and uploads it to S3 (or PUTs it to presigned URLs)
  2. Syncs configured data directories to S3 with a local manifest cache, skipping the SQLite databases Rails keeps in storage/ (the database dump covers the app's own)
  3. Deletes old database backups using a backoff heuristic: 48 hours, 30 days, 26 weeks, 24 months, then yearly
  4. Raises an error if we don't have a database backup from the previous hour
  5. Optionally encrypts uploaded payloads at rest with AES-256-GCM

Installation

Add to your Gemfile:

gem "bard-backup"

Usage

In a Rails app, configure destinations in config/bard.rb using the Bard::Config DSL:

backup do
  s3 "primary", path: "my-bucket/my-project", region: "us-west-2"
end

Credentials live in Rails encrypted credentials under bard_backup (matched by name:):

bard_backup:
  - name: primary
    access_key_id: ...
    secret_access_key: ...
    encryption_key: ...  # optional: encrypts payloads at rest with AES-256-GCM

Adding encryption_key is what turns on encryption at rest; omit it and payloads are stored in the clear. Changing its value makes existing backups undecryptable, so treat it as permanent unless you re-key deliberately.

Adding a key only encrypts what is uploaded afterwards. Database dumps stored before it still restore (they are recognized as plain gzip), and rake bard:backup:encrypt rewrites them and the data file tree in place under the key, verifying each object as it goes. It skips anything already encrypted, so it is safe to rerun.

Then run via the rake tasks provided by the bundled Railtie:

rake bard:backup          # database backup + data file-tree sync
rake bard:backup:data     # data file-tree sync only
rake bard:backup:encrypt  # encrypt everything stored before encryption_key was added
rake bard:backup:check    # prove the latest backup restores (see below)

Data directories are declared once in config/bard.rb, and bard data copies the same ones:

data "public/userfiles", "storage"

Each sync uploads only new and changed files to bard-data/<project>/ and deletes what was removed locally (the bucket's versioning keeps the old copies). When Active Storage keeps uploads on local disk outside every declared data path, bard:backup and bard:backup:data raise Bard::Backup::UncoveredUploads after backing up everything else, so the gap shows up in exception notifications instead of in a failed restore.

Checking restores

rake bard:backup:check downloads the latest database backup, decrypts it, and loads it with backhoe into a scratch database beside the live one (<database>_restore_check, or a temp file for SQLite). It then checks that the restored database has tables, a non-empty schema_migrations, and at least one table with rows. It drops the scratch database whatever the outcome. Before loading, it rejects dumps that are not valid gzip, fail their CRC, have bytes appended after the gzip stream, or end before mysqldump's or pg_dump's completion line. Any problem raises Bard::Backup::RestoreCheck::Failed, naming the backup and every problem found. On success it prints a line such as:

Restore check passed for backup 2026-11-18T03:00:00Z: 42 tables, 183920 rows

Schedule it daily, away from the hourly backup, with whenever:

every 1.day, at: "3:30 am" do
  rake "bard:backup:check"
end

or with Solid Queue in config/recurring.yml:

production:
  bard_backup_check:
    command: system "bin/rake bard:backup:check"
    schedule: every day at 3:30am
    queue: serial

The database user needs permission to create and drop the scratch database, and the server needs disk space for a second copy of the database while the check runs. When MySQL's binary log is on, the check turns it off for its own session (which needs SESSION_VARIABLES_ADMIN), so the scratch copy neither fills the binary log nor reaches replicas.

Object Lock

When the destination bucket has S3 Object Lock enabled, each dump is uploaded under a GOVERNANCE lock that lasts exactly as long as the retention policy keeps it: 48 hours for an hourly dump, 30 days for the day's 00:00 dump, 26 weeks for Monday's, 24 months for the 1st of the month's, and 25 years for January 1st's. The hourly run still deletes dumps as they age out, but in a locked bucket that only adds a delete marker, so a compromised server can hide its backups but cannot destroy them before their lock ends. Pair it with a noncurrent version lifecycle rule to clear each version once its lock lapses.

The gem detects the lock with GetObjectLockConfiguration on every run, so the backup's AWS user needs s3:GetBucketObjectLockConfiguration on the bucket and s3:PutObjectRetention on its prefix. Without the first, the backup fails rather than uploading an unlocked dump.

Restoring

Restore a database backup from S3 into the local database. The backup nearest the requested timestamp is selected (backups run hourly, so an exact match isn't guaranteed), downloaded, decrypted if necessary, and loaded via backhoe, dropping and recreating the local database.

From the bard CLI:

bard backup restore                       # list the available backup timestamps
bard backup restore 2026-06-05T16:00:00Z  # restore the backup nearest this UTC time

Or via the rake task directly:

rake "bard:backup:restore"                       # list available backups
rake "bard:backup:restore[2026-06-05T16:00:00Z]" # restore nearest this time

Or programmatically:

Bard::Backup.available                            # => [Time, Time, ...]
Bard::Backup.restore!                             # restore the latest backup
Bard::Backup.restore!(at: "2026-06-05T16:00:00Z") # restore nearest this time

Timestamps are UTC (the Z-suffixed ISO-8601 form the backups are named with). Restore requires self-managed destinations configured via the backup do ... end DSL; bard-managed backups are not yet supported.

Or call programmatically:

Bard::Backup.create!(type: :s3, path: "bucket/subfolder",
                     access_key_id: "...", secret_access_key: "...", region: "...")
Bard::Backup::FileTree.create!

UploadDestination (type: :upload, with urls: [...]) PUTs the dump to one or more presigned URLs in parallel — useful when the receiver, not the sender, holds the S3 credentials.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/botandrose/bard-backup.

License

The gem is available as open source under the terms of the MIT License.