Backing up all Repositories for an Organisation on GitHub
Because backups are important!
Imagine your internet connection is down, maybe you loose your access keys, your account is suspended or another disaster that stops you accessing your github account. What do you do to access that "Super Important Project" that you need the code for to deliver an urgent project, well you may have to turn to a backup. Of course having access to your git projects in this kind of event needs some preparation beforehand.
Passing this backup script the organisation's name and a valid personal access token, it will pull down all the projects that that organisation owns and keep them in your safe place.
Lets have a look at how to do this:
Create a Personal Access Token
Creating a token on GitHub isn't too difficult, you can follow this guide to create one. It takes just a few mins, its good to note these keys are like passwords and should be handled securely, the keys are also pretty easy to revoke should you need too.
Running the Script
This script takes 3 placement conditional arguments:
- Organisation Name
- Personal Access Token
- Destination Folder
$ ./ngd-git-backup.sh notarealorganisation dGhpc2lzbm90dGhldG9rZW55b3V3ZXJlbG9va2luZ2Zvcgo= ~/git/backup
Running the above will create the organisation folder with all it's owned repositories underneath it. You should see lots of git cloning and pulling if this script is behaving correctly. If it's a new repo the script will clone, however if it has been cloned before the script simply does a git pull. Placing this into a cronjob allows regular project backups to happen.
Here is an example cron that will clone or git pull your containers every hour:
0 * * * * your_user /path_to_script/ngd-git-backup.sh notarealorganisation dGhpc2lzbm90dGhldG9rZW55b3V3ZXJl
If you have any changes to suggest do open them on GitHub and we'll get back to you.
The code for this project can be found here: