Last active
February 12, 2019 14:52
-
-
Save owenvoke/cc8d866a80e1eb8c76fa7676f7cb2610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Check that the GitHub token is set | |
# See: https://github.com/settings/tokens | |
if [ -z ${GITHUB_TOKEN} ]; then | |
echo "The 'GITHUB_TOKEN' environment variable must be set." | |
exit | |
fi | |
# Check that all required binaries are installed | |
# Requires: https://github.com/antonmedv/xx | |
required_binaries=( 'which' 'git' 'curl' 'xx' 'xargs' ) | |
for binary in ${required_binaries}; do | |
if [ -z $(which ${binary}) ]; then | |
echo "WARNING: '${binary}' is required for the backup process." | |
exit | |
fi | |
done | |
# Set default repository type (if environment is not set) | |
# Can be: all, owner (default), public, private, member | |
REPO_TYPE=${REPO_TYPE:-owner} | |
# Check how many pages are available from the API | |
repository_list=() | |
number_of_pages=$(curl -sI -H "Authorization: token ${GITHUB_TOKEN}" "https://api.github.com/user/repos?type=${REPO_TYPE}&page=1" | sed -nr 's/^Link:.*page=([0-9]+)>; rel="last".*/\1/p'); | |
# Loop through and add lists from each page | |
echo "Found '${number_of_pages}' pages of repositories" | |
pageNumber=1 | |
while [[ ${pageNumber} -le ${number_of_pages} ]]; do | |
# Download the repository list from GitHubs API and filter a repo name list with 'fx' | |
repository_list=(${repository_list[@]} $(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "https://api.github.com/user/repos?type=${REPO_TYPE}&page=${pageNumber}" | xx 'this.forEach(function (c) { console.log(c.full_name) })' 2> /dev/null | xargs echo)) | |
pageNumber=$((${pageNumber} + 1)) | |
done | |
# Loop through the repository list and clone or update | |
for repository in ${repository_list[@]}; do | |
if [ -d "${repository}.git" ]; then | |
# Pull any changes for a repository that already exists | |
echo "- Updating repository: ${repository}" | |
git -C "${repository}.git" pull &> /dev/null | |
else | |
# Clone repositories that don't already exist | |
echo "- Cloning repository: ${repository}" | |
git clone "git@github.com/${repository}" "${repository}.git" --bare &> /dev/null | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment