Skip to content

A simple bash script to automate the backup of all your github repositories

Notifications You must be signed in to change notification settings

petrosvisconte/github_repo_backup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 

Repository files navigation

github_repo_backup

About:

A Bash script that automatically backs up all of your personal github repositories (private and public) to your local machine. For use on any linux machine.

Requirements:

  • git needs to be installed
  • You will first need to generate a github api token (instructions provided below)
  • You will need to modify some of the variables in the script (instructions provided below)
  • You may need to type a few commands in the CLI (instructions provided below)

Features:

  • Grabs the newest repository information from the user, meaning that any newely created repos will be added instantly as soon as the script is run
  • Zero maintenance required once setup
  • Will overwrite previously saved repositories to newest version so that any changes made to pre-existing repos are updated
  • Lightweight script that only uses built in bash commands

Install and configuration:

Note: If you do not already have an api token with repository access, then follow the instructions at the end of the README first to generate one, then come back here afterwards.

Installation:

Clone the repository and then copy the auto_backup file to the location of your choice. Please select a location for the script that will not get deleted or overwritten.

  • The following example assumes that you are in your home directory and wish to copy the scipt to a directory named "my_scripts"
git clone https://github.com/petrosvisconte/github_repo_backup.git
cp github_repo_backup/auto_backup.sh ~/my_scripts

Configuration:

Now open the script with the editor of your choice (vim is being used in this example)

vim /path/to/where/you/saved/auto_backup.sh

With the auto_backup script now open you will need to modify the following section:

######## CHANGE THE FOLLOWING VARIABLES BELOW #########
AUTH_TOKEN="paste your api key here"
BACKUP_DIR_NAME="name of the folder where you wish to save the backups"
PATH_TO_SCRIPT_DIR="the path to the directory that contains the auto_backup script"
PATH_TO_BACKUP_DIR="the path to the directory that contains BACKUP_DIR_NAME"
GITHUB_USERNAME="your github username"
#######################################################

AUTH_TOKEN:

  • This is the api key for your personal token. It must be connected to your account and have full access to your repositories

BACKUP_DIR_NAME:

  • This is the name of the folder where you wish to save the backups to
  • For example, if you wish to save your backup to ~/Documents/github/my_backups, then "my_backups" would be what you set this to

PATH_TO_SCRIPT_DIR:

  • The full path to the directory where the auto_backup script is located
  • For example, if you copied the script into ~/my_scripts, then you would set this to "/home/user/my_scripts"

PATH_TO_BACKUP_DIR:

  • The full path to the directory that contains the folder (BACKUP_DIR_NAME) where you wish to save the backups to. Do not include BACKUP_DIR_NAME in the path.
  • Using the example from BACKUP_DIR_NAME, you would set this to "/home/user/Documents/github"

GITHUB_USERNAME:

  • Your username for github
  • For example, I would use "petrosvisconte"

Example configuration:

######## CHANGE THE FOLLOWING VARIABLES BELOW #########
AUTH_TOKEN="deieiwaeianandiapomaaazod8492nsko"
BACKUP_DIR_NAME="my_backups"
PATH_TO_SCRIPT_DIR="/home/user/my_scripts"
PATH_TO_BACKUP_DIR="/home/user/Documents/github"
GITHUB_USERNAME="petrosvisconte"
#######################################################

Executing script:

Make the script executable

chmod +x /path/to/auto_backup.sh

You can now run the script whenever you like by entering:

./path/to/auto_backup.sh

Automating the script:

You can set the script to run at certain time intervals using whatever method you like, the following example sets the script as a crontask for automation

crontab -e

Append the following line to the end of the file

  • make sure to write out the full path when adding crontasks
@daily /full/path/to/auto_backup.sh

# using the example from before
@daily /home/user/my_scripts/auto_backup.sh

You can change the tag infront of the path to fit your needs

# runs the script every hour
@hourly
# runs the script every day
@daily
# runs the script every week
@weekly
# runs the script every month
@monthly
# runs the script every year
@yearly

Create a new github api token

  1. Navigate to your main profile page
  2. Click on "settings" in the dropdown menu located right next to your profile picture in the upper right hand corner

3. Scroll down to the bottom of settings and click on "developer settings"

4. Then click on "personal access tokens"

5. And then on "generate new token" (you may need to select "Tokens(classic)" first)

6. Enter what you wish for the note, and set the expiration as you would like. Just note that once it expires you will need to modify the script and replace the old token with the new one. You can set the expiration to "No expiration" if you would like to avoid this.

7. Select the scopes that the token has access to. You will need to select all the available options for "repo" to have full repository access. Any other scopes are optional, only full repository access is required for the script.

8. Now generate the token and copy the api key. You only have readable access to the key once so make sure to save it somewhere.

About

A simple bash script to automate the backup of all your github repositories

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages