Automate Daily Git Backups with Bash Scripts
Keeping your source code safe and versioned is one of the core principles of modern software development. Git is already a powerful tool for managing code changes, but there’s no rule that says you have to push changes manually every day. In this post, we’ll walk through how to create a Bash script that automatically commits and pushes changes in your local development directory to one or more remote repositories on a schedule. This is a practical approach to safeguarding your daily progress and maintaining consistent backups.
1. Why Automate Git Backups?
It’s easy to forget to commit or push your code at the end of a long day. Automating daily Git backups ensures your work is never lost, whether you accidentally delete a file, experience a hardware failure, or just want peace of mind.
The goal of the script we’ll create is to:
- Navigate to your project directory
- Add any file changes
- Automatically commit with a timestamped message
- Push to one or more remote Git repositories
Once complete, you can schedule the script using cron
to run daily or as often as needed.
2. Setting Up the Script
Let’s start by laying the foundation for our Bash script. Save the following as git_backup.sh
in your preferred scripts folder.
#!/bin/bash
# Project directory to back up
PROJECT_DIR="$HOME/my-dev-project"
# Change to project directory
cd "$PROJECT_DIR" || { echo "Directory not found: $PROJECT_DIR"; exit 1; }
# Git status check
git status
This base script confirms that the project directory exists and allows Git to read the current repo status. If you’re managing multiple projects, you can wrap this in a loop later.
3. Adding, Committing and Timestamping
Next, we’ll update the script to automatically stage all changes and create a timestamp-based commit message, which is great for tracking daily backups.
# Add and commit changes
git add .
# Check if there is anything to commit
if git diff --cached --quiet; then
echo "No changes to commit."
exit 0
fi
# Generate commit message
BACKUP_TIME=$(date "+%Y-%m-%d %H:%M:%S")
git commit -m "Automated backup: $BACKUP_TIME"
The git diff --cached --quiet
command ensures we don’t commit empty commits if nothing changed.
4. Pushing to Multiple Remotes
For added redundancy, you might want to back up to multiple remotes: GitHub, GitLab, a private bare repo, etc. Here’s how you can configure the script to push to all configured remotes.
# List of remotes to push to
REMOTES=("origin" "backup-remote")
for REMOTE in "${REMOTES[@]}"
do
echo "Pushing to $REMOTE..."
git push $REMOTE main || echo "Failed to push to $REMOTE"
done
Make sure you have added the remotes beforehand:
git remote add backup-remote git@yourserver.com:user/repo.git
This modular setup makes it easy to scale your backup strategy to multiple destinations.
5. Automating with Cron
To run your script daily, open your cron table for editing:
crontab -e
And add the following line to schedule your script every day at 6 PM:
0 18 * * * /path/to/git_backup.sh >> /path/to/backup.log 2>&1
Make sure your script is executable:
chmod +x /path/to/git_backup.sh
Redirecting stdout and stderr to a log file helps with debugging and auditing your automation.
6. Tips and Best Practices
- Use SSH keys for remote authentication to avoid password prompts in automated scripts.
- Scope your
git add
commands to specific files or directories if you want more control. - Consider logging commit hashes for verification later, e.g.:
git rev-parse HEAD >> backup.log
- Use Git hooks like pre-commit to run linters or formatters before automating backups.
Backup automation is a simple but powerful habit that can dramatically improve your development workflow. With Bash scripting and crontab, you can create lightweight, reliable Git backup pipelines tailored to your projects.
Conclusion
You’ve now set up a repeatable, flexible Git backup system using nothing but a Bash script and your OS scheduler. This is ideal for solo developers, side projects, or even teams looking to enforce daily version control hygiene. Git is a powerful safety net—automation makes it even better.
Happy hacking and safe coding!
Useful links: