Automatic sqlite3 backups for Rails applications using LiteStream and Tigris

When running SQLite in production, you need to have a solid backup mechanism setup. And I say this as someone who has accidentally deleted the production SQLite database. Trust me, resilience is something you should have setup from day one.

Stephen Margheim - Supercharge the One Person Framework with SQLite: Rails World 2024

With flyctl v0.3.58 and dockerfile-rails release v1.7.0, all that is required is fly launch.


Demo

No special configuration is required. Create an application and deploy it. For example:

rails new demo --css tailwind
cd demo
./bin/rails generate scaffold Post name:string title:string content:text
./bin/rails db:migrate
echo 'Rails.application.routes.draw { root "posts#index" }' >> config/routes.rb
flyctl launch

Accept the defaults and your app is up and running.

To opt out, indicate that you want to tweak the settings before proceeding and unselect Tigris.

To demonstrate worry free backups, use the application to make a draft blog post:

  • Click on the “New post” button

  • Enter your name, a title, and some content; then click “Create Post”

Now destroy your machine and volume. If you have jq installed you can do so with the following commands:

fly machine list --json | jq -r ".[]|.id" | xargs fly machine destroy -f
fly volume list --json | jq -r ".[]|.id" | xargs fly volume destroy -y

(If you don’t have jq, omit --json and run the destroy commands directly. Or just brew install jq / apt-get install jq)

Once this is done, run fly deploy, visit your web page. Your post should be there.

7 Likes