Skip to content
Mementropy
RecipesHomelab recipesReference

Backing up the K3s SQLite datastore to a NAS with Restic

Online SQLite backup of K3s state plus keys, sent encrypted to an SMB NAS.

By Ryan Hammang

Status: Extended by the systemd timer recipe.

An off-host, encrypted backup of a single-node K3s server that uses the embedded SQLite datastore.

  • K3s with the default SQLite datastore
  • A NAS share reachable over SMB from the server
  • restic and sqlite3 from the distro, and a current rclone (1.63 or later has the SMB backend; use 1.75.0 or later)
  1. Install the tools. Check the distro’s rclone version first:
Terminal window
apt-cache policy restic rclone sqlite3
sudo apt install restic sqlite3
# install the official rclone .deb and check its SHA-256
  1. Keep config and the Restic password root-only:
Terminal window
sudo install -d -m 700 /etc/rancher/k3s-backup
# /etc/rancher/k3s-backup/rclone.conf (mode 0600, an [nas] remote of type smb)
# /etc/rancher/k3s-backup/restic-password (mode 0600)
  1. Create the repository on the NAS:
Terminal window
export RESTIC_PASSWORD_FILE=/etc/rancher/k3s-backup/restic-password
export RCLONE_CONFIG=/etc/rancher/k3s-backup/rclone.conf
restic -r rclone:nas:<share>/<host>/restic init
  1. Take an online copy of the datastore and check it:
Terminal window
D=/var/lib/rancher/k3s/server
sqlite3 $D/db/state.db ".backup '/tmp/k3s/state.db'"
sqlite3 /tmp/k3s/state.db 'PRAGMA integrity_check;'
  1. Copy the files a restore also needs: /etc/rancher/k3s/config.yaml, $D/token, $D/cred/encryption-config.json and $D/tls/. Write sha256sums.txt.

  2. Send it to the NAS:

Terminal window
restic -r rclone:nas:<share>/<host>/restic backup /tmp/k3s
  • integrity_check prints ok.
  • restic snapshots lists the new snapshot.
  • Run the restore drill in the next recipe.
  • A backup on the same disk as K3s isn’t a backup.
  • Ubuntu’s apt rclone was 1.60.1, with no SMB backend.
  • Without the token and encryption config, restored Secrets can’t be decrypted.
  • This covers cluster state only. Database dumps and file volumes need their own steps (see the timer recipe).