rsync command and examples in Linux

rsync means remote sync. It is used for copying and synchronizing files and directories remotely as well as locally in Linux. You can copy and synchronize your data remotely and locally across directories, perform data backups and mirroring. Below are some features of rsync:

  1. It efficiently copies and sync files to or from a remote system.
  2. It is faster than scp because it uses remote-update protocol which allows to transfer just the differences between two sets of files. First time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.
  3. It consumes less bandwidth as it uses compression and decompression method while sending and receiving data.
Syntax: # rsync options source destination

Commonly used options used with rsync command:

  1. -v: verbose
  2. -r: copies data recursively
  3. -a: archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownership and timestamps
  4. -z: compress file data
  5. -h: human-readable, output numbers in a human-readable format

Examples:

1. Copy/Sync files and directory locally:

# rsync -zvh source.tar /tmp/destination/

The above command will sync/copy the file source.tar to /tmp/destination/ directory.

Note: If destination does not exist, it’ll create that as well.

# rsync -avzh /root/directory1 /tmp/destination/

The above command will copy/sync all the files from /root/directory1 to /tmp/destination/ directory.

2. Copy/Sync files and directories to/from a remote server:

# rsync -avz /root/directory1 [email protected]:/home/

The above command will copy/sync all the files in /root/directory1 to the remote server in /home/ directory.

# rsync -avzh [email protected]:/home/technicalmint /tmp/destination

The above command will copy/sync all the files from remote server (from /home/technicalmint/ directory) to the local server in /tmp/destination directory.

3. rsync over ssh:

We can use SSH for data transfer with rsync. Using SSH protocol while transferring data your data is being transferred in a secured connection with encryption so that nobody can read your data while it is being transferred over on the internet.

To specify a protocol with rsync you need to give “-e” option with protocol name you want to use.

Examples:

# rsync -avzhe ssh [email protected]:/home/technicalmint/ /tmp/destination/

# rsync -avzhe ssh source.tar [email protected]:/home/

4. Use of –include and –exclude options:

These options allows us to include and exclude files by specifying parameters. You can specify those files or directories which you want to include in your sync and exclude files and folders with you don’t want to be synced.

# rsync -avze --include 'd*' --exclude '*' [email protected]:/home/technicalmint/ /tmp/destination

The above command will copy/sync all the files/directories which starts with “d” and exclude everything else.

5. Automatically delete source files after successful transfer:

# rsync --remove-source-files -zvh source.tar /tmp/destination/

The above command will remove the source file source.tar after it’s successfully copied/synced to the destination directory.

6. Dry run with rsync:

Use of this option will not make any changes and will only show the dry run and the output of the command and if the output shows exactly what you need, you can remove the –dry-run option and run the command again. This is to avoid doing the undo job if you mess up after running the rsync command.

# rsync --dry-run --remove-source-files -zvh backup.tar /tmp/destination/

That’s it in this article, hope you enjoyed it. Please share it across if you think it’s good.

Leave a Reply

Your email address will not be published. Required fields are marked *