# backup git repo on every commit

14 messages from 2009-10-12 to 2009-10-13. Participants: Israel Garcia, Christian Himpel, Shawn O. Pearce.
Thread: https://gitlist.dev/t/21212

## Israel Garcia, 2009-10-12 13:41

Subject: backup git repo on every commit
Message-ID: <194a2c240910120641sccf0e55xef4226269df78864@mail.gmail.com>
URL: https://gitlist.dev/e/194a2c240910120641sccf0e55xef4226269df78864%40mail.gmail.com

```
Hi list, a simple question.

Which is the simplest  way to backup a git repository after every commit?

-- 
Regards;
Israel Garcia

```

## Christian Himpel, 2009-10-12 14:08

Subject: Re: backup git repo on every commit
Message-ID: <20091012140838.GB29984@lamagra.informatik.uni-ulm.de>
URL: https://gitlist.dev/e/20091012140838.GB29984%40lamagra.informatik.uni-ulm.de
In-Reply-To: <194a2c240910120641sccf0e55xef4226269df78864@mail.gmail.com>

```
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, 2009-10-12 14:15

Subject: Re: backup git repo on every commit
Message-ID: <20091012141544.GF9261@spearce.org>
URL: https://gitlist.dev/e/20091012141544.GF9261%40spearce.org
In-Reply-To: <194a2c240910120641sccf0e55xef4226269df78864@mail.gmail.com>

```
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, 2009-10-12 14:25

Subject: Re: backup git repo on every commit
Message-ID: <194a2c240910120725u72cdb588p3c66bc730e6953d@mail.gmail.com>
URL: https://gitlist.dev/e/194a2c240910120725u72cdb588p3c66bc730e6953d%40mail.gmail.com
In-Reply-To: <20091012141544.GF9261@spearce.org>

```
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:
> 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, 2009-10-12 14:30

Subject: Re: backup git repo on every commit
Message-ID: <20091012143043.GJ9261@spearce.org>
URL: https://gitlist.dev/e/20091012143043.GJ9261%40spearce.org
In-Reply-To: <194a2c240910120725u72cdb588p3c66bc730e6953d@mail.gmail.com>

```
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.

```

## Israel Garcia, 2009-10-12 14:39

Subject: Re: backup git repo on every commit
Message-ID: <194a2c240910120739j58cc1ec0o629b0df7d31e3fdd@mail.gmail.com>
URL: https://gitlist.dev/e/194a2c240910120739j58cc1ec0o629b0df7d31e3fdd%40mail.gmail.com
In-Reply-To: <20091012143043.GJ9261@spearce.org>

```
On 10/12/09, Shawn O. Pearce <spearce@spearce.org> wrote:
> 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.

>
>> 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

```

## Christian Himpel, 2009-10-12 18:35

Subject: Re: backup git repo on every commit
Message-ID: <20091012183504.GA3872@mrslave>
URL: https://gitlist.dev/e/20091012183504.GA3872%40mrslave
In-Reply-To: <20091012141544.GF9261@spearce.org>

```
On Mon, Oct 12, 2009 at 07:15:44AM -0700, Shawn O. Pearce wrote:
> 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, 2009-10-12 18:37

Subject: Re: backup git repo on every commit
Message-ID: <20091012183754.GP9261@spearce.org>
URL: https://gitlist.dev/e/20091012183754.GP9261%40spearce.org
In-Reply-To: <20091012183504.GA3872@mrslave>

```
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.

```

## Israel Garcia, 2009-10-13 16:43

Subject: Re: backup git repo on every commit
Message-ID: <194a2c240910130943j40c12902o760e463e7a8ce8fa@mail.gmail.com>
URL: https://gitlist.dev/e/194a2c240910130943j40c12902o760e463e7a8ce8fa%40mail.gmail.com
In-Reply-To: <20091012143043.GJ9261@spearce.org>

```
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:
> 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, 2009-10-13 17:49

Subject: Re: backup git repo on every commit
Message-ID: <20091013174913.GV9261@spearce.org>
URL: https://gitlist.dev/e/20091013174913.GV9261%40spearce.org
In-Reply-To: <194a2c240910130943j40c12902o760e463e7a8ce8fa@mail.gmail.com>

```
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.

```

## Israel Garcia, 2009-10-13 18:08

Subject: Re: backup git repo on every commit
Message-ID: <194a2c240910131108h10acbacdy68306c9c3389517@mail.gmail.com>
URL: https://gitlist.dev/e/194a2c240910131108h10acbacdy68306c9c3389517%40mail.gmail.com
In-Reply-To: <20091013174913.GV9261@spearce.org>

```
On 10/13/09, Shawn O. Pearce <spearce@spearce.org> wrote:
> 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

```

## Israel Garcia, 2009-10-13 18:14

Subject: Re: backup git repo on every commit
Message-ID: <194a2c240910131114q19b6c822t5806d20005341cb4@mail.gmail.com>
URL: https://gitlist.dev/e/194a2c240910131114q19b6c822t5806d20005341cb4%40mail.gmail.com
In-Reply-To: <20091013174913.GV9261@spearce.org>

```
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:
> 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, 2009-10-13 18:18

Subject: Re: backup git repo on every commit
Message-ID: <20091013181813.GY9261@spearce.org>
URL: https://gitlist.dev/e/20091013181813.GY9261%40spearce.org
In-Reply-To: <194a2c240910131114q19b6c822t5806d20005341cb4@mail.gmail.com>

```
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.

```

## Shawn O. Pearce, 2009-10-13 18:18

Subject: Re: backup git repo on every commit
Message-ID: <20091013181843.GZ9261@spearce.org>
URL: https://gitlist.dev/e/20091013181843.GZ9261%40spearce.org
In-Reply-To: <194a2c240910131108h10acbacdy68306c9c3389517@mail.gmail.com>

```
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.

```
