|
|
@ -8,11 +8,32 @@ |
|
|
|
################################################################### |
|
|
|
################################################################### |
|
|
|
# https://github.com/pieterlange/kube-backup/blob/master/entrypoint.sh |
|
|
|
# https://github.com/pieterlange/kube-backup/blob/master/entrypoint.sh |
|
|
|
|
|
|
|
|
|
|
|
resources_path="./backup-$(date +%s)" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getall { |
|
|
|
[[ -n $DEBUG ]] && set -x || true |
|
|
|
ns=$1 |
|
|
|
set -o errtrace # Make sure any error trap is inherited |
|
|
|
for r in $(kubectl api-resources --verbs=list --namespaced -o name | grep -v "events.events.k8s.io" | grep -v "events" | sort | uniq); do |
|
|
|
set -o nounset # Disallow expansion of unset variables |
|
|
|
|
|
|
|
set -o pipefail # Use last non-zero exit code in a pipeline |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###################################################################################################### |
|
|
|
|
|
|
|
# environment configuration |
|
|
|
|
|
|
|
###################################################################################################### |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NAMESPACE="${NAMESPACE:-all}" |
|
|
|
|
|
|
|
RESOURCES="${RESOURCES:-all}" |
|
|
|
|
|
|
|
RESOURCES_PATH="/opt/k8s-backup_$(date +%s)" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###################################################################################################### |
|
|
|
|
|
|
|
# function |
|
|
|
|
|
|
|
###################################################################################################### |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function get::resource() { |
|
|
|
|
|
|
|
ns=$1 |
|
|
|
|
|
|
|
if [[ "${RESOURCES}" == "all" ]]; then |
|
|
|
|
|
|
|
RESOURCES=$(kubectl api-resources --verbs=list --namespaced -o name | grep -v "events.events.k8s.io" | grep -v "events" | sort |uniq) |
|
|
|
|
|
|
|
fi |
|
|
|
|
|
|
|
for r in ${RESOURCES}; do |
|
|
|
echo "Resource:" $r |
|
|
|
echo "Resource:" $r |
|
|
|
for l in $(kubectl -n ${ns} get --ignore-not-found ${r} -o jsonpath="{$.items[*].metadata.name}");do |
|
|
|
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 \ |
|
|
|
kubectl -n ${ns} get --ignore-not-found ${r} ${l} -o yaml \ |
|
|
@ -22,23 +43,69 @@ function getall { |
|
|
|
-e 's/ selfLink:.*//g' \ |
|
|
|
-e 's/ selfLink:.*//g' \ |
|
|
|
-e 's/ creationTimestamp:.*//g' \ |
|
|
|
-e 's/ creationTimestamp:.*//g' \ |
|
|
|
-e 's/ managedFields:.*//g' \ |
|
|
|
-e 's/ managedFields:.*//g' \ |
|
|
|
-e '/^\s*$/d' > "$resources_path/${n}/${l}.${r}.yaml" |
|
|
|
-e '/^\s*$/d' > "$RESOURCES_PATH/${n}/${l}.${r}.yaml" |
|
|
|
done |
|
|
|
done |
|
|
|
done |
|
|
|
done |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for n in $(kubectl get ns -o jsonpath="{$.items[*].metadata.name}");do |
|
|
|
function get::namespace() { |
|
|
|
echo "Namespace:" $n |
|
|
|
if [[ "${RESOURCES}" == "all" ]]; then |
|
|
|
[ -d "$resources_path/$n" ] || mkdir -p "$resources_path/$n" |
|
|
|
NAMESPACE=$(kubectl get ns -o jsonpath="{$.items[*].metadata.name}") |
|
|
|
kubectl get ns ${n} --ignore-not-found -o yaml \ |
|
|
|
fi |
|
|
|
| sed -n "/ managedFields:/{p; :a; N; / name: ${n}/!ba; s/.*\\n//}; p" \ |
|
|
|
for n in ${NAMESPACE};do |
|
|
|
| sed -e 's/ uid:.*//g' \ |
|
|
|
echo "Namespace:" $n |
|
|
|
-e 's/ resourceVersion:.*//g' \ |
|
|
|
[ -d "$RESOURCES_PATH/$n" ] || mkdir -p "$RESOURCES_PATH/$n" |
|
|
|
-e 's/ selfLink:.*//g' \ |
|
|
|
kubectl get ns ${n} --ignore-not-found -o yaml \ |
|
|
|
-e 's/ creationTimestamp:.*//g' \ |
|
|
|
| sed -n "/ managedFields:/{p; :a; N; / name: ${n}/!ba; s/.*\\n//}; p" \ |
|
|
|
-e 's/ managedFields:.*//g' \ |
|
|
|
| sed -e 's/ uid:.*//g' \ |
|
|
|
-e '/^\s*$/d' > "$resources_path/${n}/namespace.yaml" |
|
|
|
-e 's/ resourceVersion:.*//g' \ |
|
|
|
getall $n |
|
|
|
-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: |
|
|
|
|
|
|
|
-ns,--namespace namespace, default: all |
|
|
|
|
|
|
|
-r,--resource resource, default: all |
|
|
|
|
|
|
|
-h,--help help info. |
|
|
|
|
|
|
|
EOF |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exit 1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###################################################################################################### |
|
|
|
|
|
|
|
# main |
|
|
|
|
|
|
|
###################################################################################################### |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while [ "${1:-}" != "" ]; do |
|
|
|
|
|
|
|
case $1 in |
|
|
|
|
|
|
|
-ns | --namespace ) shift |
|
|
|
|
|
|
|
NAMESPACE=${1:-$NAMESPACE} |
|
|
|
|
|
|
|
;; |
|
|
|
|
|
|
|
-r | --resource ) shift |
|
|
|
|
|
|
|
RESOURCES=${1:-$RESOURCES} |
|
|
|
|
|
|
|
;; |
|
|
|
|
|
|
|
-h | --help ) help::usage |
|
|
|
|
|
|
|
;; |
|
|
|
|
|
|
|
* ) help::usage |
|
|
|
|
|
|
|
esac |
|
|
|
|
|
|
|
shift |
|
|
|
done |
|
|
|
done |
|
|
|
|
|
|
|
|
|
|
|
echo "File: ${resources_path}" |
|
|
|
get::namespace |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
echo "File: ${RESOURCES_PATH}" |
|
|
|