lework 4 years ago
parent
commit
18fd8dc028
  1. 13
      shell/k8s/k8s-app-info.sh
  2. 85
      shell/k8s/k8s-backup.sh

13
shell/k8s/k8s-app-info.sh

@ -65,7 +65,16 @@ function get::selector {
fi fi
if [[ "${SELECTOR}" == "" ]]; then if [[ "${SELECTOR}" == "" ]]; then
echo -e "\033[0;31m[Error] not found $APPNAME selector\033[0m" resource="service job cronjob replicaset daemonset statefulset"
for r in $resource
do
SELECTOR=$(kubectl -n kube-system get ${r} ${APPNAME} --ignore-not-found --show-labels --no-headers 2>/dev/null | awk '{print $NF}' | grep -v '<none>' |head -1)
if [[ "${SELECTOR}" != "" ]]; then break;fi
done
fi
if [[ "${SELECTOR}" == "" ]]; then
echo -e "\n\033[0;31m[Error] not found $APPNAME selector.\033[0m"
exit 1 exit 1
fi fi
file::write " file::write "
@ -134,6 +143,8 @@ function get::info {
get::describe pod get::describe pod
get::describe configmaps get::describe configmaps
get::describe secrets get::describe secrets
get::describe pvc
get::describe pv
get::pods_log get::pods_log
get::k8s_event get::k8s_event
get::cluster get::cluster

85
shell/k8s/k8s-backup.sh

@ -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
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
######################################################################################################
NAMESPACE="${NAMESPACE:-all}"
RESOURCES="${RESOURCES:-all}"
RESOURCES_PATH="/opt/k8s-backup_$(date +%s)"
######################################################################################################
# function
######################################################################################################
function get::resource() {
ns=$1 ns=$1
for r in $(kubectl api-resources --verbs=list --namespaced -o name | grep -v "events.events.k8s.io" | grep -v "events" | sort | uniq); do 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,14 +43,18 @@ 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() {
if [[ "${RESOURCES}" == "all" ]]; then
NAMESPACE=$(kubectl get ns -o jsonpath="{$.items[*].metadata.name}")
fi
for n in ${NAMESPACE};do
echo "Namespace:" $n echo "Namespace:" $n
[ -d "$resources_path/$n" ] || mkdir -p "$resources_path/$n" [ -d "$RESOURCES_PATH/$n" ] || mkdir -p "$RESOURCES_PATH/$n"
kubectl get ns ${n} --ignore-not-found -o yaml \ kubectl get ns ${n} --ignore-not-found -o yaml \
| sed -n "/ managedFields:/{p; :a; N; / name: ${n}/!ba; s/.*\\n//}; p" \ | sed -n "/ managedFields:/{p; :a; N; / name: ${n}/!ba; s/.*\\n//}; p" \
| sed -e 's/ uid:.*//g' \ | sed -e 's/ uid:.*//g' \
@ -37,8 +62,50 @@ for n in $(kubectl get ns -o jsonpath="{$.items[*].metadata.name}");do
-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}/namespace.yaml" -e '/^\s*$/d' > "$RESOURCES_PATH/${n}/namespace.yaml"
getall $n get::resource $n
done 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
get::namespace
echo "File: ${resources_path}" echo "File: ${RESOURCES_PATH}"

Loading…
Cancel
Save