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
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
$ rsync -azvh deb_packages /opt/backup_dir
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
126.96.36.199 using root user credentials and saved in the root home directory.
$ rsync -avz deb_packages firstname.lastname@example.org:
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 email@example.com:documents /opt/backup_dir
In the above example, the directory
documents is copied recursively from the remote system with an IP
188.8.131.52 to the local system in the
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 firstname.lastname@example.org:/backup_dir
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
$ rsync -avzhe ssh email@example.com:/backup_dir /tmp/
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.
Linux rsync command with –include and –exclude Options
These options above help users to
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 firstname.lastname@example.org:/opt/backup_dir
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 email@example.com:/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.
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/
We hope this article has been beneficial to you. If you have any query, do get back to us with your feedback.