threads / discuss / 21212

backup git repo on every commit

Subject: backup git repo on every commit

## tl;dr

14 messages between Oct 12, 2009 and Oct 13, 2009.

replies: 13people: 3as markdown or json

Christian Himpel· Oct 12, 2009, 14:08 UTC · re: Israel Garcia · lore

Re: backup git repo on every commit

On Mon, Oct 12, 2009 at 08:41:15AM -0500, Israel Garcia wrote:
> Which is the simplest  way to backup a git repository after every commit?
Using githooks(5) you can put a simple post-commit script in .git/hooks.

Maybe something like: -->8-- #!/bin/sh

cp -a `pwd` `pwd`.`date +%s` --8<--

Don't forget to make the script executable.

Regards, chressie

Shawn O. Pearce· Oct 12, 2009, 14:15 UTC · re: Israel Garcia · lore

Re: backup git repo on every commit

Israel Garcia <igalvarez@gmail.com> wrote:
> Which is the simplest  way to backup a git repository after every commit?
Add a commit hook to push to another location, e.g.:
  git remote add --mirror backup you@another.host:path/to/backup.git
  cat >.git/hooks/post-commit
  #!/bin/sh
  git push backup
  ^D
  chmod a+x .git/hooks/post-commit
-- 
Shawn.
Israel Garcia· Oct 12, 2009, 14:25 UTC · re: Shawn O. Pearce · lore

Re: backup git repo on every commit

Hi Shawn,

That's OK, but I want to backup my git repo locally because I don't have another remote git server. Can you modify your post-commit code to backup the repo to /backups/git folder instead of using another external git repo?

thanks in advance. regards Israel.

On 10/12/09, Shawn O. Pearce <spearce@spearce.org> wrote:
Show 17 quoted lines
> Israel Garcia <igalvarez@gmail.com> wrote:
>> Which is the simplest  way to backup a git repository after every commit?
>
> Add a commit hook to push to another location, e.g.:
>
>   git remote add --mirror backup you@another.host:path/to/backup.git
>
>   cat >.git/hooks/post-commit
>   #!/bin/sh
>   git push backup
>   ^D
>
>   chmod a+x .git/hooks/post-commit
>
> --
> Shawn.
>
-- 
Regards;
Israel Garcia
Shawn O. Pearce· Oct 12, 2009, 14:30 UTC · re: Israel Garcia · lore

Re: backup git repo on every commit

Israel Garcia <igalvarez@gmail.com> wrote:
> That's OK, but I want to backup my git repo locally 

Just change the path of the backup remote (that final argument to git remote add) to point to the local directory.

Though I guess you would also need to run git init there, e.g.:
  git --git-dir=/backup/project.git init
  git remote add --mirror backup /backup/project.git
  # and create the hook as below

Of course, backup to another folder on the same disk isn't a backup at all, the disk can still fail and lose both repositories.

Show 14 quoted lines
> On 10/12/09, Shawn O. Pearce <spearce@spearce.org> wrote:
> > Israel Garcia <igalvarez@gmail.com> wrote:
> >> Which is the simplest  way to backup a git repository after every commit?
> >
> > Add a commit hook to push to another location, e.g.:
> >
> >   git remote add --mirror backup you@another.host:path/to/backup.git
> >
> >   cat >.git/hooks/post-commit
> >   #!/bin/sh
> >   git push backup
> >   ^D
> >
> >   chmod a+x .git/hooks/post-commit
-- 
Shawn.
Israel Garcia· Oct 12, 2009, 14:39 UTC · re: Shawn O. Pearce · lore

Re: backup git repo on every commit

On 10/12/09, Shawn O. Pearce <spearce@spearce.org> wrote:
Show 12 quoted lines
> Israel Garcia <igalvarez@gmail.com> wrote:
>> That's OK, but I want to backup my git repo locally
>
> Just change the path of the backup remote (that final argument to
> git remote add) to point to the local directory.
>
> Though I guess you would also need to run git init there, e.g.:
>
>   git --git-dir=/backup/project.git init
>   git remote add --mirror backup /backup/project.git
>
>   # and create the hook as below
Nice..:-)
>
> Of course, backup to another folder on the same disk isn't a backup
> at all, the disk can still fail and lose both repositories.

Yeap, but I use rsync to backup /backup folder on a remote server every night. /backup folder other apps backups.

