#! /bin/sh # # mass update TTLs in a list of source files # usage() { cat < ... Changes the global \$TTL and SOA TTL directly in a list of BIND zone files specially formatted. EOF } if [ $# -lt 2 ]; then usage exit fi NEW_TTL=$1; shift # external command dependencies awk=awk mv=mv cut=cut sed=sed echo=echo date=date printf=printf mktemp=mktemp # increment a serial number in a zone file # # we assume that the serial line contains the "serial string", eg.: # 2005012703 ; serial # # from AlternC do_domaines.sh # # returns 1 if file isn't readable # returns 2 if we can't find the serial number # returns 3 if a tempfile can't be created increment_serial() { if [ -f "$1" ]; then # the assumption is here SERIAL=`$awk '/^..*serial/ {print $1}' < $1` || return 2 if [ ! -z "${SERIAL}" ]; then DATE=`$echo $SERIAL | $cut -c1-8` ORDRE=`$echo $SERIAL | $sed s/"${DATE}0\?"/""/g` DATE_JOUR=`$date +%Y%m%d` # increment the serial number only if the date hasn't changed if [ "X$DATE" = "X$DATE_JOUR" ] ; then ORDRE=$(($ORDRE+1)) else ORDRE=1 DATE=$DATE_JOUR fi NEW_SERIAL=$DATE`$printf "%.2d" $ORDRE` TMPFILE=`$mktemp $1.XXXXXX` || return 3 # put the serial number in place $awk -v NEW_SERIAL=$NEW_SERIAL '{if ($3 =="serial") print " "NEW_SERIAL " ; serial"; else print $0}' < $1 > $TMPFILE && \ $mv -f $TMPFILE $1 return 0 else return 2 fi else return 1 fi } # # Change the global $TTL line in a zone file # change_global_ttl() { ttl=$1 file=$2 TMPFILE=`$mktemp $file.XXXXXX` $sed -e 's/^\$TTL.*$/$TTL '$ttl'/' < $file > $TMPFILE && \ $mv -f $TMPFILE $file } # # Change the TTL of an SOA record # # This expects the TTL line to be alone on a line, surrounded by space # and followed by a closing parenthesis, which should work in most # cases change_soa_ttl() { ttl=$1 file=$2 TMPFILE=`$mktemp $file.XXXXXX` $sed -e 's/\([[:space:]][[:space:]]*\)[0-9][0-9]*[DdHhMm]\?\([[:space:]]*)\)/\1'$ttl'\2/' < $file > $TMPFILE && \ $mv -f $TMPFILE $file } # do everything for i; do echo "updating ttl to $NEW_TTL in $i" >&2 change_global_ttl $NEW_TTL $i change_soa_ttl $NEW_TTL $i increment_serial $i done