Created
January 21, 2020 19:30
-
-
Save shagamemnon/f3aecce00e192cfd9282dc7dc2bd1ee8 to your computer and use it in GitHub Desktop.
Enable log file retention for a list of Cloudflare (Enterprise) zones.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Enable log retention for a list of Cloudflare zones | |
AUTH_EMAIL="xxxx@example.com" # the account you use to | |
AUTH_KEY="xxxxxxxxxxxx" # https://dash.cloudflare.com/profile/api-tokens --> "Global API Key" | |
ZONE_NAMES="franktaylor.io cfiq.io valleynet.io" # space-separated list of zone names | |
for ZONE in $ZONE_NAMES | |
do | |
printf "\n" | |
ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE" -H "X-Auth-Email: $AUTH_EMAIL" -H "X-Auth-Key: $AUTH_KEY" -H "Content-Type: application/json" | sed 's/,/\n/g' | awk -F'"' '/id/{print $6}' | head -1) | |
get_log_retention_setting=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/logs/control/retention/flag" -H "X-Auth-Email: $AUTH_EMAIL" -H "X-Auth-Key: $AUTH_KEY" -H "Content-Type: application/json") | |
current_log_retention_setting=$(echo "$get_log_retention_setting") | |
if [[ -z ${current_log_retention_setting##*'"success":true'*} ]]; then | |
printf "Log retention already enabled for $ZONE\n:" | |
printf "$current_log_retention_setting\n\n" | |
else | |
update=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/logs/control/retention/flag" -H "X-Auth-Email: $AUTH_EMAIL" -H "X-Auth-Key: $AUTH_KEY" -H "Content-Type: application/json" --data "{\"flag\":true}") | |
printf "$update\n" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment