Vaultwarden Backup to a Private Repo
What it does
Every night at 03:00 a script on the VPS:
- Takes a consistent snapshot of the Vaultwarden database
- Bundles it with attachments, sends, config and RSA keys
- Encrypts it with
age(public key only on the server) - Force-pushes it as a single commit to a private GitHub repo (latest only)
- Pings an Uptime Kuma Push monitor so a missed backup raises an alert
Assumptions
- Vaultwarden runs in Docker with a bind mount, e.g.
./vw-data:/data - Ubuntu VPS, commands run with
sudo - UptimeKuma is running so it notifies with push notification of a failed backup
- My Data dir here:
/home/ubuntu/selfhosted/vaultwarden/vw-data(changeDATA_DIRif yours differs)
Why this design
sqlite3 .backupgives a safe copy while Vaultwarden is running- age encryption means GitHub never sees plaintext vault data
- Only the public key lives on the VPS, so a hacked VPS cannot decrypt old backups
- The deploy key is limited to one repo, so no personal token sits on the server
- Latest-only keeps the repo tiny (no growing history of blobs)
Step 1: GitHub repo
Create a private, empty repo, e.g. vaultwarden_backup. Use the SSH URL in the script: git@github.com:USER/REPO.git
Step 2: Uptime Kuma Push monitor
Add New Monitor, type Push, name it. Set the heartbeat interval to 90000 seconds (25 hours, so one late night doesn't false-alarm). Save, then copy the push URL from the monitor's edit page:
https://KUMA_HOST/api/push/TOKEN?status=up&msg=OK&ping=
The token is a secret. If it leaks, regenerate it in Kuma.
Step 3: Install tools (on the VPS)
sudo apt install -y age sqlite3 git cron
sudo systemctl enable --now cron
Step 4: Generate the age key
age-keygen -o ~/age-key.txt
It prints Public key: age1.... That goes into AGE_PUBKEY in the script.
Now save the private key off the server:
cat ~/age-key.txt
Store at least one copy somewhere that survives both a VPS loss and a PC reinstall: cloud drive (ideally passphrase-protected), paper, USB stick, or a secure note in a password manager. Storing it only inside this Vaultwarden is not enough: if the VPS dies you lose both the vault and the key needed to decrypt its backup.
Once the off-server copy is confirmed, delete the key from the VPS:
shred -u ~/age-key.txt
Never store the private key as a GitHub secret. Secrets are write-only, so you could never read it back.
Step 5: Create the deploy key
sudo ssh-keygen -t ed25519 -f /root/.ssh/vw_backup -N ""
sudo cat /root/.ssh/vw_backup.pub
In GitHub: repo, Settings, Deploy keys, Add deploy key. Paste the public key and tick Allow write access.
Step 6: The script
Save as /root/vw-backup.sh (sudo nano /root/vw-backup.sh) and fill in the five values at the top.
#!/usr/bin/env bash
set -euo pipefail
DATA_DIR="/home/ubuntu/selfhosted/vaultwarden/vw-data"
AGE_PUBKEY="age1REPLACE_ME"
REPO="git@github.com:USER/REPO.git"
KUMA_URL="https://KUMA_HOST/api/push/TOKEN?status=up&msg=OK&ping="
SSH_KEY="/root/.ssh/vw_backup"
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
mkdir "$WORK/snap"
# consistent DB snapshot while Vaultwarden runs
sqlite3 "$DATA_DIR/db.sqlite3" ".backup '$WORK/snap/db.sqlite3'"
# other data
cd "$DATA_DIR"
for f in attachments sends config.json rsa_key*; do
if [ -e "$f" ]; then cp -a "$f" "$WORK/snap/"; fi
done
# archive + encrypt
tar -C "$WORK" -czf - snap | age -r "$AGE_PUBKEY" -o "$WORK/vw-backup.tar.gz.age"
# fresh single-commit repo, force-pushed (latest only)
mkdir "$WORK/repo"
mv "$WORK/vw-backup.tar.gz.age" "$WORK/repo/"
cd "$WORK/repo"
git init -q -b main
git add .
git -c user.name=backup -c user.email=backup@localhost commit -q -m "backup $(date -u +%F)"
GIT_SSH_COMMAND="ssh -i $SSH_KEY -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" \
git push -q -f "$REPO" main
# tell Uptime Kuma it worked
curl -fsS -m 10 "$KUMA_URL" >/dev/null
Step 7: Test it
sudo chmod 700 /root/vw-backup.sh
sudo /root/vw-backup.sh
Check that the repo contains vw-backup.tar.gz.age and Kuma shows a heartbeat.
Step 8: Schedule nightly at 03:00
(sudo crontab -l 2>/dev/null; echo '0 3 * * * /root/vw-backup.sh >> /var/log/vw-backup.log 2>&1') | sudo crontab -
sudo crontab -l
Logs go to /var/log/vw-backup.log.
Restore
- Get the private key from wherever you stored it and save it as
age-key.txt - Download
vw-backup.tar.gz.agefrom the GitHub repo - Stop the container:
docker compose down - Copy the contents of the extracted
snap/folder intovw-data/(db.sqlite3,attachments,sends,config.json,rsa_key*) - Start it:
docker compose up -d - Log in and check your items
Decrypt and extract:
age -d -i age-key.txt vw-backup.tar.gz.age | tar xz
Test a restore once, before you need it.
Troubleshooting
| Problem | Fix |
|---|---|
crontab: command not found |
sudo apt install -y cron |
| Push rejected / permission denied | Deploy key lacks write access, or REPO uses https instead of SSH |
| No Kuma alert on failure | Check the interval is 90000s and the monitor type is Push |
| Backup file missing in repo | Run the script by hand and read the error |
Housekeeping
- Set
SIGNUPS_ALLOWEDto"false"indocker-compose.ymlonce accounts exist - Keep the age private key copy safe; without it the backups are unreadable
- Rotate the Kuma token if it is ever exposed
- Prerequisite software packages can be installed with
sudo apt install -y age sqlite3 git cron curl; sudo systemctl enable cron