This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·183 lines (174 loc) · 6.5 KB
/
backup.sh
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
#!/usr/bin/env bash
# set -x
function backup_name() {
echo "backup-`date +%Y-%m-%d.%H.%M`"
}
#Function for backup all resources in all namespaces
function backup_cluster_all() {
folderName=$(backup_name)
mkdir -p ./$folderName
kubectl get --export -o=json ns | \
jq '.items[] |
del(.status,
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation
)' > ./$folderName/ns.json
#Backup roles and related resources
for ns in $(jq -r '.metadata.name' < ./$folderName/ns.json);do
echo "Creating backup of roles and bindings in: $ns"
kubectl --namespace="${ns}" get --export -o=json roles,rolebindings,clusterroles,clusterrolebindings,sa | \
jq '.items[] |
select(.type!="kubernetes.io/service-account-token") |
del(
.spec.clusterIP, # clusterIP is dynamically assigned
.spec.claimRef, # Throw this out so we can rebind
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.spec.template.spec.securityContext,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.template.spec.restartPolicy,
.spec?.ports[]?.nodePort? # Delete nodePort from service since this is dynamic
) |
# Set reclaim policy to retain so we can recover volumes
if .kind == "PersistentVolume" then
.spec.persistentVolumeReclaimPolicy = "Retain"
else
.
end' >> ./$folderName/rolesAndBindings.json
done
#Backup workloads
for ns in $(jq -r '.metadata.name' < ./$folderName/ns.json);do
echo "Creating backup of workloads in: $ns"
kubectl --namespace="${ns}" get --export -o=json cronjobs,jobs,ing,svc,rc,secrets,ds,cm,deploy,sts,hpa,pv,pvc,quota,limits,storageclass | \
jq '.items[] |
select(.type!="kubernetes.io/service-account-token") |
del(
.spec.clusterIP, # clusterIP is dynamically assigned
.spec.claimRef, # Throw this out so we can rebind
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.spec.template.spec.securityContext,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.template.spec.restartPolicy,
.spec?.ports[]?.nodePort? # Delete nodePort from service since this is dynamic
) |
# Set reclaim policy to retain so we can recover volumes
if .kind == "PersistentVolume" then
.spec.persistentVolumeReclaimPolicy = "Retain"
else
.
end' >> ./$folderName/workloads.json
done
echo "Use "./$folderName/ns.json" to restore all namespaces into cluster"
echo "Use "./$folderName/rolesAndBindings.json" to restore all workloads into cluster"
echo "Use "./$folderName/workloads.json" to restore all workloads into cluster"
exit 0
}
#For namespace backup
function backup_namespace {
folderName=$(backup_name)
mkdir -p ./$folderName
ns=(${1})
for ns in "${ns[@]}"; do
# echo $ns
kubectl get --export -o=json ns $ns | \
jq 'del(.status,
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation
)' | tee ./$folderName/ns.json >> ./$folderName/namespaces.json
done
#Backup roles and related resources
for ns in $(jq -r '.metadata.name' < ./$folderName/ns.json);do
echo "Creating backup of roles and bindings in: $ns"
kubectl --namespace="${ns}" get --export -o=json roles,rolebindings,clusterroles,clusterrolebindings,sa | \
jq '.items[] |
select(.type!="kubernetes.io/service-account-token") |
del(
.spec.clusterIP, # clusterIP is dynamically assigned
.spec.claimRef, # Throw this out so we can rebind
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.spec.template.spec.securityContext,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.template.spec.restartPolicy,
.spec?.ports[]?.nodePort? # Delete nodePort from service since this is dynamic
) |
# Set reclaim policy to retain so we can recover volumes
if .kind == "PersistentVolume" then
.spec.persistentVolumeReclaimPolicy = "Retain"
else
.
end' >> ./$folderName/rolesAndBindings.json
done
#Backup workloads
for ns in $(jq -r '.metadata.name' < ./$folderName/ns.json);do
echo "Creating backup of workloads in: $ns"
kubectl --namespace="${ns}" get --export -o=json cronjobs,jobs,ing,svc,rc,secrets,ds,cm,deploy,sts,hpa,pv,pvc,quota,limits,storageclass | \
jq '.items[] |
select(.type!="kubernetes.io/service-account-token") |
del(
.spec.clusterIP, # clusterIP is dynamically assigned
.spec.claimRef, # Throw this out so we can rebind
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.spec.template.spec.securityContext,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.template.spec.restartPolicy,
.spec?.ports[]?.nodePort? # Delete nodePort from service since this is dynamic
) |
# Set reclaim policy to retain so we can recover volumes
if .kind == "PersistentVolume" then
.spec.persistentVolumeReclaimPolicy = "Retain"
else
.
end' >> ./$folderName/workloads.json
done
rm ./$folderName/ns.json #<--For proper order
}
function main(){
while getopts ":an:" opt; do
case $opt in
a)
backup_cluster_all
exit 0
;;
n)
backup_namespace "${OPTARG}"
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
\?)
cat <<EOF
You should choose which namespace or namespaces with their related resource will be backed up
Usage:
cluster-ops.sh backupCluster [args]
Namespace Based Commands:
-n, (-n $namespace_name -n $namespacename) Set namespace to backup all it's related resources
-a Backup all namespaces and all related resources
EOF
exit 1
;;
esac
done
}
main "$@"