Mirroring git respositories to DreamHost

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.

# 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.

# 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
# get our directory name and split off the project name
# replace spaces with underscores in the project name
projectName=${baseProjectName// /_}
# remove any other troublesome characters
# 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"
	echo "git push failed"
	exit 1

I imagine that this will work for any server to which you can ssh.