You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.2 KiB

4 years ago
#!/usr/bin/env bash
###################################################################
#Script Name : k8s-backup.sh
#Description : backup k8s resources.
#Create Date : 2020-11-19
#Author : lework
#Email : lework@yeah.net
###################################################################
# https://github.com/pieterlange/kube-backup/blob/master/entrypoint.sh
4 years ago
[[ -n $DEBUG ]] && set -x || true
set -o errtrace # Make sure any error trap is inherited
set -o nounset # Disallow expansion of unset variables
set -o pipefail # Use last non-zero exit code in a pipeline
######################################################################################################
# environment configuration
######################################################################################################
2 years ago
KUBECONFIG="${HOME:-'~'}/.kube/config" # kubenetes config
4 years ago
NAMESPACE="${NAMESPACE:-all}"
RESOURCES="${RESOURCES:-all}"
2 years ago
WITH_CLUSTER="true"
4 years ago
RESOURCES_PATH="/opt/k8s-backup_$(date +%s)"
######################################################################################################
# function
######################################################################################################
function get::resource() {
2 years ago
ns=${1:-cluster}
namespaced="true"
if [[ "$ns" == "cluster" ]]; then
namespaced="false"
[ -d "$RESOURCES_PATH/cluster" ] || mkdir -p "$RESOURCES_PATH/cluster"
fi
4 years ago
if [[ "${RESOURCES}" == "all" ]]; then
2 years ago
RESOURCES=$($kubectl api-resources --verbs=list --namespaced=${namespaced} -o name | grep -v "events.events.k8s.io" | grep -v "events" | sort | uniq)
4 years ago
fi
for r in ${RESOURCES}; do
4 years ago
echo "Resource:" $r
2 years ago
for l in $($kubectl -n ${ns} get --ignore-not-found ${r} -o jsonpath="{$.items[*].metadata.name}");do
$kubectl -n ${ns} get --ignore-not-found ${r} ${l} -o yaml \
4 years ago
| sed -n "/ managedFields:/{p; :a; N; / name: ${l}/!ba; s/.*\\n//}; p" \
| sed -e 's/ uid:.*//g' \
-e 's/ resourceVersion:.*//g' \
-e 's/ selfLink:.*//g' \
-e 's/ creationTimestamp:.*//g' \
-e 's/ managedFields:.*//g' \
2 years ago
-e '/^\s*$/d' > "$RESOURCES_PATH/${ns}/${l}_${r}.yaml"
4 years ago
done
done
}
4 years ago
function get::namespace() {
if [[ "${RESOURCES}" == "all" ]]; then
2 years ago
NAMESPACE=$($kubectl get ns -o jsonpath="{$.items[*].metadata.name}")
4 years ago
fi
for n in ${NAMESPACE};do
echo "Namespace:" $n
[ -d "$RESOURCES_PATH/$n" ] || mkdir -p "$RESOURCES_PATH/$n"
2 years ago
$kubectl get ns ${n} --ignore-not-found -o yaml \
4 years ago
| sed -n "/ managedFields:/{p; :a; N; / name: ${n}/!ba; s/.*\\n//}; p" \
| sed -e 's/ uid:.*//g' \
-e 's/ resourceVersion:.*//g' \
-e 's/ selfLink:.*//g' \
-e 's/ creationTimestamp:.*//g' \
-e 's/ managedFields:.*//g' \
-e '/^\s*$/d' > "$RESOURCES_PATH/${n}/namespace.yaml"
get::resource $n
done
}
function help::usage {
# 使用帮助
cat << EOF
backup k8s resource.
Usage:
$(basename $0) [flag]
Flag:
2 years ago
-c,--kubeconfig Specify kubeconfig, default is ${HOME:-~}/.kube/config
4 years ago
-ns,--namespace namespace, default: all
-r,--resource resource, default: all
2 years ago
--with-cluster cluster resource.
4 years ago
-h,--help help info.
EOF
exit 1
}
######################################################################################################
# main
######################################################################################################
while [ "${1:-}" != "" ]; do
case $1 in
2 years ago
-c | --kubeconfig ) shift
KUBECONFIG=${1:-$KUBECONFIG}
;;
4 years ago
-ns | --namespace ) shift
NAMESPACE=${1:-$NAMESPACE}
;;
-r | --resource ) shift
RESOURCES=${1:-$RESOURCES}
;;
2 years ago
--with-cluster ) shift
WITH_CLUSTER=${1:-$WITH_CLUSTER}
;;
4 years ago
-h | --help ) help::usage
;;
* ) help::usage
esac
shift
4 years ago
done
2 years ago
kubectl="kubectl --kubeconfig $KUBECONFIG"
#get::namespace
[[ "$WITH_CLUSTER" == "true" ]] && get::resource cluster || echo
4 years ago
echo "File: ${RESOURCES_PATH}"