-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupyaml.sh
executable file
·162 lines (143 loc) · 4.41 KB
/
backupyaml.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
#!/bin/bash
DATA_TIME=`date +%Y-%m-%d-%H-%M-%S` #每次创建根据时间创建
BACKUP_DIR_BASE=/Users/maqingjie/work/k8s-backup-restore #备份的目录
CURRENT_BACKUP_DIR=${BACKUP_DIR_BASE}/${DATA_TIME} #备份的目录
NS_LIST=$(kubectl get ns|grep 'Active'|grep otel |awk '{print $1}') # 指定需要备份的namespaces,也可以使用kubectl获取所有,按需
#NS_LIST="tools"
#CONFIG_TYPE="service deploy configmap secret job cronjob replicaset daemonset statefulset"
CONFIG_TYPE="svc deploy ingress cm secret statefulsets pvc" # 指定namespaces下需要备份的资源类型,按需
CURRENT_DIR=$(cd $(dirname $0); pwd)
ingress_set="
del(
.metadata.uid,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.status
)
"
cm_set="
del(
.metadata.uid,
.metadata.annotations,
.metadata.resourceVersion,
.metadata.creationTimestamp
)
"
secret_set="
del(
.metadata.uid,
.metadata.annotations,
.metadata.resourceVersion,
.metadata.creationTimestamp
)
"
svc_set="
del(
.metadata.uid,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.annotations,
.status,
.spec.clusterIP,
.spec.clusterIPs,
.spec.internalTrafficPolicy,
.spec.ipFamilies,
.spec.ipFamilyPolicy,
.spec.sessionAffinity
)
"
deploy_set="
del(
.metadata.uid,
.metadata.generation,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.annotations,
.status,
.spec.progressDeadlineSeconds,
.spec.revisionHistoryLimit,
.spec.strategy,
.spec.template.metadata.annotations,
.spec.template.metadata.creationTimestamp
)
"
statefulset_set="
del(
.metadata.generation,
.metadata.uid,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.spec.podManagementPolicy,
.spec.revisionHistoryLimit,
.spec.template.metadata.creationTimestamp,
.spec.template.metadata.annotations,
.spec.template.spec.restartPolicy,
.spec.template.spec.schedulerName,
.spec.template.spec.securityContext,
.spec.updateStrategy,
.status
)
"
pvc_set="
del(
.metadata.annotations,
.spec.progressDeadlineSeconds,
.spec.revisionHistoryLimit,
.spec.volumeMode,
.spec.volumeName,
.spec.template.metadata.annotations,
.spec.template.metadata.creationTimestamp
)
"
function backup_k8s_to_yaml(){
for ns in ${NS_LIST};do
BACKUP_DIR_DATE=${CURRENT_BACKUP_DIR}/${ns} #备份目录,按namespace根据时间分别创建
mkdir -p ${BACKUP_DIR_DATE} && cd ${BACKUP_DIR_DATE} #创建备份目录
for type in ${CONFIG_TYPE};do
item_num=$(kubectl -n ${ns} get ${type} 2>/dev/null|wc -l) #过滤资源类型为空
if [ ${item_num} -lt 1 ];then continue;fi #包含NAME行,所以如果存在资源item_num不小于2
ITEM_LIST=$(kubectl -n ${ns} get ${type} | awk '{print $1}' | grep -v 'NAME')
for item in ${ITEM_LIST};do
file_name=${BACKUP_DIR_DATE}/${type}_${item}.yaml
case ${type} in
"cm")
set_info=${cm_set}
;;
"svc")
set_info=${svc_set}
;;
"secret")
set_info=${secret_set}
;;
"ingress")
set_info=${ingress_set}
;;
#kubectl -n ${ns} get ${type} ${item} -o=json | jq '. |${ingress_set}'|yq eval -P > ${file_name};;
"deploy")
set_info=${deploy_set}
;;
"statefulsets")
set_info=${statefulset_set}
;;
"pvc")
set_info=${pvc_set}
;;
esac
kubectl -n ${ns} get ${type} ${item} -o=json | jq --args "${set_info}" '.|${set_info}'| yq eval -P > ${file_name}
[[ $? -ne 0 ]] && exit
done
done
done
}
function archive_and_upload(){
archive_file_name=k8s-backup-${DATA_TIME}.tar.gz
cd ${BACKUP_DIR_BASE} && tar -jcf ${archive_file_name} ${DATA_TIME} &> /dev/null
if [[ -s ${archive_file_name} ]];then
/usr/bin/python3 ${CURRENT_DIR}/oss_uoload.py k8s_backup/${archive_file_name} ${BACKUP_DIR_BASE}/${archive_file_name}
fi
}
function main(){
backup_k8s_to_yaml;
}
main;