How to connect git clone for archiving (tar or gzip)

I am trying to make a simple backup script for my remote git repositories. 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 do all this in one line? Can I include a git clone in the tar command? I don’t need the cloned directory, I just want its compressed archive.

I tried some experiments, but can't understand the syntax.

+8
git pipe gzip tar
source share
2 answers

No, you cannot just pass git clone because it does not write it to standard output. And why do you need one liner? They are great for showing off that you can do something cool in just one line, but not very perfect in the real world.

You can do something like below, but you won't get .git , as in 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. 
+11
source share

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

http://www.kernel.org/pub/software/scm/git/docs/git-archive.html

+3
source share