-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtal-backup-etcd
More file actions
executable file
·228 lines (206 loc) · 4.91 KB
/
Copy pathtal-backup-etcd
File metadata and controls
executable file
·228 lines (206 loc) · 4.91 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
#
# tal-backup-etcd
#
# Back up the talos cluster's etcd cluster
#
# Copyright 2026, Joe Block <jpb@unixorn.net>
set -o pipefail
if [[ -n "$DEBUG" ]]; then
# shellcheck disable=SC2086
if [[ "$(echo $DEBUG | tr '[:upper:]' '[:lower:]')" == "verbose" ]]; then
set -x
fi
fi
function echo-stderr() {
printf '%s
' "$1" >&2 ## Send message to stderr. Exclude >&2 if you don't want it that way.
}
function debug() {
if [[ -n "$DEBUG" ]]; then
echo-stderr "$@"
fi
}
function verbose() {
if [[ -n "$VERBOSE" ]]; then
echo "$@"
fi
}
function fail() {
echo-stderr "$1"
exit "${2-1}" ## Return a code specified by $2 or 1 by default.
}
function has() {
# Check if a command is in $PATH
which "$@" > /dev/null 2>&1
}
function get-default-settings() {
BACKUP_D=${BACKUP_D:-'./talos-backups'}
KEEP_DAYS=${KEEP_DAYS:-'30'}
PRUNE_OK="true"
TALOSCONFIG=${TALOSCONFIG:-"$HOME/.talos/config"}
}
function check-dependency() {
if ! (builtin command -V "$1" >/dev/null 2>&1); then
fail "missing dependency: can't find $1 in your PATH"
fi
}
function check-dependencies() {
verbose "Checking dependencies..."
# shellcheck disable=SC2041
# Placeholders for whatever programs you really need
for dep in "$@"
do
if ! has "$dep"; then
fail "Can't find $dep in your $PATH"
else
debug "- Found $dep"
fi
done
}
function my-name() {
basename "$0"
}
function usage() {
echo "Usage: $(my-name)"
echo " --backup-directory /path/to/directory"
echo " --backup-filename - What filename to write in the backup directory. Defaults to etcd.snapshot.$(date +%Y-%m-%d-%H%M)"
echo " --controlplane-node IP_OR_DNS_OF_CONTROLPLANE_NODE"
echo " --keep-days (default 30) how many days of backup to keep"
echo " --no-prune - Disable stale backup pruning"
echo " --talosconfig /path/to/talosconfig"
echo " --verbose"
}
function pingable() {
verbose "Checking connectivity to $*"
# shellcheck disable=SC2068
if ! ping -c 5 $@ > /dev/null 2>&1; then
debug "Could not ping $*"
return 1
else
return 0
fi
}
function dump-settings() {
verbose "BACKUP_D: ${BACKUP_D}"
verbose "BACKUP_F: ${BACKUP_F}"
verbose "CONTROLPLANE_IP: ${CONTROLPLANE_IP}"
verbose "KEEP_DAYS: ${KEEP_DAYS}"
verbose "PRUNE_OK: ${PRUNE_OK}"
verbose "TALOSCONFIG: ${TALOSCONFIG}"
}
function squish() {
if has pbzip2; then
pbzip2 -9vr "$1"
else
bzip2 -9v "$1"
fi
}
function backup-etcd() {
mkdir -p "${BACKUP_D}" || fail "Can't make ${BACKUP_D}"
if [[ -z "${CONTROLPLANE_IP}" ]]; then
verbose "Backing up etcd to ${BACKUP_D}/${BACKUP_F}"
if ! talosctl --talosconfig "${TALOSCONFIG}" etcd snapshot "${BACKUP_D}/${BACKUP_F}"; then
fail "etcd snapshot failed! skipping backup pruning"
fi
else
verbose "Backing up etcd from ${CONTROLPLANE_IP} to ${BACKUP_D}/${BACKUP_F}"
if ! pingable "${CONTROLPLANE_IP}";then
fail "Can't ping ${CONTROLPLANE_IP}!"
fi
if ! talosctl --talosconfig "${TALOSCONFIG}" -n "${CONTROLPLANE_IP}" etcd snapshot "${BACKUP_D}/${BACKUP_F}"; then
fail "etcd snapshot failed! skipping backup pruning"
fi
fi
squish "${BACKUP_D}/${BACKUP_F}"
}
function prune-backups() {
if [[ "$PRUNE_OK" == "true" ]]; then
echo "Pruning stale backups older than ${KEEP_DAYS} days old..."
# Delete snapshots older than $KEEP_DAYS days
find "${BACKUP_D}" -mtime +"${KEEP_DAYS}" -type f -exec rm -fv '{}' ';'
fi
}
function run-cluster-health-checks() {
echo "Confirming cluster health..."
talosctl health
}
# If you run this after parsing arguments, CLI arguments will get replaced
# with the defaults
get-default-settings
# parse arguments
while :
do
if [[ $# == 0 ]]; then
break
fi
case "$1" in
--help | -h)
usage
exit 0
;;
--talosconfig)
shift
TALOSCONFIG="$1"
shift
;;
--backup-directory | --backup-dir | --bd )
shift
BACKUP_D="$1"
shift
;;
--backup-filename)
shift
BACKUP_F="$1"
shift
;;
--controlplane-node | --control-plane-node | --control-plane | --cpn )
shift
CONTROLPLANE_IP="$1"
shift
;;
--keep-days | --days)
shift
KEEP_DAYS="$1"
shift
;;
--no-prune)
PRUNE_OK='false'
shift
;;
--debug)
DEBUG='true'
shift
;;
--dry-run)
DRY_RUN='true'
shift
;;
--verbose | -v)
VERBOSE='true'
shift
;;
-- )
shift;
break
;;
*)
echo-stderr "Unexpected option: $1"
usage
fail "Invalid CLI argument: '$1'"
;;
esac
done
BACKUP_F=${BACKUP_F:-"etcd.snapshot.$(date +%Y-%m-%d-%H%M)"}
dump-settings
if [[ ! -r "${TALOSCONFIG}" ]]; then
fail "Can't read ${TALOSCONFIG}!"
fi
if [[ -n "$DRY_RUN" ]]; then
exit 0
fi
dump-settings
check-dependencies bzip2 find talosctl
run-cluster-health-checks
backup-etcd
prune-backups