BetaIT-Hub is in early access — your feedback helps us improve. Use the chat or email [email protected]

Tutorials/BACKUP RECOVERY/Backup Strategy with rsync and Cron
BeginnerBACKUP RECOVERY5 min read30 views

Backup Strategy with rsync and Cron

Set up reliable automated backups using rsync and cron. Covers local backups, remote sync, rotation, and verification — no expensive backup software needed.

A
adminEliteStaff
Published 65d ago

Backup Strategy with rsync and Cron

You don't need expensive backup software. rsync + cron gives you reliable, automated backups for free. This guide sets up a solid backup strategy you can deploy in minutes.

Prerequisites

  • Linux server(s)
  • SSH key access between servers (for remote backups)

Step 1: Understand rsync Basics

# Basic local sync
rsync -avz /source/ /destination/

Key flags:

-a Archive mode (preserves permissions, timestamps, etc.)

-v Verbose

-z Compress during transfer

--delete Remove files in destination that don't exist in source

Step 2: Local Backup Script

Create /usr/local/bin/backup.sh:

#!/bin/bash

Local backup script

DATE=$(date +%Y-%m-%d) BACKUP_DIR="/backups" SOURCE_DIRS="/etc /home /var/www /var/lib/postgresql" LOG="/var/log/backup.log"

echo "=== Backup started: $DATE ===" >> $LOG

for DIR in $SOURCE_DIRS; do DEST="$BACKUP_DIR/$(basename $DIR)" mkdir -p "$DEST" rsync -az --delete "$DIR/" "$DEST/" 2>> $LOG echo "Backed up: $DIR -> $DEST" >> $LOG done

echo "=== Backup completed: $(date) ===" >> $LOG

chmod +x /usr/local/bin/backup.sh

Step 3: Remote Backup

# Sync to a remote server
rsync -avz --delete /var/www/ user@backup-server:/backups/web/

With bandwidth limit (useful for large transfers)

rsync -avz --bwlimit=5000 /data/ user@backup-server:/backups/data/

Step 4: Database Backup

Add database dumps to your script:

# PostgreSQL
pg_dump -U postgres mydb | gzip > "$BACKUP_DIR/db/mydb-$DATE.sql.gz"

MySQL

mysqldump -u root mydb | gzip > "$BACKUP_DIR/db/mydb-$DATE.sql.gz"

Keep only last 14 days of DB dumps

find "$BACKUP_DIR/db/" -name "*.sql.gz" -mtime +14 -delete

Step 5: Automate with Cron

sudo crontab -e

# Daily backup at 3 AM
0 3   * /usr/local/bin/backup.sh

Weekly full backup to remote server on Sunday

0 4 0 rsync -avz --delete /backups/ user@backup-server:/remote-backups/

Step 6: Backup Rotation

Keep multiple versions without filling your disk:

# In your backup script, add rotation
WEEKLY_DIR="$BACKUP_DIR/weekly/$(date +%U)"
MONTHLY_DIR="$BACKUP_DIR/monthly/$(date +%Y-%m)"

Weekly snapshot (keep 4 weeks)

if [ "$(date +%u)" = "7" ]; then rsync -az --link-dest="$BACKUP_DIR/daily/" "$BACKUP_DIR/daily/" "$WEEKLY_DIR/" fi

Clean old weeklies

find "$BACKUP_DIR/weekly/" -maxdepth 1 -mtime +28 -exec rm -rf {} +

Step 7: Verify Your Backups

A backup you haven't tested is not a backup.

# Check backup integrity
rsync -avnc /source/ /backups/source/

-n = dry run, -c = checksum comparison

Test database restore

gunzip -c backup.sql.gz | psql -U postgres testdb

The 3-2-1 Rule

  • 3 copies of your data
  • 2 different storage media
  • 1 offsite copy

Conclusion

This simple setup gives you automated, reliable backups. The most common backup failure is never testing your restores — schedule a monthly restore test and you'll sleep much better.

Comments (0)

No comments yet. Be the first to share your thoughts.