Rsync for Linux: sync, copy, and back up files

9 min read - May 24, 2025

hero section cover
Table of contents
  • Rsync for Linux: how to sync, copy, and back up files like a pro
  • What is rsync?
  • Why use rsync over cp or scp
  • Common rsync flags
  • Rsync examples
  • Automating rsync with cron
  • Final thoughts
Share

How to use rsync on Linux for incremental transfers, remote syncs over SSH, scheduled backups with cron, and avoiding the trailing-slash trap.

Rsync for Linux: how to sync, copy, and back up files like a pro

Rsync is the standard tool for copying files between Linux systems. It's faster than cp or scp for anything you run more than once, recovers from network drops, and only transfers what has actually changed. This post covers what rsync does, the flags worth knowing, the examples you'll reach for most often, and how to schedule it with cron.


 

What is rsync?

Rsync is a command-line utility for copying and synchronising files between locations. It works locally, over SSH to a remote host, or against an rsync daemon. The key difference from cp or scp: rsync compares source and destination first, then transfers only the differences. On a 100 GB directory where 200 MB has changed, rsync moves 200 MB. cp and scp move the lot.

It ships with every modern Linux distribution. If it's missing: apt install rsync or dnf install rsync.

Why use rsync over cp or scp

Three reasons rsync wins for any non-trivial copy:

  • It's incremental. Subsequent runs transfer only the changed bytes, not whole files. For backups and mirrors, that turns an overnight job into a few minutes.
  • It survives interruptions. With --partial, a half-transferred 50 GB file resumes from where it stopped rather than starting over.
  • It preserves metadata. Archive mode (-a) keeps permissions, ownership, symlinks, and timestamps. Critical for system files, source trees, and anything where attributes matter.

For one-off copies of small files, scp is fine. For anything that gets repeated, anything bandwidth-heavy, or anything you might need to resume, use rsync.

Common rsync flags

The flags you'll actually reach for:

FlagWhat it does
-aArchive mode. Preserves permissions, ownership, symlinks, timestamps. Equivalent to -rlptgoD.
-vVerbose. Lists files being transferred.
-zCompress in transit. Useful on slow or expensive links, less useful on LAN or for already-compressed content.
-PCombines --partial and --progress. Resume interrupted transfers, show live progress.
--deleteDelete files on the destination that don't exist on the source. Required for a true mirror.
--excludeSkip files matching a pattern.
--exclude-from=FILERead exclude patterns from a file. Cleaner than chaining many --exclude flags.
--dry-runShow what would happen without transferring anything.
--bwlimit=KBPSCap bandwidth usage in kilobytes per second. Useful for backups that run during business hours.
--checksumCompare files by checksum instead of size and modification time. Slower but catches edge cases.
-e sshUse SSH for the transfer. Implicit on most modern installs.

Default behaviour compares file size and modification time, which is fast and almost always correct. Switch to --checksum if a deployment process rewrites timestamps without changing content, or if you suspect bit-level corruption.

Rsync examples

Copy a directory locally:

rsync -av /source/path/ /destination/path/

Sync to a remote server over SSH:

rsync -avz /local/dir/ user@remote:/remote/dir/

Dry run before doing anything destructive:

rsync -avz --dry-run /local/dir/ user@remote:/dir/

Mirror two directories, including deletions on the destination:

rsync -av --delete /source/ /destination/

Exclude logs and a temp directory:

rsync -av --exclude='*.log' --exclude='tmp/' /src/ /dest/

Resume a large transfer that might get interrupted:

rsync -avP user@remote:/backups/big.img /local/

If the SSH session drops, run the same command again. With -P, rsync picks up where it stopped instead of starting from zero.

Cap bandwidth at 50 MB/s for an off-peak backup so it doesn't saturate the uplink:

rsync -avz --bwlimit=50000 /local/dir/ user@remote:/dir/

The trailing slash gotcha

The single most common rsync mistake:

rsync -av /source  /destination/    # copies the 'source' directory INTO /destination
rsync -av /source/ /destination/    # copies the CONTENTS of /source into /destination

A trailing slash on the source means "everything inside this directory". No trailing slash means "this directory itself". Run with --dry-run first if you're unsure, especially when combined with --delete.

Automating rsync with cron

For scheduled backups, you need three things:

  1. Passwordless SSH access to the destination. Cron jobs can't type passwords. Generate a key and copy it to the backup host:

    ssh-keygen -t ed25519
    ssh-copy-id user@backup-host
  2. A script, not a one-liner in crontab. Easier to test, easier to log:

    #!/bin/bash
    LOG=/var/log/rsync-backup.log
    rsync -az --delete /etc/ user@backup:/backups/$(hostname)/etc/ >> "$LOG" 2>&1

    Make it executable with chmod +x.

  3. A cron entry to run it. Edit with crontab -e:

    0 2 * * * /usr/local/bin/rsync-backup.sh

    That runs the script every night at 02:00.

Two refinements worth making in production: wrap the script in flock so a long-running job doesn't overlap with the next night's run, and pipe output through logger so failures land in the system journal where monitoring can pick them up.

Final thoughts

Rsync is one of the few Linux tools where the time spent learning the flags pays back almost immediately. The main points:

  • Incremental transfers and resume support make rsync the right choice for anything you run more than once
  • -a for archive mode, -z for compression, -P for resume and progress, --delete for true mirrors
  • Watch the trailing slash on the source path, especially with --delete
  • Pair it with cron and SSH keys for hands-off scheduled backups

For large backup jobs that move data between facilities, running rsync on a VPS with unmetered bandwidth means backup windows aren't constrained by transfer caps.

Blog

Featured this week

More articles
Why it's important to have a powerful and unmetered VPS

Why it's important to have a powerful and unmetered VPS

An unmetered VPS gives flat-rate bandwidth at a fixed port speed. How it differs from metered plans, when it pays off, and what to check before buying.

7 min read - May 9, 2025

#server-performance

Linux Memory Management: Swap, OOM Killer & Cgroups

12 min read - May 31, 2026

More articles
background image

Have questions or need a custom solution?

icon

Flexible options

icon

Global reach

icon

Instant deployment

icon

Flexible options

icon

Global reach

icon

Instant deployment