Skip to content

Instantly share code, notes, and snippets.

@tylerhall
Created March 27, 2019 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tylerhall/4fbdbdb3d0bc56e3604115c5805e8305 to your computer and use it in GitHub Desktop.
Save tylerhall/4fbdbdb3d0bc56e3604115c5805e8305 to your computer and use it in GitHub Desktop.
Sync all of my GitHub repos to a backup server every night
#!/bin/bash
# You need a GitHub token with repo access for your account. You can create one here...
# https://github.com/settings/tokens
GITHUB_TOKEN=abc123
cd ~
mkdir github
cd github
# Use GitHub's API to fetch all the repos your user has access to.
# This includes public, private, and any you have read access to with other organizations.
# The API output is JSON, so filter for just the lines that contain a git clone URL,
# extract out that URL, and git clone it.
#
# The API returns 100 repos per API call. If you have more than that, you need to add enough
# pages to the for loop to fetch everything. I'm too lazy to use the API properly to figure out
# how many pages there actually are, I just know I have about 150 repos. So, I need to fetch 2
# pages. Adjust this for the number of repos in your account.
for PAGE in 1 2
do
curl -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/user/repos?per_page=100&page=$PAGE" | grep -w ssh_url | cut -d \" -f 4 | xargs -L1 git clone
done
# Pipe the contents of the directory containing all of our repos to a couple
# git commands to keep them up to date each night. I'm not a git expert, so if
# there is a better git command to fetch everything, let me know. This seems to
# work for my needs.
ls | xargs -L1 -I repo git -C repo fetch --all
ls | xargs -L1 -I repo git -C repo pull origin master # Not really necessary, but whatever.
# Finally, rsync everything to our backup server.
cd ~
rsync -avz github/ user@server.com:github
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment