Programs of Hyper

How-To store /etc in a git repository on debian linux

What this howto do?

Create and set up the repository

First of all install the necessary packages. Git for versionig, and metastore for save-restore permissions of files and folders.

aptitude install git metastore
Create a directory in etc to store scripts used by apt hooks
mkdir /etc/etc-git

/etc/etc-git/aptpreinstall.bash

#!/bin/bash GITSTATUSOUT=`(cd /etc;git status|grep 'nothing to commit (working directory clean)')` GITDIFFOUT=`(cd /etc;git diff)` if test "$GITSTATUSOUT" != "nothing to commit (working directory clean)" || test "$GITDIFFOUT" != "" then echo -e "\n\n !!!WARNING!!! Uncomitted changes in /etc !!!WARNING!!!" echo -e " ( Run: eg-commit )\n" exit 1 fi exit 0

/etc/etc-git/aptpostinstall.bash

#!/bin/bash GITSTATUSOUT=`(cd /etc;git status|grep 'nothing to commit (working directory clean)')` GITDIFFOUT=`(cd /etc;git diff)` DATE=`date` if test "$GITSTATUSOUT" != "nothing to commit (working directory clean)" || test "$GITDIFFOUT" != "" then echo -e "\n===== /etc changed need git commit =====" ( cd /etc/ /usr/local/bin/eg-save git add --all git commit . -m "Automatic commit after apt-get ($DATE)" ) echo -e "\n===== Comitted =====" exit 0 fi exit 0

Setting up apt hooks to use the scripts created above.
chmod +x /etc/etc-git/*.bash
echo "DPkg::Pre-Invoke { '/etc/etc-git/aptpreinstall.bash' };" >> /etc/apt/apt.conf.d/90etc-git
echo "DPkg::Post-Invoke { '/etc/etc-git/aptpostinstall.bash' };" >> /etc/apt/apt.conf.d/90etc-git
This hooks made:
Now let's create an empty repository and set up some metadata.
cd /etc
git init
echo "System ETC stored in GIT" > /etc/.git/description
git config --global user.name "Your Name"
git config --global user.email you@example.com
Create some script file in /usr/local/bin

/usr/local/bin/eg-save

#!/bin/sh cd /etc metastore -s

/usr/local/bin/eg-restore

#!/bin/sh cd /etc metastore -aeq

/usr/local/bin/eg-check

#!/bin/bash CHKRES=`cd /etc;metastore -c` if test "$CHKRES" != "" then echo -e "The permission structure of the /etc is changed\n" echo -e "Run: eg-save" exit 1 fi exit 0

/usr/local/bin/eg-commit

#!/bin/bash if test "$1" == "" then echo "Missing parameter!" echo " use: eg-commit \"Commit text\"" exit 1 fi cd /etc echo "Save permissions (eg-save)..." eg-save echo "Add all non versioned items to git (git add --all)..." git add --all echo "Do commit (git commit .)..." git commit . -m "$1"

Now we need to create a git "pre-commit" hook to check wheter the permissions are saved before commit.
This hook warn you if the permissions of /etc changed and tell you to run eg-save before commit.
(You think it would be nice to put "eg-save" into this "pre-commit" hook, but unfortunately the git doesn't allow to change the versioned files in this hook)
To create the hook create this file

/etc/.git/hooks/pre-commit

#!/bin/sh /usr/local/bin/eg-check

Don't forget to make this scripts executable
chmod +x /usr/local/bin/eg-*
chmod +x /etc/.git/hooks/pre-commit
And finally put the current etc into the git repository and do the initial commit!
eg-save
git add --all
eg-commit "Initial state"

That's it! The versioning is working now

If you change something in /etc you can easely commit your changes by command
eg-commit "Message to commit"
Of course, you can use all git command to tracking the changes of your /etc

Setting up remote repository (optional)

On (remote)server side we need to create the remote repository
mkdir /GITREPO/debianmachine
cd /GITREPO/debianmachine
git --bare init
Now we can set up git to use remote repository on our debian machine, and do the first push to server
cd /etc
git remote add -t master origin ssh://servername.domain.com/GITREPO/debianmachine
git push origin master
After this set up you can simply do "git push" and "git pull" later.

Author: Péter Deák
Contact Email: mailaddressat gmail.com