Automate Your Git Backups in 15 Minutes Flat
One hard drive failure away from losing everything? Here’s the 15-minute fix.
I used to be the guy who kept telling himself, “I’ll set up proper backups this weekend.” That weekend never came. Until it did—when my laptop’s SSD started throwing SMART errors and I realized my dotfiles, scripts, and project configs existed in exactly one place.
Sound familiar?
This is a quick guide to automating your backups with git. Not fancy cloud sync. Not expensive backup software. Just git, a script, and a cron job that runs while you sleep.
What We’re Building
A simple system that:
- Backs up your important files to a remote git repo
- Runs automatically on a schedule
- Sends you a notification if something breaks
- Can be restored on any machine in minutes
Time required: 15 minutes
Cost: $0 (if you use GitHub/GitLab free tier)
What You Need
- Git installed
- A remote git repository (GitHub, GitLab, Gitea, or your own server)
- Basic terminal comfort
- The willingness to not overthink this
Step 1: Create Your Backup Repository
First, create a private repo on your platform of choice. Name it something like backups or dotfiles or my-precious-configs.
Don’t make it public. You’re backing up config files, API keys might lurk in there.
Clone it locally:
git clone git@github.com:yourusername/backups.git ~/backups
cd ~/backups
Step 2: The Backup Script
Create ~/backups/backup.sh:
#!/bin/bash
# Backup script - runs daily via cron
set -e
BACKUP_DIR="$HOME/backups"
REPO_DIR="$BACKUP_DIR/repo"
LOG_FILE="$BACKUP_DIR/backup.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
# What to backup (customize this)
BACKUP_SOURCES=(
"$HOME/.bashrc"
"$HOME/.zshrc"
"$HOME/.ssh/config"
"$HOME/.config/nvim"
"$HOME/.openclaw"
"$HOME/scripts"
)
echo "[$DATE] Starting backup..." >> "$LOG_FILE"
# Copy files to repo directory
for source in "${BACKUP_SOURCES[@]}"; do
if [ -e "$source" ]; then
# Maintain directory structure
rel_path="${source#$HOME/}"
target_dir="$REPO_DIR/$(dirname "$rel_path")"
mkdir -p "$target_dir"
cp -r "$source" "$target_dir/"
echo "[$DATE] Backed up: $source" >> "$LOG_FILE"
fi
done
# Commit and push
cd "$REPO_DIR"
git add -A
if git commit -m "Backup: $DATE" >> "$LOG_FILE" 2>&1; then
git push origin main >> "$LOG_FILE" 2>&1
echo "[$DATE] Backup completed successfully" >> "$LOG_FILE"
else
echo "[$DATE] No changes to backup" >> "$LOG_FILE"
fi
Make it executable:
chmod +x ~/backups/backup.sh
Customize the BACKUP_SOURCES array for your setup. Add what matters to you. Remove what doesn’t.
Step 3: Test It
Run it once manually:
~/backups/backup.sh
Check your remote repo. You should see your files there. Check the log:
tail ~/backups/backup.log
If it works, move on. If not, read the error message and fix it. This is the part where most people give up. Don’t be most people.
Step 4: Automate It
Open your crontab:
crontab -e
Add this line for daily backups at 6 PM:
0 18 * * * /home/yourusername/backups/backup.sh
Or twice daily (6 AM and 6 PM):
0 6,18 * * * /home/yourusername/backups/backup.sh
Save and exit. The system will now run your backup automatically.
Pro tip: The first few times, check your repo to make sure commits are appearing. After that, trust but verify—check it weekly.
Step 5: Add Notifications (Optional but Recommended)
You want to know if your backup fails. Here’s a simple notification using notify-send (Linux) or osascript (macOS):
Add this to the end of your backup.sh:
# Notify on completion
if [ $? -eq 0 ]; then
notify-send "Backup Complete" "Your files are safely backed up" --icon=dialog-information
else
notify-send "Backup Failed" "Check ~/backups/backup.log for details" --icon=dialog-error
fi
For Telegram or Discord notifications, you can curl a webhook URL instead. I use OpenClaw to ping me, but a simple curl to a webhook works fine.
What I Actually Backup
Here’s my real BACKUP_SOURCES for transparency:
BACKUP_SOURCES=(
"$HOME/.bashrc"
"$HOME/.zshrc"
"$HOME/.ssh/config"
"$HOME/.config/nvim/init.vim"
"$HOME/.openclaw/workspace"
"$HOME/scripts"
"$HOME/.himalaya/config.toml"
)
Total size: ~50MB
Backup frequency: Twice daily
Retention: Git history forever (or until I care enough to prune it)
I don’t back up large files. Photos and documents go to my NAS. This is for configs, scripts, and the “oh right, I had a script for that” moments.
Restoring (The Part You Hope You Never Need)
When your laptop dies or you get a new machine:
git clone git@github.com:yourusername/backups.git ~/backups-temp
# Copy what you need back into place
cp -r ~/backups-temp/repo/.openclaw ~/
cp ~/backups-temp/repo/.bashrc ~/
# etc.
Done. You’re back in business.
Common Gotchas
“My repo is huge because I accidentally backed up node_modules”
Create ~/backups/repo/.gitignore:
node_modules/
*.log
.DS_Store
__pycache__/
“I committed a file with an API key”
Use BFG Repo-Cleaner or rewrite history. Then rotate that key immediately.
“I forgot about this for 3 months and my backup stopped working”
Set a calendar reminder to check your repo monthly. Or add a cron job that notifies you if no commits happened in the last 48 hours.
The Bottom Line
This isn’t enterprise-grade backup strategy. It’s pragmatic. It’s good enough.
Your configs are safe. Your scripts are versioned. You can sleep at night.
Total time invested: 15 minutes
Peace of mind: Priceless
What’s Next?
This was a Project Friday post—quick tutorials for weekend builds. If this was useful, you might also like:
- Set up Blogwatcher in 30 minutes (RSS monitoring for your interests)
- Build a temperature alert system (home server monitoring)
- Create a daily briefing bot (morning automation)
Or tell me: what else should be automated? Drop a commen.
