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

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

@ -65,7 +65,16 @@ function get::selector { @@ -65,7 +65,16 @@ function get::selector {
fi
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
fi
file::write "
@ -134,6 +143,8 @@ function get::info { @@ -134,6 +143,8 @@ function get::info {
get::describe pod
get::describe configmaps
get::describe secrets
get::describe pvc
get::describe pv
get::pods_log
get::k8s_event
get::cluster

101
shell/k8s/k8s-backup.sh

@ -8,11 +8,32 @@ @@ -8,11 +8,32 @@
###################################################################
# 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
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
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 \
@ -22,23 +43,69 @@ function getall { @@ -22,23 +43,69 @@ function getall {
-e 's/ selfLink:.*//g' \
-e 's/ creationTimestamp:.*//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
}
for n in $(kubectl get ns -o jsonpath="{$.items[*].metadata.name}");do
echo "Namespace:" $n
[ -d "$resources_path/$n" ] || mkdir -p "$resources_path/$n"
kubectl get ns ${n} --ignore-not-found -o yaml \
| 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"
getall $n
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
[ -d "$RESOURCES_PATH/$n" ] || mkdir -p "$RESOURCES_PATH/$n"
kubectl get ns ${n} --ignore-not-found -o yaml \
| 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:
-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
echo "File: ${resources_path}"
get::namespace
echo "File: ${RESOURCES_PATH}"

Loading…
Cancel
Save