Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple Python script for copying an organization's repositories
#!/usr/bin/env python3
# Requires
# Set the GITHUB_TOKEN to a personal access token with all repo scopes
import os
import subprocess
import sys
from github import Github
from optparse import OptionParser
token = os.getenv('GITHUB_TOKEN')
if token is None:
print("Must set GITHUB_TOKEN")
parser = OptionParser()
parser.add_option("-u", "--user", dest="user", help="User to use for authentication")
parser.add_option("-o", "--org", dest="org", help="Organization to back up")
(options, args) = parser.parse_args()
g = Github(token)
os.putenv('GIT_USER', options.user)
if name =
org = g.get_organization(name)
if not os.path.exists(name): os.mkdir(name)
for repo in org.get_repos(type='all'):
git_path = os.path.join(name, '{}.git'.format(
helper_sh = '!f() { echo \'username=${GIT_USER}\'; echo \'password=${GITHUB_TOKEN}\'; }; f'
if os.path.exists(git_path):'cd {} && git -c credential.helper="{}" fetch --all'.format(git_path, helper_sh), shell=True, check=True)
else:'cd {} && git -c credential.helper="{}" clone --mirror --bare {}'.format(name, helper_sh, repo.clone_url), shell=True, check=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment