Git as a Backup Solution

To preface this post, I would like to point out two very key things. The backup files will be stored in two branches inside of a single repository. Those branches will be called "files" and "database". You may choose to use other names (such as database and master) but for the purposes of this post, the afforementioned names will be used.

If it suits you better, you could also use two git repositories. I used that for a while and it worked great. I just found it more convenient to have the database dumps and the wiki files in one repository for simplicity.

Files Checkin

This will catch all upgrades, uploads, settings file changes, etc. Anything you change on the actual filesystem where your wiki is stored will be commited to the repository.

export repo =
# Check in the files
cd /path/to/your/wiki

# Add all new, edited, and deleted files
git add . -A
# Commit our changes
git commit -m "Routine Checkin"
# Push the commit to the files branch of our repository
git push origin files

Database Checkin

For this we are going to take a database dump and overwrite the old one with it. We will then check in the same file, but with the changes. Again, any changes made to pages, users, logs, etc will be in the dump file and thus will be commited to the repository.

dbFileName = ""
$password = "CheckMeYo"
$dumpPath = /path/to/dump/backups/
mysqldump -u wikiUser -p$pass 'databaseName' > $dumpPath$dbFileName
cd $dumpPath
git add . -A
git commit -m "Routine Checkin"
# Push the commit to the database branch of our repository
git push origin database

Restoring from Backups

Restoring from a backup is actually quite simple. All one needs to do is fetch the repository (origin).

  • Firstly, pull the database branch and run a mysqlimport on the dump file.

  • Secondly, to get the files (and overwrite any current files), do a

git pull --rebase origin files

and the most recent version of the files branch will show up in the current directory.

Also, if you worry someone will download your .git directory contents, you can just move the .git directory out when you aren’t doing backups and back in temporarily for a backup.

Size Concerns

Git has the capability to compress repositories using the git gc command. This will have git go back through all of the commits in the working repository and compress them in the context of all of the other commits. Currently my wiki plaintext database dump is 50 megabytes. It has been checked in to the repository 18 times and the entire repository is about 17.5 megabytes after a "git gc". Neat, huh?

Category:Git Category:Backups