Updating DigitalOcean DNS records with a dynamic IP
If you host services on a home server with a dynamic IP, you need a way to point your domain to your new public IP when it changes. Here is how I use Bash and Crontab to automatically update my DigitalOcean DNS records.
Requirements
-
DigitalOcean API Token which can be generated in your DigitalOcean account settings.
-
Domain Names list. Make a list of all the domains in your account that need to point to your new public IP.
Collect record IDs for each domain
DigitalOcean uses specific IDs for every record. You need to grab the record ID for every domain and subdomain you want to update (e.g., example.com, www.example.com).
Create a bash file to fetch record IDs for the domains
nano get_record_ids.sh
- Insert the below code into you file.
#!/bin/bash
[[ -z "$1" ]] && { echo "Usage: sudo ./get_record_ids example.com"; exit 1; }
curl GET -H "Content-Type: application/json" \
-H "Authorization: Bearer INSERT_YOUR_TOKEN_HERE" \
https://api.digitalocean.com/v2/domains/$1/records | json_pp | grep -E "id|name|type"
Make it executable and run it
chmod u+x get_record_ids.sh
- Execute the file with a parameter for each of your domains.
./get_record_ids.sh example1.com
./get_record_ids.sh example2.com
./get_record_ids.sh example3.com
- Note down all the entries that have the type A.
Do not update NS (nameserver) or SOA (Start Of Authority) records.
Here is a sample of what the output looks like:
# ./get_record_ids.sh example1.com
"id" : 117631553,
"name" : "@",
"type" : "SOA",
"id" : 117499523,
"name" : "@",
"type" : "NS",
"id" : 117636234,
"name" : "@",
"type" : "NS",
"id" : 117634432,
"name" : "@",
"type" : "NS",
"id" : 117634123,
"name" : "@",
"type" : "A",
"id" : 117634634,
"name" : "www",
"type" : "A",
"id" : 117634433,
"name" : "blog",
"type" : "A",
"id" : 117612111,
"name" : "store",
"type" : "A",
"id" : 117634674,
"name" : "www.blog",
"type" : "A",
Create the update_digital_ocean.sh script
Now we have the DigitalOcean API token, the domain names, and all the "A" record IDs. It is time to create the bash script that actually updates them.
Create a bash file to update the IPs
nano update_digital_ocean.sh
- Insert the below code into the file.
#!/bin/bash
TOKEN="insert_your_TOKEN_here"
LOG_FILE="/home/YOUR_USERNAME/digital_ocean_update_logs.txt"
DOMAIN1="example1.com"
RECORD_A1="112176990"
RECORD_A2="112177021"
RECORD_A3="113300718"
RECORD_A4="127939656"
RECORD_A5="117087411"
DETECTED="$(dig +short myip.opendns.com @resolver1.opendns.com)"
PREVIOUS="$(tail -1 $LOG_FILE | awk -F, '{print $2}')"
if [ "$DETECTED" = "$PREVIOUS" ]
then
echo "Previous IP -> ($PREVIOUS)."
echo "Detected IP -> ($DETECTED)."
echo "No change detected, exiting."
else
echo "Previous IP -> ($PREVIOUS)."
echo "Detected IP -> ($DETECTED)."
echo "New public IP detected, updating DigitalOcean records…" && echo
echo "$(date),$DETECTED" >> "$LOG_FILE"
curl -X PUT -H "Content-Type: application/json" -H \
"Authorization: Bearer $TOKEN" -d '{"data":"'"$DETECTED"'"}' \
"https://api.digitalocean.com/v2/domains/$DOMAIN1/records/$RECORD_A1"
fi
Script above only updates $RECORD_A1. It needs a copy and paste for the rest of the required records.
Make it executable and run it
chmod u+x update_digital_ocean.sh
- Run the script manually for the first time. It will throw an error because the log file does not exist yet.
./update_digital_ocean.sh
- Run the script again.
./update_digital_ocean.sh
This time it will exit normally and complete the task.
Automate the process with crontab
To fully automate this, set up a job in crontab. This runs the script in the background on a schedule.
- Open crontab.
crontab -e
- Add this line to the bottom of the file, it will run the script every 15 minutes.
*/15 * * * * /path/to/script/update_digital_ocean.sh
- Save the file and exit.
You can change the timer to whatever you want, but I wouldn't recommend going lower than 5 minutes to avoid spamming the DigitalOcean backend.