DEV Community

Cover image for Unveiling Linux Command's - rsync
Syam SV
Syam SV

Posted on

Unveiling Linux Command's - rsync

Rsync (Remote Sync) is a command-line utility used for efficiently transferring and synchronizing files and directories between local and remote systems. It uses an algorithm to minimize the amount of data copied by only moving the portions of files that have changed.

Basic Syntax:

The basic syntax of the rsync command is as follows:

rsync [options] source destination
Enter fullscreen mode Exit fullscreen mode
  • source: The source directory or file you want to transfer.
  • destination: The target directory or location where you want to copy the source.

Commonly Used Options:

  • -a or --archive: This option enables archive mode, which preserves file permissions, ownership, timestamps, and more. It's often used for full data synchronization.
  • -v or --verbose: Enables verbose output, showing details about the files being transferred.
  • -r or --recursive: Enables recursive mode, ensuring that all subdirectories and their contents are transferred.
  • --dry-run: Simulates the transfer without actually performing it. Useful for previewing what would be copied.
  • --delete: Deletes files at the destination that are not present in the source. Useful for maintaining exact synchronization.
  • -z or --compress: Compresses files during transfer, which can improve transfer speed over slow connections.

Use Cases and Examples:

Local Copy:

To copy files or directories locally, use the basic syntax:

rsync -av source_directory/ destination_directory/
Enter fullscreen mode Exit fullscreen mode

Remote Copy:

To copy files or directories from a local system to a remote server:

rsync -av source_directory/ user@remote_server:/path/to/destination/
Enter fullscreen mode Exit fullscreen mode

Remote Copy with SSH:

To copy files or directories from a local system to a remote server over SSH:

rsync -av -e "ssh -i /path/to/key.pem" source_directory/ user@remote_server:/path/to/destination/
Enter fullscreen mode Exit fullscreen mode

or

rsync -av -e ssh source_directory/ user@remote_server:/path/to/destination/
Enter fullscreen mode Exit fullscreen mode

Exclude Files or Directories:

To exclude specific files or directories from the transfer:

rsync -av --exclude="file_to_exclude" source_directory/ destination_directory/
Enter fullscreen mode Exit fullscreen mode

Dry Run:

To preview the transfer without actually copying:

rsync -av --dry-run source_directory/ destination_directory/
Enter fullscreen mode Exit fullscreen mode

Delete Unwanted Files:

To remove files at the destination that are not in the source:

rsync -av --delete source_directory/ destination_directory/
Enter fullscreen mode Exit fullscreen mode

Compressed Transfer:

To enable compression during transfer:

rsync -avz source_directory/ user@remote_server:/path/to/destination/
Enter fullscreen mode Exit fullscreen mode

Maintain Metadata:

To ensure metadata consistency during transfers:

rsync -av source_directory/ destination_directory/
Enter fullscreen mode Exit fullscreen mode

Synchronize Remote Directory:

To synchronize a remote directory with a local one:

rsync -av user@remote_server:/path/to/source_directory/ local_destination/
Enter fullscreen mode Exit fullscreen mode

Copying Remote to Local:

To copy files from a remote server to a local machine:

rsync -av user@remote_server:/path/to/source_directory/ local_destination/
Enter fullscreen mode Exit fullscreen mode

Backup option in Rsync

Rsync’s --backup option can be used to store backups of important files. It’s used in along with the --backup-dir option, which specifies the directory where the backup files should be stored

rsync -a --delete --backup --backup-dir=/path/to/backups /path/to/source destination
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Rsync is a versatile tool for efficiently transferring and synchronizing files between local and remote systems. It provides various options that allow you to control the behavior of the transfer, maintain metadata consistency, exclude files, and even simulate transfers before executing them. Its ability to work over SSH ensures secure data transfers across networks, making it an essential tool for managing and backing up data in a Linux environment.

Top comments (0)