Skip to content

Instantly share code, notes, and snippets.

@petermolnar
Last active September 3, 2020 20:25
Show Gist options
  • Save petermolnar/4299b0ae03b8692e89e5b0dd36fb9ce6 to your computer and use it in GitHub Desktop.
Save petermolnar/4299b0ae03b8692e89e5b0dd36fb9ce6 to your computer and use it in GitHub Desktop.
backup-github
#!/usr/bin/env bash
function usage () {
echo "backup-github.sh
A tiny bash script to backup own and starred public github repositories
Usage:
backup-github.sh GITHUB_USERNAME DIRECTORY_TO_BACKUP_TO"
exit 1
}
if [ -z ${1} ] || [ -z ${2} ]; then
usage
fi
if [ '--help' == '${1}' ] || [ '-h' == '${1}' ]; then
usage
fi
GHUSER="${1}"
BASE="${2}"
if [ ! -d "${BASE}" ]; then
echo "missing basedir ${BASE}" 2>&1 | logger -s -t 'backup-github'
exit 1;
fi
declare -A repositories
repositories["_github_starred"]=$(wget -O- -q "https://api.github.com/users/${GHUSER}/starred?page=1&per_page=100" | json_pp | grep 'html_url.*github\.com\/.*\/' | sed -r 's/[^:]+:\s"([^"]+)".*/\1/g')
repositories["_github_own"]=$(wget -O- -q "https://api.github.com/users/${GHUSER}/repos?page=1&per_page=100" | json_pp | grep 'html_url.*github\.com\/.*\/' | sed -r 's/[^:]+:\s"([^"]+)".*/\1/g')
for key in "${!repositories[@]}"; do
SUB="${BASE}/${key}"
echo "working dir: ${SUB}"
if [ ! -d ${SUB} ]; then
mkdir ${SUB}
fi
while read repo; do
rname=$(basename "$repo")
tdir="${SUB}/${rname}"
if [ ! -d "${tdir}" ]; then
echo "cloning ${repo} to ${tdir}" 2>&1 | logger -s -t 'backup-github'
git clone "${repo}" "${tdir}" 2>&1 | logger -s -t 'backup-github'
cd "${tdir}"
else
echo "fetching ${repo}" 2>&1 | logger -s -t 'backup-github'
cd "${tdir}"
git fetch --all 2>&1 | logger -s -t 'backup-github'
fi
git submodule update --init --recursive 2>&1 | logger -s -t 'backup-github'
git prune 2>&1 | logger -s -t 'backup-github'
done <<< ${repositories[$key]}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment