Mirroring git respositories to DreamHost
June 11th, 2009
Casper Fabricius has a nice shell script for creating new git repositories on DreamHost. Unfortunately, this works only for creating new repositories.
I already have a lot of local git repositories, and this seemed like a much better way to back them up off-site than using github or similar. This is useful only for repositories without collaborators, of course.
Here’s the shell script. I used Bash just because that’s my normal shell.
All disclaimers disclaimed. This is not bulletproof.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/bin/bash # # Create a mirror of a local git repos on DreamHost. # Run this in the root directory of an existing git repository. # Note that this sets the "origin" remote. # # Derived from http://casperfabricius.com/site/2008/09/21/keeping-git-repositories-on-dreamhost-using-ssh/ # # change this to your ssh login. DREAMGIT_DOMAIN=user@domain.com # ensure that we're at the top of a git repos if [ ! -d .git ]; then echo "The current directory is not a git repository" exit 1 fi # get our directory name and split off the project name dirPath=`pwd` baseProjectName=${dirPath##/*/} # replace spaces with underscores in the project name projectName=${baseProjectName// /_} # remove any other troublesome characters projectName=${projectName//[^-a-zA-Z0-9_.]/} # make the remote mirror on DreamHost ssh $DREAMGIT_DOMAIN 'mkdir -p ~/git/'$projectName'.git && cd ~/git/'$projectName'.git && git --bare init' # set the remote as the default push and pull target git remote add origin --mirror ssh://$DREAMGIT_DOMAIN/~/git/$projectName.git # push all to it echo "Pushing everything to remote..." if git push -v origin; then echo "Git mirror at '$DREAMGIT_DOMAIN/git/$projectName.git' all set" else echo "git push failed" exit 1 fi |
I imagine that this will work for any server to which you can ssh.