Skip to content

Instantly share code, notes, and snippets.

@EthraZa
Last active July 20, 2021 16:52
Show Gist options
  • Save EthraZa/da615103a1f0729602304fa27e99b83a to your computer and use it in GitHub Desktop.
Save EthraZa/da615103a1f0729602304fa27e99b83a to your computer and use it in GitHub Desktop.
Digital Ocean - Update DNS records with Droplets internal private IPs and hostname
#!/bin/bash
#
# Digital Ocean - Update DNS records with Droplets internal private IPs and hostname
# DO_upd_domain.sh:
#
# 2021-07-20 - v1.1 - By EthraZa <Allan Brazute>
# Add: Update only powers and --flush switch to recreate the domain
#
# 2021-07-15 - v1.0 - By EthraZa <Allan Brazute>
#
if [ ${1} ]; then
DOMAIN="${1}"
echo "Update domain ${1}"
else
echo "Usage: DO_upd_domain.sh DOMAIN.TLD [--flush] "
echo "* Depends on working doctl and VPC "
echo " "
echo "Examples: "
echo "- Only update or create new records: "
echo " DO_upd_domain.sh lan.example.com "
echo " "
echo "- Delete domain with all records and recreate it: "
echo " DO_upd_domain.sh lan.example.com --flush "
echo " "
exit 0
fi
if [ .${2} == ."--flush" ]; then
echo "Delete ${DOMAIN}"
doctl compute domain delete ${DOMAIN} -f
echo "Create ${DOMAIN}"
doctl compute domain create ${DOMAIN}
fi
for E in `doctl compute droplet list --format "Name,PrivateIPv4" --no-header|sed 's/ \+/,/'`; do
NAME=`echo ${E}|cut -d',' -f1|cut -d'.' -f1`
IP=`echo ${E}|cut -d',' -f2`
if [ ${NAME} ] && [ ${IP} ]; then
RID=`doctl compute domain records list i.ghsix.com.br --format "ID,Name" --no-header|grep -m1 -i "${NAME}"|cut -d" " -f1`
if [ ${RID} ]; then
echo "Update ${NAME}"
doctl compute domain records update ${DOMAIN} --record-type "A" --record-name "${NAME}" --record-data "${IP}" --record-id "${RID}"
else
echo "Create ${NAME}"
doctl compute domain records create ${DOMAIN} --record-type "A" --record-name "${NAME}" --record-data "${IP}"
fi
else
echo "Skip ${NAME} ${IP}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment