rails ruby sqlite

Building Data Resilience in Rails: A Complete Guide to SQLite Backup with Litestream

Building a Solid Data Backup Plan for SQLite

When your app runs in production, the next task is clear: protect your data. Data is the core of your product. It holds customer info, money trails, settings, and everything your app needs to work. If you lose it, you lose trust and time.

SQLite is simple and fast. But it needs help when it comes to safe backups. That is where Litestream comes in. It gives you continuous backups and easy recovery without complex setup.

This guide shows how to use Litestream with Rails and MinIO to keep your SQLite database safe.


Why SQLite Needs Careful Backups

SQLite stores your full database in one file. This makes it lightweight, but it brings risk.

Key things to understand:

  • One file storage
    If the machine fails, your database is gone.

  • No built-in replication
    SQLite does not sync to other servers by itself.

  • Active use challenge
    You cannot copy the database file while your app writes to it.

You need a backup system that handles this safely and without downtime.


Meet Litestream

Litestream watches your SQLite file and streams every change to remote storage. It works in real time.

Benefits

  • Very small data loss window
    You lose seconds of data, not hours.

  • Restore to any moment
    Useful when a bug deletes data.

  • Low impact on your app
    Runs in the background.

  • Compatible with S3 storage
    Works with AWS, MinIO, or other S3 systems.

  • Simple install
    One small binary.


Using Litestream in a Rails App

We’ll use MinIO for development.

1. Install Litestream

bundle add litestream
bin/rails generate litestream:install

Litestream adds:

  • config/litestream.yml
  • config/initializers/litestream.rb

2. Start MinIO

bundle add minio
bin/rails minio:server -- --console-address=:9001

Open http://localhost:9001

Login:

  • user: minioadmin
  • pass: minioadmin

Make a bucket called your-bucket.

3. Store credentials in Rails

EDITOR=vim bin/rails credentials:edit

Add:

litestream:
  replica_bucket: your-bucket
  replica_key_id: minioadmin
  replica_access_key: minioadmin

4. Load credentials

In config/initializers/litestream.rb:

Litestream.configure do |config|
  creds = Rails.application.credentials.litestream
  config.replica_bucket = creds.replica_bucket
  config.replica_key_id = creds.replica_key_id
  config.replica_access_key = creds.replica_access_key
end

Add storage config in config/litestream.yml:

replicas:
  - type: s3
    bucket: $LITESTREAM_REPLICA_BUCKET
    path: production.sqlite3
    endpoint: http://localhost:9000
    access-key-id: $LITESTREAM_ACCESS_KEY_ID
    secret-access-key: $LITESTREAM_SECRET_ACCESS_KEY

5. Start replication

bin/rails litestream:replicate

You will see logs showing snapshots and WAL streaming.


Restore a Backup

bin/rails litestream:restore -- --database=storage/production.sqlite3 -o=storage/restored.sqlite3

Then check counts:

sqlite3 storage/restored.sqlite3

Inside SQLite:

SELECT COUNT(*) FROM posts;
SELECT COUNT(*) FROM comments;

Counts should match production.


Automatic Backup Checks

Backups matter only if they work. Litestream has a verify! method to prove it.

Manual check

bin/rails console
Litestream.verify!("storage/production.sqlite3")

Stop replication, run again, and you should see a failure. That is expected.


Automate Verification

Create a job:

bin/rails generate job litestream/verification

Add:

class Litestream::VerificationJob < ApplicationJob
  queue_as :default

  def perform
    Litestream::Commands.databases.each do |db|
      Litestream.verify!(db["path"])
    end
  end
end

Schedule every 6 hours (Sidekiq example):

Sidekiq::Cron::Job.create(
  name: 'Litestream Backup Verification',
  cron: '0 */6 * * *',
  class: 'Litestream::VerificationJob'
)

You get alerts if backups stop working.


Production Setup

If you use Puma, add:

plugin :litestream

Litestream will start with your app.


Best Practices

  • Watch replication lag
  • Track storage space
  • Test restores often
  • Use encrypted credentials
  • Rotate keys
  • Document restore steps

These small habits protect your data.


Wrap-Up

SQLite is simple and fast. Litestream adds safety. With this setup, you have:

  • Real-time replication
  • Fast restores
  • Automatic verification
  • Rails and Puma support

Your backups are not only saved — they are tested and trusted.

You can now build with confidence. Data loss is no longer a fear.