Don't understand german? Read or subscribe to my english-only feed.

Maintain /etc with mercurial on Debian

Update on 2007-09-12: updated shell script and added Pre-Invoke to dpkg configuration.

Based on Bart Trojanowski’s blog article “etc snapshots with git” I removed one further item from my todo list: maintain /etc with mercurial on my Debian systems.

First step is creating the repository and securing access to the repository. As mercurial stores all its files within one single directory named .hg you won’t have any further directories within /etc besides /etc/.hg, rocking – right? ;-)

cd /etc
hg init
chmod og-rwx .hg

Ignore a few files:

cat > .hgignore << EOF
syntax: regexp
# add other files if necessary, depends on your setup...

Now commit the current state:

hg add
hg ci -m "initial checkin"

We want to commit the changes automatically when using apt-get/aptitude so let’s write and include a wrapper for it – /etc/apt/hg-snapshot-script:

set -e

caller=$(ps axww | mawk '/aptitude|apt-get/ {for (i=5; i<=NF ; i++) printf ("%s ",$i); printf ("\\n") }' | head -1)

hg addremove 1>/dev/null
STATUS="$(hg st)"

if [ -z "$STATUS" ] ; then
   echo "hg-snapshot-script: nothing to be done"
   case "$1" in
           echo "hg-snapshot-script: found changed files:"
           hg st
           hg ci -m "snapshot from $LOGNAME before: $caller"
           echo "hg-snapshot-script: found changed files:"
           hg st
           hg ci -m "snapshot from $LOGNAME after: $caller"
           echo "hg-snapshot-script: found changed files:"
           hg st
           hg ci -m "snapshot from $LOGNAME on $(date '+%Y-%m-%d - %H:%M:%S')"

Activate it:

chmod +x /etc/apt/hg-snapshot-script

cat >> /etc/apt/apt.conf << EOF
DPkg {
  Pre-Invoke  {"cd /etc ; ./apt/hg-snapshot-script pre";};
  Post-Invoke {"cd /etc ; ./apt/hg-snapshot-script post";};

Finally track the added files as well:

hg add
hg commit -m "apt will track /etc automagically using hg"

That’s it. :-)

Oh BTW: users of subversion might want to point their browser to /etc under svk (by Enrico Zini).

Comments are closed.