thanks Shawn.
regards
Israel.
Show 20 quoted lines
>
>> On 10/12/09, Shawn O. Pearce <spearce@spearce.org> wrote:
>> > Israel Garcia <igalvarez@gmail.com> wrote:
>> >> Which is the simplest  way to backup a git repository after every
>> >> commit?
>> >
>> > Add a commit hook to push to another location, e.g.:
>> >
>> >   git remote add --mirror backup you@another.host:path/to/backup.git
>> >
>> >   cat >.git/hooks/post-commit
>> >   #!/bin/sh
>> >   git push backup
>> >   ^D
>> >
>> >   chmod a+x .git/hooks/post-commit
>
> --
> Shawn.
>
-- 
Regards;
Israel Garcia
Israel Garcia· Oct 13, 2009, 16:43 UTC · re: Shawn O. Pearce · lore

Re: backup git repo on every commit

Hi Shawn,

Sorry to ask again, but I'm a little confuse about how git work in my case. I use gitosis on a server where I have all repos (/usr/local/git/repositories/), so different people clone their repos on their computers. What I want is to backup, on gitosis server, all repos in /backups/git/repositories/ after every commit. So, my questions are:

Do I have to run these two comands on tha gitosis server? Or on a
remote computer?
  git --git-dir=/backup/project.git init
  git remote add --mirror backup /backup/project.git
The post-commit are execute on gitosis server or on the remote pc?
I'm completely new using git so I'm a little confuse.. sorry for asking again.

thanks a lot. regards Israel. On 10/12/09, Shawn O. Pearce <spearce@spearce.org> wrote:

Show 35 quoted lines
> Israel Garcia <igalvarez@gmail.com> wrote:
>> That's OK, but I want to backup my git repo locally
>
> Just change the path of the backup remote (that final argument to
> git remote add) to point to the local directory.
>
> Though I guess you would also need to run git init there, e.g.:
>
>   git --git-dir=/backup/project.git init
>   git remote add --mirror backup /backup/project.git
>
>   # and create the hook as below
>
> Of course, backup to another folder on the same disk isn't a backup
> at all, the disk can still fail and lose both repositories.
>
>> On 10/12/09, Shawn O. Pearce <spearce@spearce.org> wrote:
>> > Israel Garcia <igalvarez@gmail.com> wrote:
>> >> Which is the simplest  way to backup a git repository after every
>> >> commit?
>> >
>> > Add a commit hook to push to another location, e.g.:
>> >
>> >   git remote add --mirror backup you@another.host:path/to/backup.git
>> >
>> >   cat >.git/hooks/post-commit
>> >   #!/bin/sh
>> >   git push backup
>> >   ^D
>> >
>> >   chmod a+x .git/hooks/post-commit
>
> --
> Shawn.
>
-- 
Regards;
Israel Garcia
Shawn O. Pearce· Oct 13, 2009, 17:49 UTC · re: Israel Garcia · lore

Re: backup git repo on every commit

Israel Garcia <igalvarez@gmail.com> wrote:
Show 8 quoted lines
> Sorry to ask again, but I'm a little confuse about how git work in my case.
> I use gitosis on a server where I have all repos
> (/usr/local/git/repositories/), so different people clone their repos
> on their computers. What I want is to backup, on gitosis server, all
> repos in /backups/git/repositories/ after every commit. So, my
> questions are:
> 
> Do I have to run these two comands on tha gitosis server?
Yes.
> The post-commit are execute on gitosis server or on the remote pc?

Actually, you need the post-update hook. post-commit doesn't run on the gitosis server.

I suggested post-commit because I thought you were talking about
backing up your local working directory each time you called
"git commit".  But since you are actually backing up every
"git push" you need to use the hooks invoked by that instead.
 
-- 
Shawn.
Israel Garcia· Oct 13, 2009, 18:08 UTC · re: Shawn O. Pearce · lore

Re: backup git repo on every commit

On 10/13/09, Shawn O. Pearce <spearce@spearce.org> wrote:
Show 22 quoted lines
> Israel Garcia <igalvarez@gmail.com> wrote:
>> Sorry to ask again, but I'm a little confuse about how git work in my
>> case.
>> I use gitosis on a server where I have all repos
>> (/usr/local/git/repositories/), so different people clone their repos
>> on their computers. What I want is to backup, on gitosis server, all
>> repos in /backups/git/repositories/ after every commit. So, my
>> questions are:
>>
>> Do I have to run these two comands on tha gitosis server?
>
> Yes.
>
>> The post-commit are execute on gitosis server or on the remote pc?
>
> Actually, you need the post-update hook.  post-commit doesn't run
> on the gitosis server.
>
> I suggested post-commit because I thought you were talking about
> backing up your local working directory each time you called
> "git commit".  But since you are actually backing up every
> "git push" you need to use the hooks invoked by that instead.
Hi Shawn,

