Post

Stop Losing Code - Automate Your GitHub Backups with Bash Scripts

Step-by-step guide to secure and automate code backups using GitHub and Bash.

Stop Losing Code - Automate Your GitHub Backups with Bash Scripts

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

  1. Why You Should Automate Code Backups
  2. Automating GitHub Repository Setup
  3. Securing GitHub Authentication via SSH and API Tokens
  4. Automating Code Sync and Git Push
  5. 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 project
  • GITHUB_REPO โ€“ Target GitHub repository URL
  • GIT_USER_NAME โ€“ GitHub account name
  • GIT_USER_EMAIL โ€“ GitHub email
  • GITHUB_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?

This post is licensed under CC BY 4.0 by the author.