#!/usr/bin/env bash ## export cloudflare dns zones # exit if required variables are not set set -o nounset # check for dependencies script_commands="docker git" missing_counter=0 for needed_command in ${script_commands}; do if ! hash "${needed_command}" >/dev/null 2>&1; then printf "Command not found in PATH: %s\n" "${needed_command}" >&2 ((missing_counter++)) fi done if ((missing_counter > 0)); then printf "Minimum %d commands are missing in PATH, aborting.\n" "${missing_counter}" >&2 exit 1 fi # set git remote git_remote="ssh://git/seednode/cloudflare-zones.git" # set git branch git_branch="master" # set zone file directory zone_dir="${HOME}/zones" # create zone file directory, if it does not exist if [ ! -d "${zone_dir}" ]; then mkdir -p "${zone_dir}" fi # if .git directory does not exist, init the repo if [ ! -d "${zone_dir}/.git" ]; then git -C "${zone_dir}" init # otherwise pull any remote changes else git -C "${zone_dir}" pull "${git_remote}" "${git_branch}" fi # retrieve cloudflare api token CLOUDFLARE_API_TOKEN="$(cat "${HOME}"/bin/.cloudflare_api_token.file)" # retrieve list of zones for DOMAIN in $( (docker run --tty --rm dpig/cloudflare-cli:latest --token "${CLOUDFLARE_API_TOKEN}" --format csv zones) | grep "active" | cut -d"," -f1); do (docker run --tty --rm dpig/cloudflare-cli:latest --token "${CLOUDFLARE_API_TOKEN}" --domain "${DOMAIN}" --format csv listrecords) > "${zone_dir}/${DOMAIN}.zone" done # add changed files git -C "${zone_dir}" add . # commit changes git -C "${zone_dir}" commit -a -m "Zones changed on $(date)" # add remote if needed git -C "${zone_dir}" remote show origin >/dev/null 2>&1 || git -C "${zone_dir}" remote add origin "${git_remote}" # push changes git -C "${zone_dir}" push "${git_remote}"