Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes Cluster - etcd Backup and Restore

Back


Backup


Backup Resource Configuration


Backup etcd


Backup Steps


Imperative Commands

Command Description
etcdctl version Get etcd version
etcdctl snapshot save DB_FILE Backup a snapshot to a db file
etcdctl snapshot status DB_FILE Show status of a db file
etcdctl snapshot restore DB_FILE --data-dir=PATH Restores an etcd member snapshot to a directory

etcdctl version

# Backing Up ETCD
# Using etcdctl (Snapshot-based Backup)
# To take a snapshot from a running etcd server, use:
ETCDCTL_API=3 etcdctl \
  --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/kubernetes/pki/etcd/ca.crt \
  --cert=/etc/kubernetes/pki/etcd/server.crt \
  --key=/etc/kubernetes/pki/etcd/server.key \
  snapshot save /opt/snapshot-pre-boot.db

Required Options –endpoints points to the etcd server (default: localhost:2379)

–cacert path to the CA cert

–cert path to the client cert

–key path to the client key

Using etcdutl (File-based Backup) For offline file-level backup of the data directory:

etcdutl backup
–data-dir /var/lib/etcd
–backup-dir /backup/etcd-backup This copies the etcd backend database and WAL files to the target location.

Checking Snapshot Status You can inspect the metadata of a snapshot file using:

etcdctl snapshot status /backup/etcd-snapshot.db
–write-out=table This shows details like size, revision, hash, total keys, etc. It is helpful to verify snapshot integrity before restore.

Restoring ETCD Using etcdutl To restore a snapshot to a new data directory:

etcdutl snapshot restore /opt/snapshot-pre-boot.db –data-dir /var/lib/etcd-restored

To use a backup made with etcdutl backup, simply copy the backup contents back into /var/lib/etcd and restart etcd.

Notes etcdctl snapshot save is used for creating .db snapshots from live etcd clusters.

etcdctl snapshot status provides metadata information about the snapshot file.

etcdutl snapshot restore is used to restore a .db snapshot file.

etcdutl backup performs a raw file-level copy of etcd’s data and WAL files without needing etcd to be running.





https://kubernetes.io/docs/tasks/administer-cluster/configure-upgrade-etcd/#backing-up-an-etcd-cluster

https://github.com/etcd-io/website/blob/main/content/en/docs/v3.5/op-guide/recovery.md

https://www.youtube.com/watch?v=qRPNuT080Hk



---

## Install

- Install Go

```sh
sudo apt install golang

go version
# go version go1.23.8 linux/amd64

??

sudo apt install etcd-client
git clone -b v3.4.37 https://github.com/etcd-io/etcd.git
cd etcd

# Run the build script
./build

# Add the full path
sudo export PATH="$PATH:`pwd`/bin"

etcd --version
# WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
#         A future release of golang/protobuf will delete this package,
#         which has long been excluded from the compatibility promise.

# etcd Version: 3.4.37
# Git SHA: 1a36b4853
# Go Version: go1.23.8
# Go OS/Arch: linux/amd64

etcdctl version
# etcdctl version: 3.4.37
# API version: 3.4