Backup Git Repositories You Depend On

By Eric Lathrop on

The recent news of youtube-dl being taken down from GitHub due to a DMCA takedown notice from the RIAA provides a good reminder that you should maintain backups of any software that you depend on.

With git clone --mirror it's easy to keep a backup copy of any repo. I've written a couple shell scripts you can put into cron job to keep repos you care about updated.

git-backup will either clone or update a repo:

#!/bin/bash
set -e

if [ $# -lt 1 ]; then
  echo "Usage: $(basename "$0") git-repo-url"
  exit 1
fi

folder=$(basename "$1")

if [ -d "$folder" ]; then
  pushd "$folder" || exit
  git remote update
  popd || exit
else
  git clone --mirror "$1"
fi

git-backup-all will run git-backup for each repo listed in a text file:

#!/bin/bash
set -e

if [ $# -lt 1 ]; then
  echo "Usage: $(basename "$0") file-containing-git-repo-urls"
  exit 1
fi

xargs -a "$1" -n1 git-backup

You can add it to your crontab like this:

# daily at 10am
0 10 * * * cd /path/to/my/git/backups && git-backup-all repos.txt