Home/Engineer/Linux / Shell/Safe File Synchronization an…
Linux / ShellIntermediateUbuntuDebianCentOSmacOSrsynclinux

Safe File Synchronization and Backup with rsync

A practitioner-focused guide covering rsync’s essential options, remote server synchronization, and automated incremental backups.

Contents
  1. rsync Basics
  2. Essential Options
  3. Local Synchronization
  4. Remote Server Synchronization
  5. Incremental Backup Script
  6. Performance Tuning

rsync Basics

Bash
rsync [] [] []

Essential Options

OptionMeaning
-aarchive mode (preserve permissions, owner, timestamps, and symbolic links)
-vverbose (print progress)
-zcompress during transfer (useful for remote transfers)
-Pshow progress and resume partial transfers
--deletedelete files that exist only on the destination (mirroring)
--dry-runpreview without actually running
--excludeexclude specific files/directories

Local Synchronization

Bash
# Basic copy (watch the trailing slash)
rsync -av /src/dir/ /dst/dir/

# Without a trailing slash, the directory itself is copied
rsync -av /src/dir  /dst/       # → /dst/dir/
rsync -av /src/dir/ /dst/dir/ # → /dst/dir/

# Mirroring (also delete files that disappeared from the destination)
rsync -av --delete /src/dir/ /dst/dir/

Remote Server Synchronization

Bash
# Local → remote upload
rsync -avz -P /local/app/ user@server:/opt/app/

# Remote → local download (backup)
rsync -avz user@server:/var/log/ /backup/logs/

# Specify SSH port
rsync -avz -e "ssh -p 2222" /local/ user@server:/remote/

# Exclude specific files
rsync -av --exclude="*.log" --exclude=".git" /src/ /dst/

Incremental Backup Script

Bash
#!/bin/bash
BACKUP_ROOT="/mnt/backup"
SOURCE="/var/www"
DATE=$(date +%Y-%m-%d)
LATEST="$BACKUP_ROOT/latest"
DEST="$BACKUP_ROOT/$DATE"

rsync -av --delete \
  --link-dest="$LATEST" \
  --exclude="*.tmp" \
  --exclude="node_modules" \
  "$SOURCE/" "$DEST/"

rm -f "$LATEST"
ln -s "$DEST" "$LATEST"
echo "Backup complete: $DEST"

--link-dest performs incremental backups using hard links — unchanged files take no extra space.


Performance Tuning

Bash
# Adjust compression level (0–9, default 6)
rsync -av --compress-level=1 /src/ user@server:/dst/

# Checksum-based comparison (instead of timestamps)
rsync -avc /src/ /dst/
#rsync#linux
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗

Questions & answers

Ask a question about this guide. We'll review and reply.