...
Code Block |
---|
#!/bin/bash bak_dir=/data/coredns/dns_bak dns_url=192.168.31.219:5380 username=admin password=Uenpay@2019 token_file="$bak_dir/.token" webhookurl="https://oapi.dingtalk.com/robot/send?access_token=0ccbf717fb61bef56d0cc29d25b5d7e79da719997c5d0dca55f709bd54e5ef32" if [ ! -d "$bak_dir" ]; then mkdir -p "$bak_dir" fi # 函数:获取 token get_token() { local time=$(date +%F) local current_hostname=$(hostname) local current_ip=$(ip addr | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | head -n 1 | cut -d / -f1) local token_name="${current_hostname}-${current_ip}-${time}" # 构建 tokenName curl -s "http://$dns_url/api/user/createToken?user=$username&pass=$password&tokenName=${token_name}" | jq .token | sed 's#"##g' } send_sysc_status() { local webhookurl="$1" local time=$(date) local current_ip=$(ip addr | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | head -n 1 | cut -d / -f1) local message="{ \"msgtype\": \"markdown\", \"markdown\": { \"title\": \"CoreDNS同步失败\", \"text\": \"#### **$current_ip** CoreDNS同步失败\n\n- 时间: **$time**\n\" } }" curl -s "${webhookurl}" -H 'Content-Type: application/json' -d "${message}" } # 检查并获取 token if [[ -f $token_file ]]; then tokenname=$(cat "$token_file") # 验证 token 是否有效 status=$(curl -s "http://$dns_url/api/zones/records/get?token=$tokenname&domain=zhengyu1992.cn&zone=zhengyu1992.cn&listZone=true" | jq .status | sed 's#"##g') if [[ $status != "ok" ]]; then echo "Token 无效,重新获取 Token." tokenname=$(get_token) echo "$tokenname" > "$token_file" else echo "Token 有效,不需要重新获取." fi else echo "Token 文件不存在,获取新的 Token." tokenname=$(get_token) echo "$tokenname" > "$token_file" fi domainlist=( hkrt.cn hzmohai.com uenpay.com xscashier.com zhengyu1992.cn zhuduan.vip ) for domains in "${domainlist[@]}" do # 获取当前的 serial 值 current_serial=$(curl -s "http://$dns_url/api/zones/records/get?token=$tokenname&domain=$domains&zone=$domains&listZone=true" | jq -r '.response.records[]?.rData.serial' | grep -v null | head -n 1) if [[ -z $current_serial ]]; then echo "$domains 没有 serial 信息,可能不存在,请确认" continue fi # 检查历史记录中的 serial 值 if [[ -f "$bak_dir/.${domains}_serial.txt" ]]; then last_serial=$(cat "$bak_dir/.${domains}_serial") if [[ "$current_serial" == "$last_serial" ]]; then echo "$domains 的 serial 未变化,跳过备份" continue fi fi # 如果 serial 发生变化,进行备份 echo "$domains 的 A记录" > "$bak_dir/$domains-$(date +%F).txt" backup_status=$(curl -s "http://$dns_url/api/zones/records/get?token=$tokenname&domain=$domains&zone=$domains&listZone=true" | jq .response.records | jq -r '.[]' | jq .name,.type,.rData.ipAddress | sed 's/"//g' | paste - - - | awk '$2 == "A" { print $3, $1 }' >> "$bak_dir/$domains-$(date +%F).txt") # 判断备份命令是否成功 if [ $? -neeq 0 ]; then echo "$domains 备份成功" else echo "$domains 备份异常" send_sysc_status $webhookurl exit 1 fi # 更新 serial 值 echo "$current_serial" > "$bak_dir/.${domains}_serial" done |
...
同步脚本,配合上一步使用
Code Block |
---|
#/bin/bash cd bak_dir=/data/coredns/dns_sync && bak sh /shell/dns_bak.sh cd $bak_dir cat /data/coredns/dns_sync/*.txt >/data/coredns/config/customer-hosts |
...