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.
Rsync comes with a great number of options to control how to apply and use the sync software. Some of the most common commands are below:
archive files and directory while synchronizing ( -a equal to following options -rlptgoD)
take the backup during synchronization
copy symlinks as symlinks during the sync
deletes extraneous files from the destination location.
mention the remote shell to use in rsync
display the output numbers in a human-readable format
don’t copy the files from source to destination if destination files are newer
sync files and directories recursively
perform a trial run without synchronization
show the sync progress during transfer
compress file data during the transfer
suppress message output
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 (/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/) directory.
rsync -avzh /home/joshua/Downloads /tmp/backups/
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. We will use the folder (backup) on your local server to be sent to remote machines in your internal or external network in the following example.
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).
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.
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/
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.
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.
Overall, Rsync is probably one of the most efficient, free 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.