Ensuring your etcd snapshots are consistent and contain up-to-date data is a critical part of disaster recovery and backup verification in a Kubernetes environment. Here's a structured approach to validate the consistency and freshness of your etcd backups after restoring them.
✅ Goals:
- Verify that etcd snapshot is restorable and consistent (integrity check).
- Verify that snapshot contains expected, current data (freshness and correctness check).
1. 🎯 Automate etcd Snapshot Restore (on isolated environment)
Restore your etcd snapshot on a dedicated VM or containerized environment. This should be done daily or with each backup.
Steps:
# Assuming your snapshot is named 'snapshot.db'
etcdctl snapshot restore snapshot.db \
--data-dir=/tmp/etcd-restore \
--name=restore-node \
--initial-cluster=restore-node=http://localhost:2380 \
--initial-advertise-peer-urls=http://localhost:2380 \
--initial-cluster-token=etcd-cluster-1 \
--advertise-client-urls=http://localhost:2379 \
--listen-client-urls=http://0.0.0.0:2379 \
--listen-peer-urls=http://0.0.0.0:2380
Then start the etcd server pointed to this restored data directory:
etcd --data-dir=/tmp/etcd-restore
2. 🔍 Check for Consistency (Post-Restore)
When you restore the snapshot, etcdctl performs integrity checks as part of the restore process. Optionally, manually verify database integrity:
etcdctl --data-dir=/tmp/etcd-restore snapshot status snapshot.db
This command will return metadata such as:
- Hash
- Size
- Revision
- Total keys
Make sure that:
- The snapshot size is reasonable (not empty).
- Revision is roughly expected.
- There are no errors during restore.
3. 🧪 Validate Against Known Test Key
Option 1: Use a Known Probe/Test Key for Verification
Before taking the snapshot, publish a test verification key into the etcd cluster during your backup job:
TIMESTAMP=$(date --iso-8601=seconds)
etcdctl put /backup/verification/key "$TIMESTAMP"
After restoring, check this key and value from the restored etcd instance:
etcdctl --endpoints=http://localhost:2379 get /backup/verification/key
Check if:
- The key exists
- The timestamp corresponds to the current snapshot date
Option 2: Compare a Set of Real Cluster Keys
Pull a small subset of "production" keys daily before backup (e.g., certain Kubernetes objects), then verify the same after restore.
Before backup:
etcdctl get /registry/pods/kube-system/kube-apiserver --print-value-only > /tmp/kube-apiserver-pod.json
After restore:
etcdctl --endpoints=http://localhost:2379 get /registry/pods/kube-system/kube-apiserver --print-value-only > /tmp/restored-kube-apiserver-pod.json
diff /tmp/kube-apiserver-pod.json /tmp/restored-kube-apiserver-pod.json
A successful restore will result in no diff.
4. 🧩 Validate Freeness (Snapshot Not Stale)
Check the latest revision of etcd after restore:
etcdctl --endpoints=http://localhost:2379 endpoint status --write-out=table
You’ll see:
- revision
- DB size
- member id
Compare the revision to what you’d expect on the live server (or log it automatically every time snapshot is taken).
Example: Save value of revision daily:
etcdctl endpoint status --write-out=json | jq '.[0].Status.header.revision'
Save this in metadata for the backup, and compare it post-restore.
5. 🛠 Optional: Script the Whole Process
You can include this in a CI/CD pipeline or daily cron job.
🔧 Example pseudocode:
# Run on live Kubernetes etcd cluster
etcdctl put /backup/verification/key "$(date +%s)"
etcdctl snapshot save /var/backups/etcd/snapshot_$(date +%s).db
# Copy to test VM and restore, then test:
etcdctl snapshot restore snapshot_*.db ...
etcdctl get /backup/verification/key
# Check if timestamp is from today
6. 📈 Log and Alert
If:
- Restore fails
- Snapshot is stale
- Test key doesn’t match
→ Raise alert or fail job, notify team.
Use tools like:
- Prometheus + Alertmanager
- CI/CD pipelines (GitLab CI, Jenkins, etc.)
🧠 Summary
| Check | How | Why |
|---|
| Snapshot integrity | etcdctl snapshot status | Data is not corrupted |
| Restore works | etcdctl snapshot restore | Snapshot is restorable |
| Data present | etcdctl get /backup/verification/key | Snapshot contains data |
| Snapshot is fresh | Check key timestamp, or etcd revision | Not restoring old backup |
| Actual data check | Check multiple production keys | Real-world verification |
Let me know if you want a ready-made validation script for the restore process.