Backup multiple git bare resoritories with git clone --mirror
-------------------------------------------------------------
Last edited: $Date: 2015/10/26 11:40:41 $
## git clone --mirror
For almost everything I do, I make use of a version control system.
I started out with CVS many years ago. Later I migrated my entire
collection of CVS repositories to Git.
Now I have a great number of bare repositories on my git-server and
used rsync to make a backup of these repositories.
Git offers the --mirror option to make perfect backups, so I wrote a
little scripts to use that option to keep a backup.
This is what this script looks like:
#!/bin/sh
backupdir=/path/to/backup/mirrors
cd $backupdir
for file in `ssh git@git.example.com ls`
do
echo $file
if [ -d "$file.git" ]; then
cd $backupdir/$file.git; git remote update; cd $backupdir/
else
git clone --mirror git@git.example.com:$file
fi
done
This script runs on the system where the backup lives.
* backupdir : this is the directory where the backups will be
pushed
* git@git.example.com : this is the user and the server where the
original bare repositories are
The scripts first asks a list of all available repositories on my
local git server, the server where the original bare repositories
are.
This is done with the line
for file in `ssh git@git.example.com ls`
Then it iterates over this list and for each repository checks if a
directory for this already exits. If so, it does a git remote
update, of not it creates a new mirror with clone --mirror.
## ssh key and authorized_keys
In order to let the backup scripts do its work unattended, set up
ssh- keys so the script can reach the local git server without the
need for passwords.
Put the .pub part of the key from the user on the backupserver in
the authorized_keys file on the local git server. Check to see if
you can login without the need to enter a password.
## Testing the script
Create the backupdir the backup-server and run the script. This will
create a number of directories, each with a mirror of the git
repository in case.
Clone a repository from the local git server, make some changes and
push those back to the local git server.
Run the backup-script again and check that there is actual some
output on this particular repository (lines like "remote: Counting
objects: 4, done." et cetera).
## Testing the backup
> A backup that is not tested is not a backup!
You should test your backups regular, so you know for sure you can
count on them when needed.
So do a git clone from the backupserver (and not from the local git
server) to see if the backup really works.
## Run the script from cron
Now the final step is to let cron do nightly backups
Setup a line for this with crontab -e, perhaps something like this:
12 2 * * * /path/to/the/backup-script >/dev/null 2>&1
This will run the script every night at 2:12.
Next morning, check to see if your scripts has worked.
$Id: gitbackup.txt,v 1.2 2015/10/26 11:40:41 matto Exp $