Thanks for your answer, could you please put here how you think post-update file should be setup? Remember in my case I want to backup every repo to /backups/git folder for example.

Thanks in advance. regrads

Israel.
>
> --
> Shawn.
>
-- 
Regards;
Israel Garcia
Shawn O. Pearce· Oct 13, 2009, 18:18 UTC · re: Israel Garcia · lore

Re: backup git repo on every commit

Israel Garcia <igalvarez@gmail.com> wrote:
> Thanks for your answer,  could you please put here how you think
> post-update file should be setup? Remember in my case I want to backup
> every repo to /backups/git folder for example.

Exactly the same as that post-commit I mentioned, just call the file .git/hooks/post-update instead.

-- 
Shawn.
Israel Garcia· Oct 13, 2009, 18:14 UTC · re: Shawn O. Pearce · lore

Re: backup git repo on every commit

BTW, is there any git-dump or git-backup command to do some kind of backup I'm looking for?

regards,
Israel.
On 10/13/09, Shawn O. Pearce <spearce@spearce.org> wrote:
Show 26 quoted lines
> Israel Garcia <igalvarez@gmail.com> wrote:
>> Sorry to ask again, but I'm a little confuse about how git work in my
>> case.
>> I use gitosis on a server where I have all repos
>> (/usr/local/git/repositories/), so different people clone their repos
>> on their computers. What I want is to backup, on gitosis server, all
>> repos in /backups/git/repositories/ after every commit. So, my
>> questions are:
>>
>> Do I have to run these two comands on tha gitosis server?
>
> Yes.
>
>> The post-commit are execute on gitosis server or on the remote pc?
>
> Actually, you need the post-update hook.  post-commit doesn't run
> on the gitosis server.
>
> I suggested post-commit because I thought you were talking about
> backing up your local working directory each time you called
> "git commit".  But since you are actually backing up every
> "git push" you need to use the hooks invoked by that instead.
>
> --
> Shawn.
>
-- 
Regards;
Israel Garcia
Shawn O. Pearce· Oct 13, 2009, 18:18 UTC · re: Israel Garcia · lore

Re: backup git repo on every commit

Israel Garcia <igalvarez@gmail.com> wrote:
> BTW, is there any git-dump or git-backup command to do some kind of
> backup I'm looking for?

No, you backup git by making a clone. E.g. `git clone --bare`. Since this leaves you with a directory, you need to then perhaps use some sort of file combiner tool like tar or zip to produce a single file for backup to tape.

You can incrementally update that backup clone using git push to write into it, or you can just blow it away and recreate it each time you make a backup.

One could also use `git bundle` to create backup file that had
everything packaged in one neat file, but this can be slightly
harder to work with since a bundle is not a git repository.
 
-- 
Shawn.
Christian Himpel· Oct 12, 2009, 18:35 UTC · re: Shawn O. Pearce · lore

Re: backup git repo on every commit

On Mon, Oct 12, 2009 at 07:15:44AM -0700, Shawn O. Pearce wrote:
Show 9 quoted lines
> Israel Garcia <igalvarez@gmail.com> wrote:
>   git remote add --mirror backup you@another.host:path/to/backup.git
> 
>   cat >.git/hooks/post-commit
>   #!/bin/sh
>   git push backup
>   ^D
> 
>   chmod a+x .git/hooks/post-commit

Since this is meant as a backup, is there also a sane way to push the reflog entries as well? Or is it okay just to copy .git/logs to the backup repository?

Thanks, chressie

Shawn O. Pearce· Oct 12, 2009, 18:37 UTC · re: Christian Himpel · lore

Re: backup git repo on every commit

Christian Himpel <chressie@googlemail.com> wrote:
> Since this is meant as a backup, is there also a sane way to push the
> reflog entries as well?  Or is it okay just to copy .git/logs to the
> backup repository?

There isn't a way to transfer reflogs, but one could rsync them over to the backup. Some records might be missing their SHA-1 in the backup if that particular object wasn't pushed there, but the reflog code is generally tolerant of those garbage records.

-- 
Shawn.

← back to recent threads