summaryrefslogtreecommitdiff
path: root/update.sh
diff options
context:
space:
mode:
authorSteph Enders <steph@senders.io>2023-07-05 10:40:37 -0400
committerSteph Enders <steph@senders.io>2023-07-05 10:40:37 -0400
commit0e0988682f887cd5e363ab7e23e9ed2a4c67184e (patch)
treea413c4e252f88b2a00dead7a72af1374fb9057b1 /update.sh
parent7ad553afa01bc362871480ad74780e678f88e7ea (diff)
Create update script
This script will: 1. take in a domain and a config path 2. fetch your domains current IP using dig 3. fetch your server IP using ipinfo.io/ip (via curl) 4. then if there is a mismatch it will: 4.1. fetch your DNS A Record for that API from the API 4.2. POST an update for that DNS record with the new IP from the API 5. else print that things are successful The porkbun APIs denoted here: https://porkbun.com/api/json/v3/documentation
Diffstat (limited to 'update.sh')
-rwxr-xr-xupdate.sh40
1 files changed, 40 insertions, 0 deletions
diff --git a/update.sh b/update.sh
new file mode 100755
index 0000000..9fba7be
--- /dev/null
+++ b/update.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env sh
+set -e
+if [ $# -ne 2 ]; then
+ echo "Usage: ./update.sh DOMAIN API_CONFIG" >&2
+ exit 1
+fi
+
+DOMAIN=$1
+API_CONFIG=$2
+
+source $API_CONFIG
+config_ip=$(dig +short $DOMAIN)
+actual_ip=$(curl https://ipinfo.io/ip)
+
+if [ "$config_ip" != "$actual_ip" ]; then
+ echo "Updating IP from $config_id to $actual_ip for $DOMAIN"
+ res=$(curl -X POST "https://porkbun.com/api/json/v3/dns/retrieve/$DOMAIN" \
+ -d "{ \"secretapikey\": \"${SECRETAPIKEY}\", \"apikey\": \"${APIKEY}\" }")
+ status=$(echo "$res" | jq -r '.status')
+ if [ "$status" != "SUCCESS" ]; then
+ echo "Failed to fetch domain ID:\n\t$res" >&2
+ exit 1
+ fi
+ ID=$(echo "$res" | jq -r ".records[] | select(.type == \"A\") | select(.content == \"${config_ip}\") | .id")
+ res=$(curl -X POST "https://porkbun.com/api/json/v3/dns/edit/$DOMAIN/$ID" \
+ -d "{ \"secretapikey\": \"${SECRETAPIKEY}\", \"apikey\": \"${APIKEY}\", \"content\": \"${actual_ip}\", \"ttl\": \"600\", \"type\": \"A\", \"name\": \"\" }")
+ status=$(echo "$res" | jq -r '.status')
+ if [ "$status" == "SUCCESS" ]; then
+ echo "Successfully updated $DOMAIN record $ID from $config_ip to $actual_ip"
+ else
+ echo "Failed to update DNS record for $DOMAIN with $ID from $config_ip to $actual_ip" >&2
+ pretty=$(echo "$res" | jq '.')
+ echo "$pretty" >&2
+ exit 1
+ fi
+else
+ echo "$DOMAIN is currently registered properly: $config_ip $actual_ip"
+fi
+
+exit 0