Skip to content

A backup script for GitHub repositories

License

Notifications You must be signed in to change notification settings

henri/github-backup

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub backup script

This directory contains a script, backup.py, for backing up GitHub repositories.

The script requires a GitHub token and a destination directory. It then uses the token to populate the destination directory with clones of all the repositories the token can access.

Repeated runs only update the already existing backups and add new repositories, if any.

Installation

Install the required Python dependencies using pip3:

$ pip3 install -r requirements.txt

Configuring

Create a token

For authorization you need to create a new personal GitHub token. You can do this from the GitHub settings, under the Personal Access Tokens tab.

Step 1

When you click the Generate new token button you enter the token creation screen. Here you should give the token a descriptive name and choose its scopes, which basically determine what the token is allowed to do.

Step 2

To backup public and private repositories you need to select only the repo scope. If you have no need for private repositories just choose the public_repo scope.

Step 3

After clicking the Generate token button you're presented with the generated token. Remember to store it now, as GitHub won't show it to you anymore!

In the next example let's assume your token is 6b86190dd45c57c1a1b039a5a54d892e019102f7 as in the above image.

Create a configuration file

To run the script you need a JSON configuration file. For an example see the included file config.json.example.

As an example let's create a file, config.json. This file should contain the token we just created and the destination directory where we want to back up the repositories:

{
    "token": "6b86190dd45c57c1a1b039a5a54d892e019102f7",
    "directory": "~/backups/github.com"
}

Choose some repositories to exclude from the backup.

By default, all repositories you have read access to are backed up. To exclude some repos, add exclude-repos to config.json:

{
    "token": "6b86190dd45c57c1a1b039a5a54d892e019102f7",
    "directory": "~/backups/github.com",
    "exclude-repo": ["dont-need-this-repo", "forked-repo-to-skip", "the-giant-repo-to-skip"]
}

Keep in mind that if a repo has already been backed up it will not be deleted. But it will no longer continue to be updated with subsequient backups. You could manually delete the excldued repository and it would not come back when you run the backup script again, as the repository was excluded.

Choose users and organizations to back up

By default, all repositories you have read access to are backed up. To choose which users' and organizations' repos are backed up, add owners to config.json:

{
    "token": "6b86190dd45c57c1a1b039a5a54d892e019102f7",
    "directory": "~/backups/github.com",
    "owners": ["username", "anotherusername"]
}

Running

After preparing the token and the configuration file you now can run the script:

$ python3 backup.py config.json

About

A backup script for GitHub repositories

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%