Skip to content

Instantly share code, notes, and snippets.

@vincent-zurczak
Last active March 30, 2020 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincent-zurczak/5171dac2b930b4ac719494182d5ed36e to your computer and use it in GitHub Desktop.
Save vincent-zurczak/5171dac2b930b4ac719494182d5ed36e to your computer and use it in GitHub Desktop.
Backup K8s secrets (to store in some external vault, e.g. in case of corrupted cluster)
mkdir -p /tmp/backup
cd /tmp/backup
# Only backup "opaque" secrets
# (not those for service accounts, etc).
#
# To back up them all, use the following query:
# kubectl get secrets -o json | jq -r '.items[].metadata.name'
for secret in $(kubectl get secrets -o json | jq -r '.items[] | select(.type == "Opaque") | .metadata.name'); do
echo "Backing up ${secret}..."
# --export is deprecated (and was not perfect)
# https://github.com/kubernetes/kubernetes/pull/73787
kubectl get secret "${secret}" -o yaml | \
sed '/namespace: /d' | \
sed '/resourceVersion: /d' | \
sed '/selfLink: /d' | \
sed '/uid: /d' | \
sed '/creationTimestamp: /d' | \
sed '/field.cattle.io/d' > "${secret}".yaml
done
the_date=`date '+%Y-%m-%d--%H-%M-%S'`
tar -czvf "secrets-backup-${the_date}.tar.gz" *.yaml
mkdir restore
tar -C restore -xvf secrets-backup-some_date.tar.gz
kubectl create -f restore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment