rsync Basics: Secure Backups and File Sync on Linux (With Real Examples)


Every backup guide eventually says the same thing: use rsync. It is the standard Linux tool for copying files between folders, machines and servers — and it is smarter than a plain copy. It only transfers what changed, it resumes interrupted transfers, and over SSH it encrypts everything in transit. If you run a server, learning rsync is non-negotiable.

The Basic Syntax

rsync -av /home/anony/documents/ /backup/documents/

The -a means archive mode: preserve permissions, timestamps, ownership and symlinks. The -v means verbose — show what is happening. The trailing slash on the source matters: /documents/ copies the contents of the folder, while /documents (no slash) copies the folder itself. Getting this wrong is the classic rsync mistake, so double-check it every time.

Sync Over SSH (The Secure Way)

rsync -avz -e ssh /home/anony/website/ anony@203.0.113.10:/var/www/html/

The -z compresses data in transit, and -e ssh sends everything through an encrypted SSH connection. Your files never travel in plaintext. Use an SSH key instead of a password so the transfer can run unattended in scripts and cron jobs. Only sync to servers you own or have written permission to access.

Dry Run First — Always

The -n (or --dry-run) flag shows what rsync would do without changing anything:

rsync -avn /home/anony/documents/ /backup/documents/

This is your safety net, especially before using --delete, which removes files from the destination that no longer exist in the source. A wrong --delete can wipe a backup in seconds. Dry-run first, read the output, then run it for real.

Backups With --delete (Carefully)

For a true mirror backup, add --delete:

rsync -av --delete /home/anony/documents/ /backup/documents/

Now the backup exactly matches the source, including deletions. Keep at least one backup copy that does not use --delete, so a file you accidentally deleted locally is not instantly deleted from every backup too.

Exclude What You Don't Need

rsync -av --exclude='*.tmp' --exclude='.cache/' /home/anony/ /backup/anony/

Excludes keep caches, temp files and giant downloads out of your backups. You can also put patterns in a file and use --exclude-from=excludes.txt.

Automate It With Cron

A daily backup is one cron line:

0 2 * * * rsync -a --delete /home/anony/documents/ /backup/documents/

This runs every night at 2 AM. Add >> /var/log/rsync-backup.log 2>&1 to keep a log of each run so you can check it worked.

The 3-2-1 Rule

One backup is not a backup strategy. Follow 3-2-1: three copies of important data, on two different media, with one copy offsite. rsync over SSH to a remote server you own gives you the offsite copy. Test your restores regularly — a backup you have never restored is just a hope.

Learn rsync once and you will use it for the rest of your Linux life. Backups, migrations, deployments — it does all of them, securely.

Comments

Popular Posts