Locally Backup GitHub Repositories

Locally Backup GitHub Repositories

If you need to download your GitHub repositories, one option you have is to open each of them on GitHub and manually download/clone them. But, this can get cumbersome if you have so many repos. Also, if you are in an organization, for example your grad school lab, you have to manually find the repos you have contributed to. This is the problem /kanishkegb/github-backup-all is trying to automate.

The repo provides a simple python code to backup up all repositories owned by user, user’s organizations, and repositories owned by others which user has contributed. The code will clone new repositories if they do not exist in the local directory or pull new commits from GitHub if the repository already exists in the local directory. If a repository has submodules, they will be updated too.

What you just need to do is to run the code whenever you need to backup your GitHub repositories.

Backup will have the following structure:

+ GitHub
  + personal
    - repo_1
    - repo_2
  + contributed
    - repo_1
    - repo_2
  + organization_1
    - repo_1
    - repo_2
  + organization_2
    - repo_1
    - repo_2

Setting-Up

  1. Install dependencies
      pip install pygithub # GitHub API
    
  2. Create a personal access token for GitHub. Copy the token and do the following in the terminal before running the code. You MUST to do this every time you run the code. You can skip this if you add the line to ~/.bashrc or ~/.zshrc.
      export GH_ACCSS_TKN=personal_access_token_generated_in_GitHub
    
  3. Make sure that you are connected to GitHub with an ssh key.

Backing-Up

Run the code:

python backup_github_repos.py

For help and arguments, run:

python backup_github_repos.py -h