Create a Custom Bash Script for Daily Git Backups
Maintaining up-to-date backups of your code repositories is a smart development practice. Accidental deletions, hardware failures, or even bad pushes can lead to irreversible data loss. Automating backups using a Bash script ensures your project is securely stored and timestamped every day without manual effort. In this post, we’ll walk through creating a Bash script that compresses your Git repository, adds a timestamp, and uploads it to cloud storage using rclone.
Sections:
1. Why Automate Git Backups?
Manual backups are error-prone and often forgotten. Automating your backup routine ensures consistency and minimizes data loss. In the case of Git repositories, it’s especially important because not all files are pushed to remote (e.g., ignored files, notes, or building artifacts). An automated local backup complements your Git pushes with a reliable archive.
Let’s make a script to:
- Create a compressed tar.gz archive of your project folder
- Add a timestamp to preserve historical backups
- Upload the archive to remote cloud storage (Google Drive, Dropbox, S3, etc.)
2. Preparing Your Environment
Before writing the script, make sure you have the following installed:
tar– For creating compressed archives (usually pre-installed)rclone– A powerful command-line tool for syncing files to cloud storage
Installing rclone:
curl https://rclone.org/install.sh | sudo bash
Configuring remote storage:
rclone config
This command walks you through setting up a remote destination, such as Google Drive or Dropbox. Name your remote (e.g., mydrive) when configuring—it will be required in the upload step.
3. Creating the Bash Script
Let’s write the core script that compresses your Git repo into a uniquely named archive.
#!/bin/bash
# --- User Configs ---
REPO_PATH="$HOME/projects/my-awesome-repo"
BACKUP_DIR="$HOME/git-backups"
REMOTE_NAME="mydrive"
REMOTE_PATH="git-backups"
# --- Date and Filename ---
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
ARCHIVE_NAME="$(basename "$REPO_PATH")_$TIMESTAMP.tar.gz"
# Create backup directory if not exists
mkdir -p "$BACKUP_DIR"
# --- Archiving ---
tar -czf "$BACKUP_DIR/$ARCHIVE_NAME" -C "$(dirname "$REPO_PATH")" "$(basename "$REPO_PATH")"
# --- Upload to Cloud ---
rclone copy "$BACKUP_DIR/$ARCHIVE_NAME" "$REMOTE_NAME:$REMOTE_PATH"
# --- Optional: Remove local archive after upload ---
# rm "$BACKUP_DIR/$ARCHIVE_NAME"
echo "Backup $ARCHIVE_NAME successfully created and uploaded."
This script is easy to customize. Replace REPO_PATH and REMOTE_NAME with real values. It compresses the whole project directory, adds a readable timestamp, and syncs to your cloud.
4. Automating with Cron
Cron jobs are ideal for scheduling tasks in Unix-based systems. To run your script daily, edit your crontab:
crontab -e
Add the following line to run the script every day at 11 PM:
0 23 * * * /path/to/git_backup.sh >> $HOME/logs/git_backup.log 2>&1
This not only automates the job, but also logs output for troubleshooting. Make sure the script is executable:
chmod +x /path/to/git_backup.sh
5. Tips, Optimizations, and Safeguards
Here are some useful tips to improve your backup setup:
- Use SSH keys for security: If your repo includes sensitive files, ensure your cloud integrations are secure.
- Ignore unnecessary files: Modify the
tarcommand to skip large or unwanted files using--exclude. - Use retention policies: Automate deletion of backups older than X days:
find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +30 -exec rm {} \;
This command deletes backups older than 30 days – great for saving space.
Parallelization Tip: If you back up multiple repos, run them in parallel with a background job pattern:
# In a wrapper script
bash backup_repo1.sh &
bash backup_repo2.sh &
wait
Conclusion
Creating a Bash script to handle daily Git backups is a simple yet powerful form of automation. It’s an elegant safeguard against data loss and a helpful practice for solo developers and teams alike. You now have a ready-to-use solution to compress, archive, and upload your repositories with confidence. Happy scripting!
Useful links:


