Stop Losing Code - Automate Your GitHub Backups with Bash Scripts
Step-by-step guide to secure and automate code backups using GitHub and Bash.
Are you tired of losing track of your projects and code scattered across multiple machines? Youโre not alone. I used to have this bad habit of never checking in my code, which left me hunting down files across old laptops and virtual machines. That changed when I automated my GitHub workflow with simple Bash scripts. Hereโs how you can automate GitHub backups and secure your code effortlessly.
๐ Table of Contents
- Why You Should Automate Code Backups
- Automating GitHub Repository Setup
- Securing GitHub Authentication via SSH and API Tokens
- Automating Code Sync and Git Push
- Final Thoughts and Next Steps
๐ Why You Should Automate Code Backups
Manual backups fail becauseโฆ well, weโre human. Automating this process ensures that your projects are always safe, versioned, and recoverableโeven if your machine crashes tomorrow.
๐ก Pro Tip: Automating GitHub repository creation and code check-ins not only saves time but also improves your DevOps hygiene.
๐ง Automating GitHub Repository Setup
While working on my Ethical Hacking Robot project, I realized my code wasnโt backed up anywhere. This led me to create a Bash script that automatically links new projects to GitHub repositories using a configuration file.
๐๏ธ Configuration Variables (setup_config.sh):
USERNAME
โ Linux user running the projectGITHUB_REPO
โ Target GitHub repository URLGIT_USER_NAME
โ GitHub account nameGIT_USER_EMAIL
โ GitHub emailGITHUB_API_TOKEN
โ GitHub API token (retrieved securely at runtime)
๐ Sample Script:
1
2
3
4
5
6
#!/bin/bash
source setup_config.sh
LOGFILE="/home/$USERNAME/setup.log"
exec > >(tee -a "$LOGFILE") 2>&1
bash setup_github.sh "$USERNAME" "$GITHUB_REPO" "$GIT_USER_NAME" "$GIT_USER_EMAIL" "$GITHUB_API_TOKEN"
๐ Securing GitHub Authentication via SSH and API Tokens
Security first! Instead of hardcoding sensitive credentials, I store the GitHub API token in a secure file. The script then generates SSH keys and configures GitHub authentication automatically.
1
2
3
4
5
6
sudo -u $USERNAME ssh-keygen -t rsa -b 4096 -C "$USERNAME@$(hostname)" -f $USER_HOME/.ssh/id_rsa -N ""
echo "Host github.com
User git
IdentityFile ~/.ssh/id_rsa
StrictHostKeyChecking no" | sudo -u $USERNAME tee $USER_HOME/.ssh/config > /dev/null
๐ก Quick Security Tip: Always restrict permissions on sensitive files like .github_token
using chmod 600
.
๐ฆ Automating Code Sync and Git Push
Now that authentication is set, itโs time to sync your local code with GitHub automatically.
๐ Define Source and Destination Paths:
1
2
3
SRC_ROS2="/home/ros2_dev/ros2_ws/src/"
SRC_NON_ROS="/home/ros2_dev/non_ros_code/"
DEST_GITHUB="/home/ros2_dev/github/dev/"
๐ Sync Code Using Rsync:
1
2
rsync -av --exclude='build/' --exclude='install/' --exclude='log/' "$SRC_ROS2" "$DEST_GITHUB/src/"
rsync -av "$SRC_NON_ROS" "$DEST_GITHUB/non_ros_code/"
๐ค Automate Git Commit and Push:
1
2
3
git add dev/
git commit -m "Automated backup on $(date)"
git push origin main
๐ก Pro Tip: Schedule this script using cron
for fully automated daily or hourly backups.
๐ Final Thoughts and Next Steps
This workflow has saved me countless hours and secured my codebase across multiple projects. But itโs still a work in progress. Next, I plan to:
- Replace hardcoded paths with dynamic variables.
- Add full error handling and logging.
- Publish a fully polished version of these scripts as a public GitHub repository.
๐ Check out the full working scripts here!
If you found this helpful, share it with your network or drop a comment below! What automation hacks are you using to improve your workflow?