liminfo

rsync Reference

Free reference guide: rsync Reference

31 results

About rsync Reference

The rsync Reference is a comprehensive, searchable cheat sheet for the rsync file synchronization utility. It covers 30 entries across six categories: Basic Options, Filters, Remote Sync, SSH, Backup, and Exclusions. Each entry documents the exact flag or pattern syntax, explains what it does, and provides a practical command-line example — making it easy to look up the right options whether you are syncing a local directory, deploying to a server, or building an automated backup system.

This reference is designed for system administrators, DevOps engineers, developers, and power users who need to transfer files efficiently over networks. The Basic Options section covers the essential flags: -a (archive mode, preserving permissions and timestamps), -v (verbose), -z (compression), -P (progress plus partial resume), --delete (mirror synchronization), -n (dry run simulation), and --stats (transfer summary). The Filters section covers --include and --exclude patterns for file type filtering, --exclude-from for reading patterns from a file, the --filter rule syntax, and --max-size/--min-size for size-based filtering.

The Remote Sync section explains rsync local-to-remote, remote-to-local, and daemon mode (rsync:// protocol). The SSH section covers the -e ssh transport option, custom SSH ports with -e "ssh -p PORT", SSH key authentication with -e "ssh -i KEY", bandwidth limiting with --bwlimit, and checksum-based comparison with --checksum. The Backup section documents --backup with suffix, --backup-dir for date-based versioning, --link-dest for hard-link incremental backups, cron scheduling patterns, and full mirror backup. The Exclusions section covers practical patterns for excluding .git, node_modules, hidden files, and using .rsync-filter files for per-directory rules.

Key Features

  • Core rsync flags: -a (archive), -v (verbose), -z (compress), -P (progress+partial), --delete (mirror), -n (dry-run), --stats
  • Filter patterns: --include/--exclude with wildcards, --exclude-from file, --filter rules, --max-size and --min-size
  • Remote synchronization: local-to-remote, remote-to-local, rsync daemon mode (rsync:// and ::module syntax)
  • SSH transport: -e ssh, custom port with -e "ssh -p PORT", SSH key file with -e "ssh -i KEY", --bwlimit bandwidth control
  • Backup strategies: --backup with --suffix, --backup-dir for dated backups, --link-dest for hard-link incremental backups
  • Scheduled backups: cron job template for daily rsync with logging to /var/log/rsync-backup.log
  • Practical exclusion patterns: .git/, node_modules/, hidden dot files, --cvs-exclude flag, .rsync-filter per-directory rules
  • Checksum-based sync with --checksum for accurate comparison, and mirror backup with --delete-excluded

Frequently Asked Questions

What does rsync -av do and when should I use it?

The -a flag enables archive mode, which is equivalent to -rlptgoD: it recursively copies directories (-r), preserves symlinks (-l), preserves permissions (-p), preserves modification times (-t), preserves group ownership (-g), preserves owner (-o), and preserves device/special files (-D). The -v flag enables verbose output so you can see each file being transferred. Use -av as your default starting point for most synchronization tasks.

What is the difference between rsync with and without a trailing slash?

The trailing slash on the source path makes a significant difference. With a trailing slash (rsync -av /src/ /dest/), rsync copies the contents of /src/ directly into /dest/, so the files end up in /dest/file.txt. Without a trailing slash (rsync -av /src /dest/), rsync copies the directory itself, creating /dest/src/file.txt. The destination path trailing slash does not matter. This is one of the most common sources of rsync mistakes.

How do I do a dry run to preview what rsync will change?

Use the -n or --dry-run flag: rsync -avn --delete /src/ /dest/. Combined with -v, this shows exactly which files would be created, updated, or deleted without actually making any changes. Always run a dry run before using --delete or other destructive operations to verify the file list is what you expect. The output format is the same as a real run, making it easy to review.

How do I implement incremental backups with rsync?

Use the --link-dest option to create hard-link-based incremental backups. The pattern is: LATEST=$(ls -td /backup/*/ | head -1) && rsync -av --link-dest="$LATEST" /src/ /backup/$(date +%Y%m%d)/. Files that have not changed since the last backup are hard-linked from the previous backup directory (using no extra disk space), while changed files are copied fresh. Each dated backup directory appears to contain a complete copy but uses minimal additional space.

How do I sync files over SSH with a non-standard port?

Use the -e flag to specify SSH transport options: rsync -avz -e "ssh -p 2222" /local/ user@remote:/data/. The -e option replaces the default remote shell command, so you can pass any ssh flags including -p for port, -i for key file, -o for SSH config options, and -C for SSH-level compression. If you use a non-standard port frequently, add it to your ~/.ssh/config with Host and Port directives instead.

How do I exclude node_modules and .git from rsync transfers?

Use --exclude flags: rsync -av --exclude='.git/' --exclude='node_modules/' /project/ user@server:/var/www/project/. You can chain multiple --exclude flags. For larger projects, put all exclusion patterns in a text file (one per line) and use --exclude-from=exclude-list.txt. Alternatively, use --cvs-exclude (or -C) which automatically excludes common VCS and build artifact directories including .git, .svn, *.o, and *.swp.

What is the --delete flag and when is it safe to use?

The --delete flag causes rsync to delete files in the destination that no longer exist in the source, making the destination an exact mirror of the source. This is essential for true mirroring but dangerous if used carelessly — any file that exists only in the destination will be permanently deleted. Always run with --dry-run first to verify the file list, and consider using --backup or --backup-dir to save deleted files before using --delete in production.

How does rsync --checksum differ from the default file comparison?

By default, rsync decides whether to transfer a file by comparing its size and modification time. This is fast but can miss cases where a file was modified and restored to the same size and timestamp. The --checksum flag forces rsync to compute an MD5 or similar checksum of each file and compare checksums instead of size/time. This is slower (requires reading every byte of every file) but guarantees bit-for-bit accuracy. Use --checksum when data integrity is critical, such as after media copy or when timestamps cannot be trusted.