-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetcd-backup.sh
More file actions
executable file
·50 lines (40 loc) · 1.39 KB
/
etcd-backup.sh
File metadata and controls
executable file
·50 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# etcd backup and restore — most common exam task
set -euo pipefail
BACKUP_DIR="/tmp/etcd-backups"
BACKUP_FILE="${BACKUP_DIR}/snapshot-$(date +%Y%m%d-%H%M%S).db"
ETCD_CACERT="/etc/kubernetes/pki/etcd/ca.crt"
ETCD_CERT="/etc/kubernetes/pki/etcd/server.crt"
ETCD_KEY="/etc/kubernetes/pki/etcd/server.key"
ETCD_ENDPOINT="https://127.0.0.1:2379"
mkdir -p "$BACKUP_DIR"
case "${1:-backup}" in
backup)
echo "Creating etcd snapshot..."
ETCDCTL_API=3 etcdctl snapshot save "$BACKUP_FILE" \
--endpoints="$ETCD_ENDPOINT" \
--cacert="$ETCD_CACERT" \
--cert="$ETCD_CERT" \
--key="$ETCD_KEY"
echo "Verifying snapshot..."
ETCDCTL_API=3 etcdctl snapshot status "$BACKUP_FILE" --write-table
echo "Backup saved to: $BACKUP_FILE"
;;
restore)
RESTORE_FILE="${2:?Usage: $0 restore <snapshot-file>}"
RESTORE_DIR="/var/lib/etcd-restored"
echo "Restoring from: $RESTORE_FILE"
ETCDCTL_API=3 etcdctl snapshot restore "$RESTORE_FILE" \
--data-dir="$RESTORE_DIR"
echo "Restored to: $RESTORE_DIR"
echo ""
echo "Next steps:"
echo " 1. Stop kube-apiserver: mv /etc/kubernetes/manifests/kube-apiserver.yaml /tmp/"
echo " 2. Update etcd manifest: change --data-dir to $RESTORE_DIR"
echo " 3. Restart etcd and move kube-apiserver manifest back"
;;
*)
echo "Usage: $0 {backup|restore <snapshot-file>}"
exit 1
;;
esac