You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

指定域名备份,备份的解析记录会以txt形式的文本文件保存到当前目录下,可以结合其他备份脚本将txt文件拷贝到安全的位置进行存放

第一个domain相当于是完整的域名,如果是要获取单个域名的话,这里就是aa.$domains

备份脚本bak.sh

dnsname=192.168.1.100:5380
username=admin
password=Uenpay2023
tokenname=`curl -s "http://$dnsname/api/user/createToken?user=$username&pass=$password&tokenName==zhengyu"|jq .token|sed 's#"##g'`
domainlist=(
  uenpay.com
  weifumao.com
  5ubp.com
  zhuduan.vip
  merrymate.cn
)

for domains in ${domainlist[@]}
do
  status=`curl -s "http://$dnsname/api/zones/records/get?token=$tokenname&domain=$domains&zone=$domains&listZone=true"|jq .status|sed 's#"##g'`
  if [ $status = "error" ];then
    echo "$domains 不存在"
    continue
  elif [ $status = "ok" ];then
    ##备份A记录
    curl -s "http://$dnsname/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 }' > $domains-`date +%F`.txt
    ##备份CNAME记录
    curl -s "http://$dnsname/api/zones/records/get?token=$tokenname&domain=$domains&zone=$domains&listZone=true"|jq .response.records |jq  -r '.[]' |jq .name,.type,.rData.cname|sed 's/"//g'|paste - - - |awk '$2 == "CNAME" { print }' >> $domains-`date +%F`.txt
   else
    exit 1
  fi
done

备份的文件格式如下

a.abc.com    A       1.1.1.1

b.abc.com   CNAME   b.abc.com.w.kunlunpi.com

如果DNS服务器不慎被破坏,可将之前定期备份的txt文件拿过来进行恢复

恢复的时候默认创建的是Primary Zone,如果需要Conditional Forwarder Zone可自行在控制台进行转换

恢复脚本res.sh

dnsname=192.168.1.100:5380
username=admin
password=Uenpay2023
tokenname=`curl -s "http://$dnsname/api/user/createToken?user=$username&pass=$password&tokenName==zhengyu"|jq .token|sed 's#"##g'`
domainlist=(
  uenpay.com
  weifumao.com
  5ubp.com
  zhuduan.vip
  merrymate.cn
)
for domains in ${domainlist[@]}
do
  status=`curl -s "http://$dnsname/api/zones/options/get?token=${tokenname}&zone=$domains&includeAvailableTsigKeyNames=true"|jq .status|sed 's#"##g'`
  if [ $status = "ok" ];then
    echo "$domains 已存在,请登陆控制台确认"
  else
    curl "http://$dnsname/api/zones/create?token=${tokenname}&zone=$domains&type=Primary"
    #创建Conditional Forwarder Zone(只能用于单点dns服务)
    #curl "http://$dnsname/api/zones/create?token=${tokenname}&zone=$domains&type=Forwarder&forwarder=223.5.5.5"
    ##添加A记录
    cat ${domains}-*.txt|awk '$2 == "A" { print }'|while read line;
    do
      domain=`echo $line|awk '{print $1}'`
      ip=`echo $line|awk '{print $3}'`
      curl -s "http://$dnsname/api/zones/records/add?token=$tokenname&domain=$domain.$domains&zone=$domains&type=A&ttl=600&overwrite=true&ipAddress=$ip"
    done
    ##添加CNAME记录
    cat ${domains}-*.txt|awk '$2 == "CNAME" { print }'|while read line;
    do
      domain=`echo $line|awk '{print $1}'`
      cname=`echo $line|awk '{print $3}'`
      curl -s "http://$dnsname/api/zones/records/add?token=$tokenname&domain=$domain.$domains&zone=$domains&type=CNAME&ttl=600&overwrite=true&cname=$cname"
    done
  fi
done


通过接口获取当前所有域名对象

dnsname=192.168.1.100:5380
username=admin
password=Uenpay2023
tokenname=`curl -s "http://$dnsname/api/user/createToken?user=$username&pass=$password&tokenName==zhengyu"|jq .token|sed 's#"##g'`
curl -s "http://$dnsname/api/zones/list?token=${tokenname}&"|jq .response.zones|jq  -r '.[]'|jq .name|grep -Ev "arpa|localhost"|sed 's#"##g'
  • No labels