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.
Status: Extended by the systemd timer recipe.
What it’s for
Section titled “What it’s for”An off-host, encrypted backup of a single-node K3s server that uses the embedded SQLite datastore.
What you need
Section titled “What you need”- K3s with the default SQLite datastore
- A NAS share reachable over SMB from the server
resticandsqlite3from the distro, and a currentrclone(1.63 or later has the SMB backend; use 1.75.0 or later)
- Install the tools. Check the distro’s rclone version first:
apt-cache policy restic rclone sqlite3sudo apt install restic sqlite3# install the official rclone .deb and check its SHA-256- Keep config and the Restic password root-only:
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)- Create the repository on the NAS:
export RESTIC_PASSWORD_FILE=/etc/rancher/k3s-backup/restic-passwordexport RCLONE_CONFIG=/etc/rancher/k3s-backup/rclone.confrestic -r rclone:nas:<share>/<host>/restic init- Take an online copy of the datastore and check it:
D=/var/lib/rancher/k3s/serversqlite3 $D/db/state.db ".backup '/tmp/k3s/state.db'"sqlite3 /tmp/k3s/state.db 'PRAGMA integrity_check;'-
Copy the files a restore also needs:
/etc/rancher/k3s/config.yaml,$D/token,$D/cred/encryption-config.jsonand$D/tls/. Writesha256sums.txt. -
Send it to the NAS:
restic -r rclone:nas:<share>/<host>/restic backup /tmp/k3sCheck it worked
Section titled “Check it worked”integrity_checkprintsok.restic snapshotslists the new snapshot.- Run the restore drill in the next recipe.
Traps seen
Section titled “Traps seen”- 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).