bind/OLD_Versions/bind_change_SOA.sh
2017-08-04 00:15:29 +02:00

118 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
#_zone_files_dir="/etc/bind/master"
_zone_files_dir=/root/tmp/master
## - Settings SOA Record
## -
_soa_nameserver="a\.ns\.oopen\.de"
_soa_admin="domreg\.oopen\.de"
cp -a $_zone_files_dir ${_zone_files_dir}.BAK-`date +%Y-%m-%d-%H%M`
## --- some functions
## ---
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation wird abgebrochen\033[m\033[m"
echo ""
exit 1
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
echo_ok() {
echo -e "\033[75G[ \033[32mok\033[m ]"
}
echo_failed(){
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
}
## ---
## --- END: functions
echo ""
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo ""
## - Read list of zonefiles
## -
while IFS='' read -r -d '' zone_file ; do
declare -i _serial_new=`date +%Y%m%d01`
filename=$(basename "$zone_file")
zone="${filename%.*}"
echo ""
echo -e "\tlooking at $zone_file.."
## - calculate new serial
## -
declare -i __serial=`grep -e "[0-9]\{10\}" $zone_file | grep serial | awk '{print$1}'`
while [ ! $_serial_new -gt $__serial ]; do
let _serial_new++
done
echononl "\t Changing SOA Record.."
if ! grep -E "^.*IN\s+SOA\s+${_soa_nameserver}\.\s+${_soa_admin}\.\s+\(" > /dev/null 2>&1 $zone_file ; then
perl -i -n -p -e "s/^(.*IN\s+SOA).*$/\1 ${_soa_nameserver}. ${_soa_admin}. \(/" $zone_file
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
else
echo_skipped
continue
fi
## - Set new serial
## -
echononl "\t Renew serial.."
perl -i -n -p -e "s#^(\s*) $__serial(.*)#\1 $_serial_new\2#" $zone_file
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
## - Reload Zone
## -
echononl "\t Reload Zone $zone.."
/usr/sbin/rndc reload $zone > /dev/null 2>&1
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
sleep 1
done < <(find $_zone_files_dir -mindepth 1 -maxdepth 1 -type f -print0)
echo ""
exit