← Back to list

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.

SSK · 2026-03-21 10:39 · 1 claps · 3.2 min read
#backup-and-restore #linux #cronjob #rsync #ssh
Open on Medium ↗
Wiki topics: 🔓 · Open Source 🎬 · Film & Television

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.

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 my 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:

| Directory | Description                              | Backup Priority |
|-----------|------------------------------------------|-----------------|
| `~/Code`  | Personal projects and side ventures      | Critical        |
| `~/Work`  | Work-related files and documents         | Critical        |

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 classical rotation strategy: daily, weekly, and monthly backups with different retention periods.

| Schedule | Time                     | Retention | Purpose                                |
|----------|--------------------------|-----------|----------------------------------------|
| Daily    | 12:15 AM                 | 7 days    | Quick recovery from recent changes     |
| Weekly   | 12:30 AM (Monday)        | 30 days   | Recover from accidental deletes        |
| Monthly  | 12:45 AM (1st of month)  | 365 days  | Long-term archive                      |

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

The Backup Scripts

Daily Backup Scripts

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

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

# 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

| Field          | Meaning                                      |
|----------------|----------------------------------------------|
| `15 0 * * *`   | Minute 15, hour 0 (12:15 AM), every day      |
| `30 0 * * 1`   | Minute 30, hour 0, every Monday              |
| `45 0 1 * *`   | Minute 45, hour 0, day 1 of month            |

Restore from Backup

When disasters 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.


메타데이터
post_id
2d37f8dace1a
slug
my-backup-strategy-tar-rsync-and-cron-2d37f8dace1a
url
https://medium.com/@2ssk/my-backup-strategy-tar-rsync-and-cron-2d37f8dace1a
canonical_url
https://medium.com/@2ssk/my-backup-strategy-tar-rsync-and-cron-2d37f8dace1a
author_url
https://medium.com/@2ssk
status
ok
fetched_at
2026-06-09 18:04:40