aboutsummaryrefslogtreecommitdiff
path: root/blog/2022-03-23-cloudflare-dns-api.org
diff options
context:
space:
mode:
Diffstat (limited to 'blog/2022-03-23-cloudflare-dns-api.org')
-rw-r--r--blog/2022-03-23-cloudflare-dns-api.org99
1 files changed, 50 insertions, 49 deletions
diff --git a/blog/2022-03-23-cloudflare-dns-api.org b/blog/2022-03-23-cloudflare-dns-api.org
index 8b87aba..3a80a71 100644
--- a/blog/2022-03-23-cloudflare-dns-api.org
+++ b/blog/2022-03-23-cloudflare-dns-api.org
@@ -1,39 +1,40 @@
-+++
-date = 2022-03-23
-title = "Dynamic DNS with Cloudflare API"
-description = "Learn how to update Cloudflare DNS records automatically with a simple bash script."
-draft = false
-+++
-
-## DDNS: Dynamic DNS
-
-If you're hosting a service from a location with dynamic DNS (where your IP may
-change at any time), you must have a solution to update the DNS so that you can
-access your service even when the IP of the server changes.
-
-The process below uses the [Cloudflare API](https://api.cloudflare.com/) to
-update DNS `A` and `AAAA` records with the server's current IP. If you use
-another DNS provider, you will have to find a way to update your DNS (or find a
-way to get a static IP).
-
-First, install `jq` since we will use it in the next script:
-
-```sh
+#+title: Dynamic DNS with Cloudflare API
+#+date: 2022-03-23
+
+** DDNS: Dynamic DNS
+:PROPERTIES:
+:CUSTOM_ID: ddns-dynamic-dns
+:END:
+If you're hosting a service from a location with dynamic DNS (where your
+IP may change at any time), you must have a solution to update the DNS
+so that you can access your service even when the IP of the server
+changes.
+
+The process below uses the [[https://api.cloudflare.com/][Cloudflare
+API]] to update DNS =A= and =AAAA= records with the server's current IP.
+If you use another DNS provider, you will have to find a way to update
+your DNS (or find a way to get a static IP).
+
+First, install =jq= since we will use it in the next script:
+
+#+begin_src sh
sudo apt install jq
-```
+#+end_src
-Next, create a location for your DDNS update scripts and open the first script:
+Next, create a location for your DDNS update scripts and open the first
+script:
-```sh
+#+begin_src sh
mkdir ~/ddns
nano ~/ddns/update.sh
-```
+#+end_src
-The following `update.sh` script will take all of your domains and subdomains
-and check Cloudflare to see if the current `A` and `AAAA` records match your
-server's IP address. If not, it will update the records.
+The following =update.sh= script will take all of your domains and
+subdomains and check Cloudflare to see if the current =A= and =AAAA=
+records match your server's IP address. If not, it will update the
+records.
-```sh
+#+begin_src sh
# file: update.sh
#!/bin/bash
@@ -55,20 +56,20 @@ do
echo -e "\nUpdating $subdomain..."
zone_name=$domain dns_record=$subdomain /home/<your-username>/ddns/ddns.sh
done
-```
+#+end_src
-Next, open up the `ddns.sh` script. Paste the following into the script and
-update the `api_token` and `email` variables.
+Next, open up the =ddns.sh= script. Paste the following into the script
+and update the =api_token= and =email= variables.
-```sh
+#+begin_src sh
nano ~/ddns/ddns.sh
-```
+#+end_src
-:warning: **Note**: If you want your DNS records to be proxied through
-Cloudflare, find and update the following snippet: `\"proxied\":false}"` to say
-`true` instead of `false`.
+:warning: *Note*: If you want your DNS records to be proxied through
+Cloudflare, find and update the following snippet: =\"proxied\":false}"=
+to say =true= instead of =false=.
-```sh
+#+begin_src sh
# file: ddns.sh
#!/bin/bash
# based on https://gist.github.com/Tras2/cba88201b17d765ec065ccbedfb16d9a
@@ -161,30 +162,30 @@ then
else
echo "There is a problem with either the email or the password"
fi
-```
+#+end_src
Once the script is saved and closed, make the scripts executable:
-```sh
+#+begin_src sh
chmod +x ~/ddns/ddns.sh
chmod +x ~/ddns/update.sh
-```
+#+end_src
You can test the script by running it manually:
-```sh
+#+begin_src sh
./update.sh
-```
+#+end_src
-To make sure the scripts run automatically, add it to the `cron` file so that it
-will run on a schedule. To do this, open the cron file:
+To make sure the scripts run automatically, add it to the =cron= file so
+that it will run on a schedule. To do this, open the cron file:
-```sh
+#+begin_src sh
crontab -e
-```
+#+end_src
In the cron file, paste the following at the bottom of the editor:
-```sh
+#+begin_src sh
*/5 * * * * bash /home/<your_username>/ddns/update.sh
-```
+#+end_src