Updating bind serial numbers automatically
Posted by Steve on Fri 31 Mar 2006 at 19:50
If you work with the DNS server bind you'll probably be used to updating the serial number for your zone files manually after making changes. If you're an Emacs user there is a simple automatic way of doing the job.
Thanks to a blog posting by Tollef Fog Heen I learnt today that there is an Emacs mode for working with Zone files. (Perhaps I shouldn't be suprised; there seems to be a mode for everything!)
If you make changes to your zone file Emacs will automatically update the serial number for you, in the canonical format of YYYYMMDDXX (where YYYY is the current year, MM is the current month, DD is the current day, and XX is the revision number).
Simply add the string "-*- zone -*-" to the head of the file. For example:
;; -*- zone -*-
;;
;; Zone file for example.org
;;
example.org 86400 IN SOA example.org. hostmaster.example.org. (
2006033100 ; Serial YYYYMMDDXX
10800 ; Refresh
3600 ; Retry
3600000 ; Expire
86400 ) ; minimum
IN NS ns1.example.org.
IN NS ns2.example.org.
...
The next time you load the file in Emacs you'll see that you've got syntax highlighting, and if you write any changes to the file the Serial will be incremented appropriately.
Obviously this won't help you if you use another editor, or another nameserver, but it was a neat trick that I thought was worth sharing.
[ Parent | Reply to this comment ]
"--- .../plugins/named.vim ----
function! UPDSERIAL(date, num)
if (strftime("%Y%m%d") == a:date)
return a:date . a:num+1
endif
return strftime("%Y%m%d") . '01'
endfunction
command Soa :%s/\(2[0-9]\{7}\)\([0-9]\{2}\); Serial/\=UPDSERIAL(submatch(1), submatch(2)) . '; Serial'/gc
"---- eof ---------------------
serial is updated calling :Soa
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
Thanks, thats a neat match for Vim.
The big difference is that using that plugin the user must remember to run :Soa or it won't be updated - the Emacs version will automatically do the update for the relevent files.
If it were possible to get the Vim script to run automatically on file saves for files matching a pattern (e.g. /etc/bind/db.*) then it might be more usable.
Any time you're required to do something manually there is a chance you'll forget..
[ Parent | Reply to this comment ]
autocmd! BufWrite * call s:Incr_Soa()
function s:Incr_Soa()
....
into soa.vim inside ~/.vim/plugin/soa.vim
You could also change it to
autocmd! BufWrite /etc/bind/* call s:Incr_Soa()
to only match on paths inside /etc/bind
[ Parent | Reply to this comment ]
autocmd BufWritePre /etc/bind/db.* call s:Incr_Soa()
which gets fired when the buffer is written, but before it goes into the file.... In any case, :help autocmd will tell you all you need to know
[ Parent | Reply to this comment ]
thanks for this.
A tiny update to make sure it keeps the spacing anyone will use:
"--- .../plugins/named.vim ----
function! UPDSERIAL(date, num)
if (strftime("%Y%m%d") == a:date)
return a:date . a:num+1
endif
return strftime("%Y%m%d") . '01'
endfunction
command Soa :%s/\(2[0-9]\{7}\)\([0-9]\{2}\)\(\s*;\s*Serial\)/\=UPDSERIAL(subm atch(1), submatch(2)) . submatch(3)/gc
"---- eof ---------------------
Essentially I only change the command to match the '; Serial' in a more generic way.
Good luck and thanks again,
Gerke
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
--
If you're smart enough to ask this question, you're smart enough to RTFM and find out yourself.
[ Parent | Reply to this comment ]
This sort of crypto-primitivism drives me crazy. Just because doing something a better way most of the time might occasionally cause us to have to remember something in the exceptional times, isn't a good argument for not improving how we do things.
[ Parent | Reply to this comment ]
mgrzybowski@mi:~/skrypty/dns$ cat serial.awk
BEGIN {
serail_new=0
serial_old=0
}
{
if ($1 > 2000010101 && $1 < 2050010101)
{
serial_old=$1
tmp=strftime("%Y%m%d")
serial_new=tmp*100+1
while (serial_old >= serial_new)
{
serial_new++
}
print " "serial_new, " ",$2,$3,$4,$5,$6,$7,$8.$9
}
else{
print $0
}
}
END {
}
usage:
gawk -f serial.awk zone >> zone
[ Parent | Reply to this comment ]
usage:
gawk -f serial.awk zone > zone.tmp
cp zone.tmp zone
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
I guess it just the defacto standard for people who work with bind - but why? I have no idea.
I know it is the recommended format in O'Reilly's DNS & Bind book, perhaps that has something to do with it?
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Those who auto-admin their zone files in some way... well, they should know what they're doing anyway, which implies knowing when the "canonical" way is not the right way for them. ;)
Human beings also tend not to edit their zonefiles more than 100 times a day :)
[ Parent | Reply to this comment ]
[ Send Message | View Weblogs ]
Emacs vs vi... again and again, but this time it is a pure hazard!! Christoph Berg published quite the same thing for vi on his blog today: http://www.df7cb.de/blog/2006/03/31#2006-03-31-dns-serials
Cheers,
Julien
[ Parent | Reply to this comment ]