17

I am trying to make a simple backup script for my remotely hosted git repos. In the script I have a few lines that currently look like this:

git clone git@server:repo.git $DEST
tar czvf repo.tgz $DEST
rm -rf $DEST

Is there a way to make this all happen in one line? Can I pipe the git clone into the tar command? I don't need the cloned directory, I just want the compressed archive of it.

I've tried some experiments but can't seem to figure out the syntax.

bryan kennedy
  • 6,969
  • 5
  • 43
  • 64

2 Answers2

22

No you cannot just pipe git clone because it does not write it out to the standard output. And why do you need a one-liner? They are great for boasting that you can do something cool in just one line, but not really ideal in real world.

You can do something like below, but you would not get .git like you would in a git clone :

git archive --format=tar --remote=git@server:repo.git master | tar -xf -

From git archive manual

--remote=<repo>

    Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository.
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 6
    The `--remote` option to `git archive` requires that the server has an option enabled to allow it. By default it is not enabled. – Arrowmaster May 13 '11 at 21:59
  • If I understand it correctly git archive is only useful as a backup of the files contained in a git repo. Right? What I would like to do is create a full backup of the repo and all the versioning information contained within. So I think I still want to use git clone. – bryan kennedy May 14 '11 at 04:28
  • 1
    As to doing it on one line, yes, it is "cool" for sure. However I was also trying to prevent cloning a big repo then copying a compression of it. And then deleting that copy. I was trying to be efficient with disk writes, but since you state that git clone isn't stdout I guess I just have to go with what I was planing before. – bryan kennedy May 14 '11 at 04:32
  • @bryan kennedy I thought I clearly mentioned `but you would not get .git like you would in a git clone` – manojlds May 14 '11 at 04:32
  • 1
    @bryan kennedy - if you are doing a backup, why are cloning again and again? Just do a `--mirror` and have it in sync. – manojlds May 14 '11 at 04:38
  • @manojlds - Ah thanks for pointing me to the mirror flag. Hadn't seen that before. This helps with my goal to make a fully recoverable off site backup of my hosted git repos. – bryan kennedy May 14 '11 at 04:49
  • @Arrowmaster What option needs to be enabled to allow it? – dtmland Jul 10 '18 at 15:06
  • @Arrowmaster UPDATE - Found it: https://git-scm.com/docs/git-daemon#git-daemon-upload-archive – dtmland Jul 10 '18 at 15:13
6

You can use git archive for this purpose with the --remote option.
You can pipe it to create zip or tar or whatever you like.

Please, see the git-archive(1) manual page.

rtn
  • 127,556
  • 20
  • 111
  • 121