Blog

My Backup Strategy: Tar, Rsync, and Cron

How I automated daily, weekly, and monthly backups of my Code and Work directories to my Android phone via Termux.
4 min read
On this page
  1. What I'm Backing Up
  2. Three-Tier Backup System
  3. The Backup Scripts
  4. Daily Backup Script
  5. Weekly Backup Script
  6. Monthly Backup Script
  7. Remote Sync with Rsync to Termux
  8. Cron Schedule
  9. Cron Syntax Explained
  10. Restore from Backup
  11. Key Takeaways

While working on production deployments and writing my Production Linux Deployment Guide, I realized I didn't have a robust backup system in place, neither for personal laptop nor for the servers I manage. Across both environments, I store years of critical data: projects, infrastructure, configs, notes, and operational scripts. Losing any of it would be catastrophic. So I designed a three-tier backup system using tar, rsync, and cron that runs automatically and fits both personal and production use cases.

What I'm Backing Up

I backup two critical directories:

DirectoryDescriptionBackup Priority
~/CodePersonal projects and side venturesCritical
~/WorkWork-related files and documentsCritical

Both directories live on my laptop, and I sync them to my Android phone running Termux for off-site backup.


Three-Tier Backup System

I use a classic rotation strategy: daily, weekly, and monthly backups with different retention periods.

ScheduleTimeRetentionPurpose
Daily12:15 AM7 daysQuick recovery from recent changes
Weekly12:30 AM (Monday)30 daysRecover from accidental deletes
Monthly12:45 AM (1st of month)365 daysLong-term archive

This ensures I always have multiple recovery points without hoarding endless backups.


The Backup Scripts

Daily Backup Script

bash
#!/bin/bash
# backup-daily.sh - Runs at 12:15 AM daily
# Retention: 7 days
 
BACKUP_DIR="/home/ssk/backup/daily"
DATE=$(date +%Y-%m-%d)
 
# Backup ~/Code
tar -zcf "$BACKUP_DIR/code-$DATE.tar.gz" "$HOME/Code"
 
# Backup ~/Work
tar -zcf "$BACKUP_DIR/work-$DATE.tar.gz" "$HOME/Work"
 
# Remove backups older than 7 days
find "$BACKUP_DIR"/* -mtime +7 -delete

Weekly Backup Script

bash
#!/bin/bash
# backup-weekly.sh - Runs at 12:30 AM every Monday
# Retention: 30 days
 
BACKUP_DIR="/home/ssk/backup/weekly"
DATE=$(date +%Y-%m-%d)
 
tar -zcf "$BACKUP_DIR/code-$DATE.tar.gz" "$HOME/Code"
tar -zcf "$BACKUP_DIR/work-$DATE.tar.gz" "$HOME/Work"
 
find "$BACKUP_DIR"/* -mtime +30 -delete

Monthly Backup Script

bash
#!/bin/bash
# backup-monthly.sh - Runs at 12:45 AM on the 1st of each month
# Retention: 365 days
 
BACKUP_DIR="/home/ssk/backup/monthly"
DATE=$(date +%Y-%m-%d)
 
tar -zcf "$BACKUP_DIR/code-$DATE.tar.gz" "$HOME/Code"
tar -zcf "$BACKUP_DIR/work-$DATE.tar.gz" "$HOME/Work"
 
find "$BACKUP_DIR"/* -mtime +365 -delete

Make them executable:

bash
chmod +x backup-daily.sh backup-weekly.sh backup-monthly.sh

Remote Sync with Rsync to Termux

The local backups protect against disk failure, but I wanted off-site backup too. My Android phone is always nearby, so I installed Termux and set up an SSH server:

bash
# In Termux
pkg install openssh
sshd

Now I sync my local backup directory to my phone:

bash
rsync -a --delete /home/ssk/backup/ ssk@192.168.1.xxx:~/backup/

The --delete flag ensures the remote mirrors the local directory exactly—files deleted locally get removed on the phone too.

I use SSH key authentication so rsync runs without a password prompt:

bash
# Generate key on laptop (if you haven't)
ssh-keygen -t ed25519
 
# Copy to phone
ssh-copy-id ssk@192.168.1.xxx

Cron Schedule

Open crontab with crontab -e and add:

bash
# Daily backup at 12:15 AM
15 0 * * * /home/ssk/backup-daily.sh
 
# Weekly backup at 12:30 AM (Monday)
30 0 * * 1 /home/ssk/backup-weekly.sh
 
# Monthly backup at 12:45 AM (1st of month)
45 0 1 * * /home/ssk/backup-monthly.sh
 
# Sync to phone at 2:00 AM
0 2 * * * rsync -a --delete /home/ssk/backup/ ssk@192.168.1.xxx:~/backup/

Cron Syntax Explained

FieldMeaning
15 0 * * *Minute 15, hour 0 (12:15 AM), every day
30 0 * * 1Minute 30, hour 0, every Monday
45 0 1 * *Minute 45, hour 0, day 1 of month

Restore from Backup

When disaster strikes, recovery is straightforward:

bash
# Extract a specific backup
tar -xzf /home/ssk/backup/daily/code-2026-03-08.tar.gz -C ~/
 
# Extract to a specific directory
tar -xzf /home/ssk/backup/daily/work-2026-03-08.tar.gz -C /tmp/restore/

Key Takeaways

  1. Three-tier rotation - Daily/weekly/monthly gives you multiple recovery points without accumulating endless backups
  2. Off-site matters - Local backups protect against disk failure; remote sync protects against theft/loss
  3. Automate everything - Cron runs backups on schedule so you never forget
  4. Test restores - Backups are useless if you can't recover from them

Having a backup system in place gives me peace of mind. My files sync automatically to my phone every night, and I can recover from any point in the past year if needed. It's not perfect—no encryption yet—but it's a solid foundation I can improve over time.

Keep reading