User:Jjmcd/Drafts/Backing up config files with git

From FedoraProject

Jump to: navigation, search

OK, one of the Linux rags had something like this, but their approach used a convoluted ruby script, generated a tar file every night, and basically just did a lot more work than necessary.

The idea here is to use git to backup your configuration files. The Fedora project keeps all the web content, puppet files, etc. in git which makes for a very nice manageable system.

If you set this up, config files will be backed up each night if they change, and git will provide you of dates when anything changed, names of changed files, before/after snapshots, histories of individual files, just about anything you might want.

OK, first, make a git server. You could use gitorious or github which has the advantage of being offsite, but config files often contain passwords, so you might want to be careful about that. Let's just say you have a server on your LAN, and like me, you have symlinked /git to /var/lib/git (which makes the paths a little shorter). The root user on the target computer needs to have ssh access to the git repo.

OK, so, log on as root, I want to keep the config files for all my boxes together, so I want all those repos in a folder:

 mkdir /git/config
 cd /git/config

Now, initialize a bare repo for the computer to be backed up

 git init --bare computername.git

Especially if you are running gitweb, you may want to edit ./computername.git/description to provide a nice friendly description for the repo. This is purely optional.

Go to the /root directory of the machine to be backed up and clone the repo (logged on as root)

 cd ~
 git clone ssh://gitcomputername/git/config/computername.git

Go into the repo and make a .gitignore file:

 cd computername
 emacs .gitignore

You could use gedit, kate, whatever. If you are a vi guy, that might work too. It should contain at least a line like:

 *~

but over time you may find other things you want in there. Now, make an initial commit:

 git add .gitignore
 git commit -m "Initial commit"
 git push origin master

This also creates the master branch in the repo so the rest goes smoothly. Next, copy the hidden git directory to /etc

 cd /etc
 cp -r ~/computername/.git .
 cp ~/computername/.gitignore .

It is kind of nice to set the initial state, although the first run of the script will do this for you:

 git add .
 git commit -m "Initial state"
 git push origin

Finally, put the following script on the crontab:

#!/bin/sh
cd /etc
DA=`date +"%F"`
git add .
if git commit -m "Nightly backup $DA" ; then
   git push origin
fi

Now, any time anything in /etc changes, the a new commit with the date will be added to the repo. You can use normal git tools to see anything that has changed. gitweb is really nice for this, but gitk ain't too bad, either.

If you want to grab a particular file at a particular time git checkout will do that for you, git pull to get an entirely freshed up copy, like if you shot yourself in the foot editing. Pretty darned nice.

Example gitweb screenshots

Gitconfig-over.png

Activity of backed-up systems


Gitconfig-summ.png

Summary of commits for a particular system


Gitconfig-commit.png

Details of a particular commit


Gitconfig-commitdiff.png

Changes made to a file


Gitconfig-blame.png

Blame report showing commits to 'blame' for each line