Rsync (Remote Sync): Practical Examples of Rsync

Rsync is a Linux-based tool that can be used to sync files between remote and local servers. Rsync has many options that can help you define the connections you make. From deciding the type of shell that should be used to files that should be excluded in a transfer, Rsync gives you the power to shape the transfer specifications.

  • The advantage of incremental backup may be used, supports the socket, several (sliding supports, are client-reference)
  • Remote fellow shell can also encrypt (ssh) transmission, socket, you require encrypted transmission, or the service may be utilized ××× ipsec service.
  • Rsync uses compression and decompression method while sending and receiving data both ends leading to overall less bandwidth consumption.
  • Rsync has the support for copying devices, groups, links, owners and permissions.

In the following tutorial, we will run by some of the most common examples of using Rsync.

Install Rsync On Linux

Below are some of the most common Linux systems and how to install Rsync.

Install Rsync Alma Linux, Fedora, Rhel, Rocky Linux based distributions

sudo yum install rsync -y

Install Rsync Debian, Linux Mint, Ubuntu based distributions

sudo apt install rsync -y

Advertisement


Rsync Command Syntax

First, it is helpful to know the basics of Rsync syntax, which is straightforward and operates to a similar CP, SCP, and SSH operation style. Some examples are as follows:

FunctionSyntax Example
Local Sync:rsync {options} {Source} {Destination}
Remote Sync Pull:rsync {options}  <User_Name>@<Remote-Host>:<Source-File-Dir>  <Destination>
Remote Sync Push:rsync  <Options>  <Source-Files-Dir>   <User_Name>@<Remote-Host>:<Destination>
  • DEST – Destination directory.
  • HOST – Remote hostname or IP Address.
  • OPTION – The rsync options .
  • SRC – Source directory.
  • USER – Remote username.

Rysnc Commands

Rsync comes with a significant number of options to control how to apply and use the sync software. Some of the most common commands are below:

CommandPurpose
-a, –archivearchive files and directory while synchronizing ( -a equal to following options -rlptgoD)
-b, –backup take the backup during synchronization
-l, –linkscopy symlinks as symlinks during the sync
-d, –deletedeletes extraneous files from the destination location.
-e, –rsh=COMMANDmention the remote shell to use in rsync
-h, –human-readable display the output numbers in a human-readable format
-u, –update   don’t copy the files from source to destination if destination files are newer
-r, –recursive     sync files and directories recursively
-n, –dry-run   perform a trial run without synchronization
–p, –progressshow the sync progress during transfer
-z, –compress   compress file data during the transfer
-q, –quiet   suppress message output
-v, –verbose  verbose output

Advertisement


Copy/Sync Files and Directory Locally

The following command will sync a single file on a local machine from one location to another location. The example will take the following file (backup.zip) and copy or sync it to the (/tmp/backups/) folder.

rsync -zvh backup.zip /tmp/backups/

Note, even if the path does not exist, the Rsync command can create a directory.

Copy/Sync a Directory on Local Computer

To transfer or sync all the files from one directory to a different directory in the same machine. The example you will see here is taking the (downloads) directory and creating a backup again in sync location (/tmp/backups/).

rsync -avzh /home/joshua/Downloads /tmp/backups/
Rsync (Remote Sync): Practical Examples of Rsync

Advertisement


Copy a Directory from Local Server to a Remote Server

Rsync can be used to sync directories to and from local servers to remote servers. In the following example, we will use the folder (backup) on your local server to be sent to remote machines in your internal or external network.

rsync -avz backup/ root@192.168.50.107:/home/

Copy/Sync a Remote Directory to a Local Machine

The next Rsync remote directory example will sync the opposite way around this time. The following example code snippet will use Rsync to sync a remote directory to your local server. Example directory (/home/josh/backup/packages) to be copied to your local server (/tmp/packages).

rsync -avzh root@192.168.50.107:/home/josh/backup/packages /tmp/packages

Advertisement


Copy a File from a Remote Server to a Local Server with SSH

One of the most popular ways to transfer data between local and remote servers is to use SSH (Secure Shell) as the data is protected in a secured connection and encrypted so that nobody can read the data in transit which, given the privacy concerns these days, is a critical factor.

To specify a protocol with rsync with SSH, you need to specify the (-e) protocol option as per the start of the guide with basic commands and syntax.

rsync -avzhe ssh root@192.168.50.107:/home/josh/packages /tmp/

Copy a File from a Local Server to a Remote Server with SSH

The following example will show the opposite with rsync syncing a file, this time from your local server to a remote internal or external server using SSH and Rsync.

rsync -avzhe ssh images.zip root@192.168.50.107:/uploads/

Advertisement


Exclude Files and Directories

These two options allow you to exclude files by specifying parameters. The first option is to use the (–exclude) argument and specify the files and directories you want to exclude on the command line.

Firstly, when excluding files or directories, you need to make sure that you use the relative paths to the source destination, or else it will fail or have incorrect outcomes in excluding the wrong files. In the following first example, you will see an option to exclude (cache) and (tmp) directories:

rsync -a --exclude=cache --exclude=tmp /src_directory/ /dst_directory/

In the second option, you will use the (–exclude-from) option and specify the files and directories you want to exclude which is stored in a file:

rsync -a --exclude-from='/exclude-file.txt' /src_directory/ /dst_directory/
Rsync (Remote Sync): Practical Examples of Rsync

Set the Max Size of Files to be Transferred

The following example will limit the size of files to be synced. This is useful for constantly running rsync between local and remote machines where files possibly could bloat or bandwidth factors come into play.

rsync -avzhe ssh --max-size='100k' /var/log root@192.168.0.100:/home/josh/logs

Note, you must specify the size. For example, KB uses K, MB uses M, and GB uses G.


Advertisement


Show Progress While Transferring Data with rsync

Another popular feature when syncing large directories is to see the progress. This can be done in the following example using the (-progress) option, which will show the files and the time remaining on the transfer to complete.

rsync -avzhe ssh --progress images.zip root@192.168.50.107:/uploads/

Do not Sync/Copy Modified files from Destination

Sometimes you may require the source to not sync and copy any changes from the destination. An example of this would be backing up all files to a backup server, and you want only the source to sync and ignore any changes at the destination since they will not be valid on your source server to avoid corruption.

This can be done by using the -u flag in the rsync command.

rsync -avzu root@192.168.50.107:/uploads/ /backup/website1

Advertisement


Comments and Conclusion

Rsync is probably one of the most efficient, accessible, and secure ways to sync files between operating systems and servers. One drawback is that rsync can accidentally overwrite your files, so it is vital to double-check everything before you sync or using the (–dry-run) option.

Rsync is fantastic for moving complex file syncs and for transferring a large number of files. To many users, it’s better to use Rsync for moving large batches of files than SCP. When used with cron, Rsync is also able to take automatic backups. While it may look and sound difficult, Rsync can be beneficial and accomplish things that less intimidating interfaces may not be able to.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x