How do I pipe a git clone to archive (tar or gzip)
Asked Answered
R

2

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.

Robin answered 13/5, 2011 at 16:58 Comment(0)
N
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.
Nominal answered 13/5, 2011 at 17:1 Comment(8)
The --remote option to git archive requires that the server has an option enabled to allow it. By default it is not enabled.Hurricane
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.Robin
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.Robin
@bryan kennedy I thought I clearly mentioned but you would not get .git like you would in a git cloneNominal
@bryan kennedy - if you are doing a backup, why are cloning again and again? Just do a --mirror and have it in sync.Nominal
@Nominal - 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.Robin
@Hurricane What option needs to be enabled to allow it?Kinesics
@Hurricane UPDATE - Found it: git-scm.com/docs/git-daemon#git-daemon-upload-archiveKinesics
K
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.

Knighterrant answered 13/5, 2011 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.