JustGeek.dev Tech, simplified.

Rsync command examples

Rsync (Remote Sync) is a powerful utility for efficiently synchronizing files and directories between different locations, whether on the same machine or across remote systems. This guide covers essential rsync commands and best practices.

What is Rsync?

Rsync is a versatile file synchronization and transfer tool that offers several advantages:

  • Performs incremental transfers by copying only changed portions of files
  • Supports compression during transfer to reduce bandwidth usage
  • Preserves file permissions, ownership, timestamps, and other attributes
  • Provides built-in error checking and verification
  • Works over SSH for secure remote file transfers
  • Supports both local and remote synchronization

Basic Syntax

The basic syntax for rsync follows this pattern:

rsync [options] source destination

For remote transfers:

rsync [options] source [user@]host:destination
rsync [options] [user@]host:source destination

Essential Options

Common rsync options and their meanings:

  • -a (archive): Preserves permissions, ownership, timestamps, and recursively copies directories
  • -v (verbose): Shows detailed output of the transfer process
  • -z (compress): Compresses data during transfer
  • -h (human-readable): Displays file sizes in human-readable format
  • -P (progress): Shows progress during transfer and allows transfer resumption
  • --delete: Removes files in destination that don’t exist in source
  • -n or --dry-run: Simulates the transfer without making changes

Common Usage Examples

1. Basic Local Directory Sync

rsync -avh /source/directory/ /destination/directory/

2. Remote Directory Sync with Progress

rsync -avzh --progress /local/directory/ user@remote:/remote/directory/

3. Sync from Remote to Local

rsync -avzh user@remote:/remote/directory/ /local/directory/

4. Excluding Files and Directories

Create an exclude file (exclude.txt):

*.tmp
temp/
.git/
node_modules/
*.log

Use the exclude file:

rsync -avzh --exclude-from='exclude.txt' source/ destination/

5. Mirror Sync with Deletion

rsync -avzh --delete source/ destination/

6. Bandwidth-Limited Transfer

rsync -avzh --bwlimit=1000 source/ destination/  # Limits to 1000 KB/s

Advanced Features

1. Backup with Timestamp

rsync -avb --backup-dir=/backup/$(date +%Y-%m-%d) source/ destination/

2. Include/Exclude Patterns

rsync -avz --include='*.txt' --exclude='*' source/ destination/

3. Resume Interrupted Transfer

rsync -avzh --partial --progress source/ destination/

Best Practices

  1. Always Test First: Use --dry-run before performing critical synchronizations
    rsync -avzhn source/ destination/
    
  2. Use Trailing Slashes Correctly:
    • With slash /source/: Copies contents of the directory
    • Without slash /source: Copies the directory itself
  3. Secure Remote Transfers: Use SSH for secure remote transfers
    rsync -avz -e "ssh -p 2222" source/ user@remote:/destination/
    
  4. Handle Special Files:
    • -D: Preserve device files
    • -l: Copy symlinks as symlinks
    • -H: Preserve hard links

Troubleshooting

Common issues and solutions:

  1. Permission Denied:
    • Use sudo when necessary
    • Check file permissions
    • Verify SSH key permissions
  2. Network Issues:
    • Use -P to enable resume capability
    • Add --timeout=X to handle unstable connections
  3. Space Issues:
    • Use --max-size to limit file sizes
    • Check destination disk space before transfer

Safety Tips

  1. Always verify source and destination paths
  2. Use --dry-run before critical operations
  3. Create backups before large synchronizations
  4. Be careful with --delete option
  5. Test restore procedures regularly

Monitoring and Logging

Add logging to your rsync commands:

rsync -avzh --log-file=rsync.log source/ destination/

Performance Optimization

  1. Use -z only for text files over slow connections
  2. Consider using -W for local transfers
  3. Adjust --checksum usage based on needs
  4. Use --partial-dir for large file transfers

Remember to always verify your backups and test restore procedures regularly to ensure data integrity and availability.