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
(^)*.dpkg-new
(^)*.dpkg-old
(^)blkid.tab(|.old)
(^)mtab
# add other files if necessary, depends on your setup...
EOF

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:

#!/bin/sh
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"
else
   case "$1" in
        pre)
           echo "hg-snapshot-script: found changed files:"
           hg st
           hg ci -m "snapshot from $LOGNAME before: $caller"
          ;;
        post)
           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')"
          ;;
   esac
fi

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";};
}
EOF

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.