Home > Archive/Compression > rsync

Mastering the rsync Command

This guide covers the `rsync` command for efficiently synchronizing and backing up files and directories on Linux. It allows for safe and fast incremental copying and synchronization between local and remote systems.

Main Options

Combine various options of the `rsync` command to set up file synchronization and backup tasks.

1. Basic Synchronization

2. Transfer Modes and Options

Generated command:

Try combining the commands.

Description:

`rsync` Executes the command.

Combine the above options to virtually execute commands with AI.

How rsync Works

`rsync` uses the 'delta encoding' algorithm to transmit only the changed portions of files, efficiently utilizing network bandwidth. This makes it extremely fast and efficient for synchronizing large files or many files.

Key Features of rsync

These are the core features that differentiate `rsync` from other copying tools.

  • Delta Transfer: Identifies and transmits only the differences between source and destination files. Instead of copying the entire file again, it transmits only the changed blocks, maximizing efficiency.
  • Incremental Backup: Copies only the files that have changed since the last backup, saving time and space by eliminating the need for full backups every time.
  • Remote Synchronization: Files can be transmitted over encrypted channels through various remote shells, including SSH, enhancing security.
  • Preservation of File Attributes: Using archive mode (`-a`), all file attributes such as permissions, owners, groups, timestamps, and symbolic links are preserved.

The Importance of Trailing Slash in Paths

Whether or not to append a slash (`/`) at the end of the source path in `rsync` is very important. - `source/`: Copies the **contents inside** the `source` directory. (Similar to `cp source/* dest/`) - `source`: Copies the **directory itself**. (Similar to `cp -r source dest/`) Failing to understand this difference can lead to unexpected results.

Usage Examples

Establish file synchronization and backup strategies through practical usage examples of the `rsync` command.

Synchronize Local Directory (Archive Mode)

rsync -av /path/to/source_dir/ /path/to/destination_dir/

Synchronizes the contents of `source_dir` to `destination_dir`, preserving all attributes like permissions and timestamps. Files not in `source_dir` will not be deleted from `destination_dir`.

Mirror Directory to Remote Server (Backup)

rsync -avz --delete --progress /local/my_data/ user@remote_host:/remote/backup/

Mirrors the contents of the local `my_data` directory to the remote server’s `/backup/` directory. Files not in the source will also be deleted from the destination. Uses compression and shows progress.

Fetch Specific Files from Remote Server to Local

rsync -avz --include='*.log' --exclude='*' user@remote_host:/var/log/ /tmp/logs/

Fetches only `.log` files from the `/var/log/` directory on the remote server to the local `/tmp/logs/` directory.

Preview Changes Without Actual Execution

rsync -avz --delete --dry-run /path/to/source/ /path/to/destination/

Shows what changes will occur without actually transferring or deleting files. It is advisable to always use this before important synchronizations.

Synchronize Using a Different SSH Port

rsync -avz -e "ssh -p 2222" /local/src/ user@remote_host:/remote/dest/

Synchronizes files with the remote server using port 2222 instead of the default SSH port (22).


Same category commands