Linux rsync Command Examples

Filed Under: UNIX/Linux

Rsync, short for remote sync, is a Linux command used for copying and synchronizing files, between 2 Linux systems. It works in such a way that any additional changes made to a file or directory are replicated on another Linux system.

With Linux rsync command, you can seamlessly copy and synchronize backups locally and remotely between Linux systems & servers. In this article, we will examine various command options that you can use alongside rsync command.

Basic syntax for Linux rsync command

The syntax for rsync is briefly summed up below.

$ rsync options source destination

Options used with rsync command

  • -z: This compresses data.
  • -v: This displays the process in verbose mode.
  • -r: The option copies data recursively (However, the option doesn’t preserve timestamps and file permission during data transfer).
  • -h: This outputs information in a human-readable format.
  • -a: This not only allows copying of files recursively but also preserves symbolic links, timestamps, file permissions as well as user and groups ownerships.

Copy files and directories locally

You can sync file locally from one location to another in a Linux machine using the example provided below.

$ rsync -zvh backup.tar /opt/backup_dir

In the example above, the tarball backup.tar is copied and synchronized to the /opt/backup_dir directory.

Sample output

Backup File Locally With Rsync Command

To copy a directory locally from one location to another:

The syntax will be similar to copying files, only that in this case, we use the -a flag for recursive copying.

In the example below, we are copying and synching the deb_packages directory to the /opt/backup_dir directory.

$ rsync -azvh deb_packages /opt/backup_dir

Sample output

Backup File Locally With Rsync Command

Copy/Sync files or directories to and from a remote server

To copy a directory to a remote server, the username and IP address of the server has to be specified alongside the destination folder.

In the example below, the deb_packages directory is copied to a remote server with an IP 66.152.163.19 using root user credentials and saved in the root home directory.

$ rsync -avz deb_packages root@66.152.163.19: 

Sample output

Copy Fand Sync Files To A Remote Server

To copy and sync a directory from a remote server to a local PC:

To copy and sync files from a remote system to a Local Linux system, use the example below

$ rsync -avzh root@66.152.163.19:documents /opt/backup_dir

In the above example, the directory documents is copied recursively from the remote system with an IP 66.152.163.19 to the local system in the /opt/backup_dir directory.

Rsync command over SSH

Rsync can be used alongside SSH protocol to secure or encrypt the files being sent to and from a Linux system. this ensures that no one can eavesdrop the data that you are transferring over the network.

To copy a file from a local server to a remote server using rsync over SSH:

In this example, we will use the -e option with rsync to specify a protocol, in this case, SSH.

$ rsync -avhze ssh backup.tar root@66.152.163.19:/backup_dir

Sample output

Rsync Over Ssh Send Files To A Remote System

To copy a file from a remote server to a local system using rsync over SSH:

Just like in the previous example, the rsync command is used together with the -e option.

$ rsync -avzhe ssh root@66.152.163.19:/backup_dir /tmp/

Sample output

Rsync Over Ssh Send Files From A Remote System To A Local System

Display progress while transferring data with rsync command

If you want to view progress during transfer of data, use the --progress option. This displays the file and estimated time to completion of data transfer.

Sample output

Display Progress Of Data Transfer Using Progree Option

Linux rsync command with –include and –exclude Options

These options above help users to include and exclude files that they may want to be included or excluded in the syncing process.

In the example below, rsync command will include those files in /var/lib directory which starts with ‘p’ and excludes all other files and directory.

$ rsync -avz --include 'p*' --exclude '*' /var/lib root@66.152.163.19:/opt/backup_dir

Sample output

Include And Exclude Options

Set maximum size of files to be transferred

You can define the maximum file size that is to be transferred and synced using the “–max-size” option.

$ rsync -avzh --max-size='50M' deb_packages root@66.152.163.19:/opt/backup-dir

The command above limits transfer of file in deb_packages directory to 50 Megabytes. This implies that only files equal to or less than 50 MB will be transferred and synched.

Sample output

Limit Size Of File Transfer

Delete source Files after successful Transfer

If you are making a backup copy of your files toa remote target but you wish not to keep a local copy, use the –remove-source-files option as shown.

$ rsync --remove-source-files -zvh backup.tar /tmp/mybackups/

Sample output
Rsync And Remove Files Locally

We hope this article has been beneficial to you. If you have any query, do get back to us with your feedback.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages