tags:

views:

223

answers:

3

I am wondering if there is an easy way, ie like a simple cron job, to regularly pull from a remote git repository to a local read only mirror for backup purposes?

Ideally it would pull all branches and tags, but the master/trunk/head would be sufficient.

I just need a way to make sure that if the master git server dies, we have a backup location that we could manually fail over to.

+5  A: 

First create a mirror with

git clone --mirror [email protected]:repo.git

then setup a cron job like this:

*/1 * * * * gitbackup cd /backup/repo.git && git fetch -q

This will backup the changesets every minute. Maybe you want to do this less frequently.

gregor
That is great. I knew there would be a simple answer! (I was so close)
corydoras
A: 

do you have direct access to the server? then you could just rsync the .git directory

knittl
Depending on the platform, simply copying a git repo can lead to problems, for example due to permissions not being propagated from the source to the destination. Cloning is therefore a better solution, hence -1 for this answer.
Gareth Stockwell
rsync can take care of that. furthermore, i often find that pull does not pull remotes, stashes and other important information
knittl
i really don't see how this answer is *so bad*? maybe it's not the best solution, but it's definitely a working solution if you have direct access to the server. if you rsync directly to a tar-archive you don't even have problems with permissions …
knittl
i still don't get what is **wrong** with this answer? can anyone explain?
knittl
how about: it's just not so safe? it might work now, but that's accidental; who's to guarantee it'll work a year from now?
hasen j
@hasen, i guess permissions models just don't change overnighht. if it works today it should work in a year too—and if the binary format changes, i'm sure we will be informed
knittl
for all I know, git can change its formats, allow linking to other directories that hold part of the object database, or whatever.
hasen j
Despite the answer not really answering the question (its good to think outside the box), I am fairly sure rsync would not guarantee a working functional copy of the repository if an rsync is running in the middle of a commit to the git repository.
corydoras
@corydas, i think it will sync a working copy ;) heads get updated last and git's internal data storage is well designed and robust
knittl
this is fun. already 5 downvotes :D
knittl
5? how do you know that, it says -2 for me
corydoras
@corydoras, -5 +3 = -2 ;)
knittl
+1  A: 

As Andrew noted, every clone of a git repo is a full-fledged backup of the repo. That said, if you want something backed up automatically to a particular machine, you can create a bare repo on the backup server, push into it with all the branches you want backed up in order to initially populate it. Then just setup a post update hook on the "main" repo so that as soon as there are commits pushed in, it goes ahead and pushes them to the backup repo. No need for a cron job or rsync, and its an almost live copy.

jcordasc
+1 for adding a solution that is good in general (and hence a good reference) but doesn't answer the question. We are asking how to pull as we need to pull through a NAT
corydoras