
Install your server and create a token
Install your Litebackup server first. Then create a join token on the server for instances that come and go, or check Ephemeral when you add a machine under Machines → Add machine in the console.
# Create a join token for autoscaled, spot, and container instances.
litebackup server join-token create --name autoscaled --ephemeral --uses 0 --lifetime 30d
A machine that joins with this token is ephemeral. The server removes it as soon as it drains, and removes one that stops without draining once it has been offline for --ephemeral-machine-expiry, 15 minutes by default, so instances the platform replaced do not accumulate in your machine list. Each join uses the token once, including a rejoin after the instance was removed.
Start the app under litebackup run
Install the litebackup command where the app runs, set LITEBACKUP_API_KEY to the token and your server's address, and give litebackup run the app's command after --.
# Install the litebackup command for the user that runs the app.
curl --fail --silent --show-error --location https://litebackup.com/cli.sh | bash
# Join with the ephemeral token, acquire the database, and start the app.
export LITEBACKUP_API_KEY='YOUR_JOIN_TOKEN@HOST'
litebackup run --acquire app.sqlite=/data/app.sqlite -- ./server --port 8080
litebackup run takes the write lock for app.sqlite before the app starts. The first instance creates the database as an empty file at the path, and every later instance downloads the newest version, so an instance with a fresh disk starts with current data. If another instance still holds the lock, it waits up to --lock-timeout, 5 minutes by default. The app then opens /data/app.sqlite with its usual driver while Litebackup syncs each commit to your server in the background.
litebackup run stays as the app's parent process, or PID 1 in a container. It forwards TERM, INT, HUP, QUIT, USR1, USR2, and WINCH to the app's process group and reaps orphaned processes. When the app exits, it syncs every database the session holds, releases them, and exits with the app's status, so the next instance can acquire the database at once.
In a container image, pin the release and make litebackup run the entrypoint.
# syntax=docker/dockerfile:1.10
FROM python:3.13-slim
ARG TARGETARCH
ARG LITEBACKUP_VERSION=1.20.0
ARG LITEBACKUP_SHA256
ADD --chmod=0755 --checksum=sha256:${LITEBACKUP_SHA256} \
https://litebackup-cdn.com/litebackup/${LITEBACKUP_VERSION}/litebackup-linux-${TARGETARCH} \
/usr/local/bin/litebackup
COPY app.py /app/app.py
# Keep the client's identity and state on the volume with the database.
ENV XDG_STATE_HOME=/data/state
ENTRYPOINT ["litebackup", "run", "--acquire", "app.sqlite=/data/app.sqlite", "--"]
CMD ["python3", "/app/app.py"]
LITEBACKUP_SHA256 is the SHA-256 of the executable for the image's architecture, from the release metadata, and the build fails if the download differs. Keeping the client's state on a volume is optional for ephemeral instances. With it, a restarted instance only fetches what changed. Without it, the instance joins again with the token and downloads the database.
Give the drain time to finish
The platform sends TERM and kills the instance when its grace period ends. In that time the app has to exit and litebackup run has to send the writes your server does not have yet. Set the grace period to a minute or more, for example docker stop --timeout 60, stop_grace_period: 60s in Docker Compose, or kill_timeout on Fly.io. Add --drain-timeout below that period if you want litebackup run to give up first and exit with status 125.
An instance that crashes or loses power does not drain. Its writes up to the last sync are on your server, and the next instance acquires the database once the old session's lease lapses, about a minute with the server's default --session-lease.
Test a shutdown the way the platform does it
To check that the drain finishes before the platform kills the old instance, the app below records a row with its host name when it receives TERM and prints the newest row when it starts. When the next instance prints the row the previous one wrote as it stopped, that row reached the new instance through your server.
# Serve the shutdown log, and record a row when the platform stops the app.
import os
import signal
import socket
import sqlite3
import sys
from datetime import datetime, timezone
from wsgiref.simple_server import make_server
DATABASE = os.environ.get("DATABASE", "/data/app.sqlite")
database = sqlite3.connect(DATABASE)
database.execute("CREATE TABLE IF NOT EXISTS shutdowns (host TEXT, stop_time TEXT)")
last = database.execute("SELECT host, stop_time FROM shutdowns ORDER BY rowid DESC LIMIT 1").fetchone()
print("last shutdown", last, file=sys.stderr)
def stop(signal_number, frame):
database.execute(
"INSERT INTO shutdowns VALUES (?, ?)",
(socket.gethostname(), datetime.now(timezone.utc).isoformat()),
)
database.commit()
database.close()
sys.exit(0)
def shutdown_log(environ, start_response):
rows = database.execute("SELECT host, stop_time FROM shutdowns ORDER BY rowid").fetchall()
start_response("200 OK", [("Content-Type", "text/plain")])
return ["".join(f"{host} {stop_time}\n" for host, stop_time in rows).encode()]
signal.signal(signal.SIGTERM, stop)
make_server("", int(os.environ.get("PORT", "8080")), shutdown_log).serve_forever()
Save it as app.py next to the Dockerfile above, then build the image and start the first instance.
# Look up the SHA-256 of the 1.20.0 Linux executable in the release metadata.
LITEBACKUP_SHA256="$(curl --fail --silent --show-error --location \
https://litebackup.com/pkg/metadata \
| jq --raw-output '.packages[] | select(.slug == "cli") | .releases[]
| select(.version == "1.20.0") | .artifacts[]
| select(.name == "litebackup-linux-amd64") | .sha256')"
# Build the image and start the first instance.
docker build --build-arg LITEBACKUP_SHA256="${LITEBACKUP_SHA256}" --tag shutdown-test .
export LITEBACKUP_API_KEY='YOUR_JOIN_TOKEN@HOST'
docker run --detach --name first --env LITEBACKUP_API_KEY --volume first-data:/data shutdown-test
Stop it the way the platform would, then start a second instance on an empty volume, as a new machine would be, and read the log.
# Stop the first instance, start a second one, and read the shutdown log.
docker stop --timeout 60 first
docker run --detach --name second --env LITEBACKUP_API_KEY --volume second-data:/data \
--publish 127.0.0.1:8080:8080 shutdown-test
curl http://127.0.0.1:8080/
The second instance prints the row the first one wrote as it stopped. docker logs first shows the signal, exited, and drain events from litebackup run. To see a crash instead, stop the second instance with docker kill, start a third, and it waits about a minute for the lease before it starts the app.
Guides for each platform
- Fly.io runs
litebackup runas the entrypoint of a Machine with a volume and drains withinkill_timeout. - DigitalOcean runs the app as a systemd user unit on Droplets, with Spaces as the bucket.
- Rails acquires and drains from the Railtie inside Puma, with or without
litebackup run.
The deployment guides list every platform. For backups on a single machine, start with how to back up SQLite, and for deploys in general, see SQLite in production.
Install
Install your Litebackup server on a Linux machine you control, then join your first machine with the command line or an SDK. The deployment guides cover each platform. Keep a second, independent backup of data you cannot lose, so no single system holds the only copy.