Backup git bare repository

Written by
Date: 2013-06-26 19:52:13 00:00


Introduction

Git is great to version control your work, and it is also good that is does not have a central repository, but a distributed authority.

Anyway, when you create bare repository to sync the job between different contributors, you are somehow in a "virtual" way creating a centralized authority for git. Usually contributors will clone this bare repository work with it, make changes, commit them and then upload (push) them to the bare repository.

Before going further, let's see what is a bare repository.

A git repository is usually divided in two parts, one is the history of your project, and it is made by a lot of files in a hidden folder named .git. The other part is your working folder, which is the current state of your job, as was created by git, with the info in the .git folder. In a bare repository, there is no working folder, just the history of your repository. In other words, all the info needed to create a working folder in its current state or any state it may have had in the past.

Bare repositories are very important when a lot of people is working on the same project. All of them can clone the bare repository with:

git clone [bare repository url or folder]

Work on it, commit changes and push the changes back to the bare repository. You can read more about this in this introduction to git article.

Now that we know how important bare repositories are, you may want to know how to back it up? Being git almost a backup system by it own, you may assume that all clones are backups, and that is true. But there is another way to be sure you have a complete backup of your bare repository. git clone --mirror.

The –mirror switch will do this:

–mirror

Set up a mirror of the source repository. This implies –bare. Compared to –bare, –mirror not only maps local branches of the source to local branches of the target, it maps all refs (including remote branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository.

Backup a bare repository with git clone –mirror

Assumptions

  • You have access via ssh to your bare repository
  • You are doing the backups in another server
  • You will be doing periodic backups automatically

To use this command and backup your bare repository in a timely basis, do this.

We will do this in a different server, so the backup is a real backup. Let's create a place to store it.

mkdir ~/my-git-backup
cd ~/my-git-backup

Then, clone the bare respository.

git clone --mirror user@server:/url-to-repo.git

Enter the ssh password, and you have your backup, it is time to enable automatic and periodic backups.

Prepare your server, where the backup is stored to access the one where the original bare repository is via ssh with no password

Then create a cronjob to update it every 5 minutes, edit the crontab

crontab -e

And add this line at the end.

*/5 * * * * cd ~/my-git-backup; git remote update