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.
- 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)
- 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
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.
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
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"
######## 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"
#######################################################
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
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
- General instructions from github: https://docs.github.com/en/enterprise-server@3.4/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
- Navigate to your main profile page
- Click on "settings" in the dropdown menu located right next to your profile picture in the upper right hand corner