views:

685

answers:

5

Hey,

Go easy on me, I'm not much of a command line man... We have GIT set up within our windows network (using msysgit & GitExtensions). We each have our own repositories and we push to a remote 'bare' repository on one of our servers. All good.

I'm trying to set up a scheduled job on the server, which will clone a repository from the C drive to an external drive (on F) - having some difficulty getting this to work. I can do this in GIT bash relatively easily, but I'm not sure how to save this into a batch file that I can then scehdule.

What I have so far:

rmdir F:\GitClone /s /q
mkdir F:\GitClone
mkdir F:\GitClone\Repo1
CD /D F:\GitClone\Repo1\
GIT CLONE /c/GIT/Repo1/

I've also tried the following for the last line:

GIT CLONE C:\GIT\Repo1\

But this doesn't work either... I'm a little stumped and would appreciate some help. The C drive contains our bare repositories and the F drive being our external drive that we swap out daily...


Several answers here that have been very useful, thanks. My resulting answer is probably a combination of these, so points for pointing out how to run a bash script and how to script the pull/push.

Need to bring these together to work so that it's happy when various drives are swapped in and out (i.e. clone a repository if it doesn't exist on the external drive and then only pull the differences otherwise), but that should be doable. Thanks to all.

+3  A: 

You could always just schedule bash.exe mybashbackupscript.sh

Anyway, in windows terms:

rmdir F:\GitClone /s /q
mkdir -p F:\GitClone\Repo1
copy c:\GIT\Repo1\.git F:\GitClone\Repo1\.git

git clone doesn't really do anything fancier than that.

edit: as someone else pointed out, it's probably best to just make a new backup repo and just pull/fetch into that. You'll avoid any accesing issues with the .git :)

Pod
That's kinda my thinking at the minute.
Paddy
+2  A: 

Because git command is little bit weird you have to use call to execute any git commands from a batch file:

rmdir F:\GitClone /s /q
mkdir F:\GitClone
CD /D F:\GitClone\
call GIT CLONE c/GIT/Repo1/
Igor Zevaka
+1  A: 

Sorry, I can't comment posts, but I was thinking too about copying the .git, but what happens if the .git is copyed during a pull ?

Anyway, why copying the whole stuff as you could fetch the deltas from time to time ?

Initialization of backup : (in F:\GitClone\Repo1 empty)

git init
git add remote origin /c/GIT/Repo1

Then your "delta backup script" would just do :

cd /f/GitClone/Repo1
git fetch origin
Tryum
+11  A: 

Please note that git itself is excellent at copying only the needed changes to a cloned repository.

If you want a copy of your repo to be regularly updated, do this: You create a bare repository as a backup repository, and then repeatedly push all new changes there (no need to delete the old backup).

Ok, let's start by creating your repo

$ cd /tmp
$ mkdir myrepo && cd myrepo
$ touch hi && git add . && git commit -m "bla" 

So, this is your repository. Now we create the clone:

$ cd /tmp
$ mkdir backup && cd backup 
$ git --bare init
Initialized empty Git repository in /tmp/backup/

Now, let's set up your repo for regular backups …

$ cd /tmp/myrepo
$ git remote add backup /tmp/backup
$ git config remote.backup.mirror true

Then copy everything to the backup:

$ git push backup
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/backup
 * °new branch§      master -> master

And see if it worked:

$ cd /tmp/backup
$ git log
commit d027b125166ff3a5be2d7f7416893a012f218f82
Author: Niko Schwarz <niko.schwarzàgmail.com>
Date:   Fri Dec 11 12:24:03 2009 +0100

    hi

Tada, you're set. Therefore, all your script needs to do is to issue git push backup. There's exactly no need to repeatedly throw away the old backup.

The alternative is you can have rsync do it all for you:

rsync -av rsync://rsync.samba.org/ftp/unpacked/rsync /dest/dir/

User Offby adds: Since version 1.5.4, "git remote add" takes a "--mirror" option, which saves you both from having to "git config remote.origin.mirror true", and from having to pass --mirror to "git push".

nes1983
why are you rsyncing from a bare repo? It will have no checked out files. If you --exclude=.git/, then you will esentially be copying nothing.Also, why is your repositroy pushing to the backup? Why doesn't the backup pull from the repo?
Pod
I'm not, I'm proposing two different ways: rsyncing, or copying to a bare repo for copying. And to my mind, it's more natural to push a backup than to pull it.
nes1983
Ah, now I see what you mean, sorry, the exclude statement slipped in by mistake.
nes1983
+1: a bare repo is all you need/want for backups. It's likely, though, that you actually want to do "git push --mirror origin" rather than "git push origin master" to ensure that *all* the branches are backed up. Or indeed just set "git config remote.origin.mirror true" and then "git push origin". And maybe call the remote "backup" instead of "origin" ;)
araqnid
araqnid, yea … that's all correct … working on it
nes1983
Thanks araqnid :)
nes1983
Since version 1.5.4, "git remote add" takes a "--mirror" option, which saves you both from having to "git config remote.origin.mirror true", and from having to pass --mirror to "git push".
offby1
A: 

Why do you delete the clone all the time? What is the point of cloning an existing repository when you just want to copy the file?

Just create the repo on the external drive (using git clone once), and then run git pull on it regularly.

Aaron Digulla
These drives get swapped in and out on a daily basis - I want to ensure that it gets created if not already there.
Paddy