Files
dotfiles/scripts/cloudflare-ddns.sh
T
2026-07-02 21:29:51 +01:00

34 lines
1023 B
Bash

#!@bash@/bin/bash
source /run/secrets/main.env
cache=/var/lib/cloudflare-ddns/old_ip
IP=$(@curl@/bin/curl -s 'https://api.ipify.org')
arr=("" "git." "www.")
if [[ -f "$cache" ]]; then
old_ip=$(cat "$cache")
else
old_ip=""
fi
if [[ "$IP" != "$old_ip" ]]; then
for sub in "${arr[@]}"; do
RECORD_ID=$(@curl@/bin/curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
| @jq@/bin/jq -r ".result[] | select(.name==\"${sub}syedm.dev\" and .type==\"A\") | .id")
if [ -n "$RECORD_ID" ]; then
@curl@/bin/curl -s -X PUT \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"type\":\"A\",\"name\":\"${sub}syedm.dev\",\"content\":\"$IP\",\"ttl\":3600,\"proxied\":false}"
fi
done
echo "$IP" > "$cache"
fi