-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-script-cm.yaml
95 lines (76 loc) · 3.06 KB
/
backup-script-cm.yaml
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
kind: ConfigMap
apiVersion: v1
metadata:
name: etcd-backup-script
data:
etcd-backup.sh: |
#!/usr/bin/env bash
### Created by cluster-etcd-operator. DO NOT edit.
set -o errexit
set -o pipefail
set -o errtrace
# example
# cluster-backup.sh $path-to-snapshot
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
function usage {
echo 'Path to backup dir required: ./cluster-backup.sh <path-to-backup-dir>'
exit 1
}
# If the first argument is missing, or it is an existing file, then print usage and exit
if [ -z "$1" ] || [ -f "$1" ]; then
usage
fi
if [ ! -d "$1" ]; then
mkdir -p "$1"
fi
# backup latest static pod resources
function backup_latest_kube_static_resources {
RESOURCES=("$@")
LATEST_RESOURCE_DIRS=()
for RESOURCE in "${RESOURCES[@]}"; do
# shellcheck disable=SC2012
LATEST_RESOURCE=$(ls -trd "${CONFIG_FILE_DIR}"/static-pod-resources/"${RESOURCE}"-[0-9]* | tail -1) || true
if [ -z "$LATEST_RESOURCE" ]; then
echo "error finding static-pod-resource ${RESOURCE}"
exit 1
fi
echo "found latest ${RESOURCE}: ${LATEST_RESOURCE}"
LATEST_RESOURCE_DIRS+=("${LATEST_RESOURCE#${CONFIG_FILE_DIR}/}")
done
# tar latest resources with the path relative to CONFIG_FILE_DIR
tar -cpzf "$BACKUP_TAR_FILE" -C "${CONFIG_FILE_DIR}" "${LATEST_RESOURCE_DIRS[@]}"
chmod 600 "$BACKUP_TAR_FILE"
}
function source_required_dependency {
local path="$1"
if [ ! -f "${path}" ]; then
echo "required dependencies not found, please ensure this script is run on a node with a functional etcd static pod"
exit 1
fi
# shellcheck disable=SC1090
source "${path}"
}
BACKUP_DIR="$1"
DATESTRING=$(date "+%F_%H%M%S")
BACKUP_TAR_FILE=${BACKUP_DIR}/static_kuberesources_${DATESTRING}.tar.gz
SNAPSHOT_FILE="${BACKUP_DIR}/snapshot_${DATESTRING}.db"
BACKUP_RESOURCE_LIST=("kube-apiserver-pod" "kube-controller-manager-pod" "kube-scheduler-pod" "etcd-pod")
trap 'rm -f ${BACKUP_TAR_FILE} ${SNAPSHOT_FILE}' ERR
source_required_dependency /etc/kubernetes/static-pod-resources/etcd-certs/configmaps/etcd-scripts/etcd.env
source_required_dependency /etc/kubernetes/static-pod-resources/etcd-certs/configmaps/etcd-scripts/etcd-common-tools
# TODO handle properly
if [ ! -f "$ETCDCTL_CACERT" ] && [ ! -d "${CONFIG_FILE_DIR}/static-pod-certs" ]; then
ln -s "${CONFIG_FILE_DIR}"/static-pod-resources/etcd-certs "${CONFIG_FILE_DIR}"/static-pod-certs
fi
IP_ADDR=$(hostname -i)
#dl_etcdctl
backup_latest_kube_static_resources "${BACKUP_RESOURCE_LIST[@]}"
ETCDCTL_ENDPOINTS="https://${IP_ADDR}:2379" etcdctl snapshot save "${SNAPSHOT_FILE}"
echo "snapshot db and kube resources are successfully saved to ${BACKUP_DIR}"
#Tar
tar -cpzf ${BACKUP_DIR}.${DATESTRING}.tar.gz ${BACKUP_DIR}
echo "backup available as ${BACKUP_DIR}.${DATESTRING}.tar.gz"
rm -fr ${BACKUP_DIR}