Files
infrastructure/clusters/cl01tl/helm/vault/templates/config-map.yaml
Alex Lebens 8f8537c31d
All checks were successful
lint-test-helm / lint-helm (push) Successful in 16s
render-manifests-push / render-manifests-push (push) Successful in 38s
renovate / renovate (push) Successful in 1m16s
fix loop
2025-12-19 23:02:30 -06:00

99 lines
3.0 KiB
YAML

apiVersion: v1
kind: ConfigMap
metadata:
name: vault-snapshot-script
namespace: {{ .Release.Namespace }}
labels:
app.kubernetes.io/name: vault-snapshot-script
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/part-of: {{ .Release.Name }}
data:
snapshot.sh: |
SUCCESS=false
for i in {1..5}; do
if apk update --short &> /dev/null; then
echo ">> Attempt $i: Repositories are reachable";
SUCCESS=true;
break;
else
echo ">> Attempt $i: Connection failed, retrying in 5 seconds ...";
sleep 5;
fi;
done;
if [ "$SUCCESS" = false ]; then
echo ">> ERROR: Could not connect to apk repositories after $MAX_RETRIES attempts, exiting ...";
exit 1;
fi
if ! command -v jq 2>&1 >/dev/null;
then
echo "jq could not be found, installing";
apk add --no-cache jq;
if [ $? -eq 0 ]; then
echo ">> Installation successful";
else
echo ">> Installation failed with exit code $?";
exit 1;
fi;
fi;
echo " ";
echo ">> Fetching Vault token";
export VAULT_TOKEN=$(vault write auth/approle/login role_id=$VAULT_APPROLE_ROLE_ID secret_id=$VAULT_APPROLE_SECRET_ID -format=json | jq -r .auth.client_token);
echo " ";
echo ">> Taking Vault snapsot ...";
vault operator raft snapshot save /opt/backup/vault-snapshot-$(date +"%Y%m%d-%H-%M").snap
echo " ";
echo ">> Completed Vault snapshot";
---
apiVersion: v1
kind: ConfigMap
metadata:
name: vault-backup-script
namespace: {{ .Release.Namespace }}
labels:
app.kubernetes.io/name: vault-backup-script
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/part-of: {{ .Release.Name }}
data:
backup.sh: |
echo " ";
echo ">> Running S3 backup for Vault snapshot";
OUTPUT=$(s3cmd sync --no-check-certificate -v /opt/backup "${BUCKET}/cl01tl/cl01tl-vault-snapshots/" 2>&1)
STATUS=$?
echo " ";
if [ $STATUS -ne 0 ]; then
if echo "$OUTPUT" | grep -q "403 Forbidden"; then
MESSAGE="403 Authentication Error: Your keys are wrong or you don't have permission"
elif echo "$OUTPUT" | grep -q "404 Not Found"; then
MESSAGE="404 Error: The bucket or folder does not exist"
elif echo "$OUTPUT" | grep -q "Connection refused"; then
MESSAGE="Network Error: Cannot reach the S3 endpoint"
else
MESSAGE="Unknown Error: $OUTPUT"
echo ">> Unknown Error, output:"
echo " "
echo "$OUTPUT"
echo " "
fi
echo ">> Message: $MESSAGE"
echo ">> Sending to NTFY ..."
curl \
-H "Authorization: Bearer ${NTFY_TOKEN}" \
-H "X-Priority: 5" \
-H "X-Tags: warning" \
-H "X-Title: Vault Backup Failed for ${TARGET}" \
-d "$MESSAGE" \
${NTFY_ENDPOINT}/${NTFY_TOPIC}
else
echo ">> S3 Sync succeeded"
fi