Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shaposhnikoff/0b0c75f38fd3aea3a92af06872cf9e11 to your computer and use it in GitHub Desktop.
Save shaposhnikoff/0b0c75f38fd3aea3a92af06872cf9e11 to your computer and use it in GitHub Desktop.
Backup Github organization - enterprise
from github import Github
import re,logging
from subprocess import call
token="ghp_###########"
g = Github(base_url="https://code.some.organization/api/v3", login_or_token=token)
regexp = ['regular-expression-*']
user = g.get_user()
def backup_all():
for repo in g.get_organization("organization_name").get_repos():
logging.info(repo.full_name)
for reg in regexp:
if re.match(reg, repo.name):
print(repo.full_name)
logging.info(f"Repository: {repo.full_name}, URL: {repo.html_url}")
call(["git", "clone", repo.ssh_url, "organization_dir/" + repo.full_name])
def backup_starred():
for starred_repo in g.get_user().get_starred():
print(starred_repo.full_name,starred_repo.stargazers_count)
logging.info(f"Repository: {starred_repo.full_name}, URL: {starred_repo.html_url}")
call(["git", "clone", starred_repo.ssh_url, "starred_repos/" + starred_repo.full_name])
if __name__ == "__main__":
print("This script will backup all repositories from your organization with this regexp " + str(regexp) + '\n' +
"or all repositories you starred in your personal account." + '\n\n')
backup_type = input("What type of backup do you want to do - all or starred ? (a/s): ")
if backup_type == "a":
backup_all()
elif backup_type == "s":
backup_starred()
else:
print("Wrong input. Please try again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment