cd ..
backup

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.

3 min read

After reading my Production Linux Deployment Guide, I realized I needed a proper backup system for my personal files. My laptop holds years of work—projects, notes, configurations—and losing it would be catastrophic. So I built a three-tier backup system using tar, rsync, and cron that runs automatically.

What I'm Backing Up

I back up 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

#!/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

#!/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

#!/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:

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:

# In Termux
pkg install openssh
sshd

Now I sync my local backup directory to my phone:

rsync -a --delete /home/ssk/backup/ ssk@192.168.1.100:~/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:

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

Cron Schedule

Open crontab with crontab -e and add:

# 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.100:~/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:

# 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.

More to Read