KlangDocs

    Open Klang

    Self-hosting

    View as Markdown

    Backups

    What to back up (the database, the files and .env), the commands to do it, and how to restore an install from them on the same or another machine.

    What to back up

    What Where Why
    The database the postgres service Accounts, servers, messages, everything but the files
    The files the s3-data volume Attachments, avatars, emojis, and the staff’s evidence
    .env selfhost/.env The secrets. Without the same ones, a restored install signs everyone out and its webhooks and bot keys stop working

    You don’t need Valkey (valkey-data): it’s a cache and the rate limits, rebuilt by itself. Nor Caddy’s volumes: it gets the certificates again. LiveKit keeps nothing; its keys are in .env.

    Keep the backups off the server, and keep .env as safe as the server itself: it has every key of the install.

    Back up

    From selfhost/, with the install running. Volume names start with the Compose project’s name. The commands below use the default one, klang-server, so the files’ volume is klang-server_s3-data. With another project name (-p), change it (docker volume ls shows yours). backups/ is ignored by git, so a backup never ends up in a commit.

    Shell
    mkdir -p backups
    
    # The database, while it runs: pg_dump takes a consistent snapshot.
    docker compose exec -T postgres pg_dump -U klang -d klang -Fc > backups/klang-db.dump
    
    # The files: the store stops for a few seconds (an upload made meanwhile fails).
    docker compose stop s3
    docker run --rm -v klang-server_s3-data:/data:ro -v "$PWD/backups":/backup alpine:3.22 \
      tar -czf /backup/klang-files.tar.gz -C /data .
    docker compose start s3
    
    cp .env backups/klang.env

    Add the date to the file names if you keep several, and copy the backups folder to another machine. A nightly cron job with these lines is enough for a small install.

    Restore

    On a new machine, or after losing the volumes. You need the same release checked out (see Install), and the three files in selfhost/backups/.

    Shell
    cp backups/klang.env .env
    
    # Create the services and their empty volumes, without starting them.
    docker compose create
    
    # The database first, into the new empty one.
    docker compose start postgres
    until docker compose exec -T postgres pg_isready -U klang -d klang; do sleep 1; done
    docker compose exec -T postgres pg_restore -U klang -d klang --no-owner --exit-on-error \
      < backups/klang-db.dump
    
    # The files, into the store’s volume while it’s stopped.
    docker run --rm -v klang-server_s3-data:/data -v "$PWD/backups":/backup alpine:3.22 \
      tar -xzf /backup/klang-files.tar.gz -C /data
    
    # Everything else.
    docker compose up -d

    migrate finds the database up to date and the API starts as it was. Sign in and open a channel with files to check.

    Restoring over an install that still has data: stop it and delete its volumes first (docker compose down -v, which deletes everything of this install), then follow the steps above.

    Snapshots instead

    If your provider snapshots the server’s disk, that also works, but only with the install stopped (docker compose stop) while the snapshot is taken: a snapshot of a running Postgres may not start again.