Backups & Snapshots

Back up SQLite by copying its files, or take automatic etcd snapshots on HA clusters.

Your cluster's entire state lives in its datastore, so backing it up is critical.

SQLite (single server)

Stop k3s and copy the /var/lib/rancher/k3s/server/db directory. Restore by putting the files back.

Embedded etcd (HA)

k3s takes automatic etcd snapshots on a schedule and can restore from them. Snapshots are saved under /var/lib/rancher/k3s/server/db/snapshots and can be shipped to S3-compatible storage.

Example

Example · bash
# Take an on-demand etcd snapshot
sudo k3s etcd-snapshot save --name pre-upgrade

# List snapshots
sudo k3s etcd-snapshot ls

# Restore a server from a snapshot (server stopped)
sudo k3s server \
  --cluster-reset \
  --cluster-reset-restore-path=/var/lib/rancher/k3s/server/db/snapshots/pre-upgrade

When to use it

  • An ops team schedules a nightly cron job to copy the k3s SQLite database file to an NFS share so the cluster can be restored after a server failure.
  • A production k3s HA cluster uses automatic etcd snapshots saved to S3 so operators can restore to any point from the past 5 days.
  • A developer takes a manual etcd snapshot before applying a large schema migration to provide a fast rollback path if the migration fails.

More examples

Back up the SQLite datastore

Stops k3s to get a consistent snapshot of the SQLite state file, copies it to a backup location, then restarts.

Example · bash
systemctl stop k3s
cp /var/lib/rancher/k3s/server/db/state.db \
   /backup/k3s-state-$(date +%Y%m%d).db
systemctl start k3s

Take an etcd snapshot manually

Creates a named etcd snapshot on an HA cluster and lists the snapshot directory to confirm the file was created.

Example · bash
k3s etcd-snapshot save --name pre-upgrade-$(date +%F)
ls /var/lib/rancher/k3s/server/db/snapshots/

Configure automatic etcd snapshots

Configures k3s to take etcd snapshots every 6 hours, keeping the last 10, and storing them on a mounted backup drive.

Example · yaml
# /etc/rancher/k3s/config.yaml (HA server)
etcd-snapshot-schedule-cron: '0 */6 * * *'
etcd-snapshot-retention: 10
etcd-snapshot-dir: /mnt/backup/etcd-snapshots

Discussion

  • Be the first to comment on this lesson.