In this guide we will explain how to use rsync to improve productivity on Linux based systems.
Rsync is a utility that allows you to efficiently transfer and synchronize files or directories between a local machine or remote server.
The basic syntax for rsync in local environments is as follows:
rsync [optional modifiers] [source] [destination]
The basic syntax for rsync between a local environment to a remote environment is as follows:
rsync [optional modifiers] [source] [user]@[host]:[destination]
And vice versa:
rsync [optional modifiers] [user]@[host]:[source] [destination] ````.
Many Linux distributions include the rsync package preinstalled. In case it is not preinstalled you can do it manually with the following command:
apt-get install rsync
yum install rsync
bew install rsync
It's that easy! To verify that it has been installed you can get the version with the following command:
In this first example we create the
destination directories. Next, we will create 5 files in the source directory and we will see the magic of rsync.
cd ~ mkdir source mkdir destination
destinationby using rsync:
* indicates to synchronize the entire contents of the directory
If files are added or modified in the
source directory and the
rsync command is run again, only the new and modified files will be synchronized, thus avoiding copying unnecessary files.
The following are some of the optional modifiers that can be added to the use of
--archivethis option enables us: -rlptgoD , which means:
-lcopy symbolic links as symbolic links.
-tpreserve the modification/creation date of the file.
-gpreserve the file group.
-opreserve the owner of the files.
-Dpreserve device files and preserve special files.
--verboseto display process information.
--rshis used to specify a remote shell, for example, if you want to rsync between servers.
--deletedeletes files in the destination directory if they do not exist in the source directory.
--compresscompresses the data during the transfer.
In this first example we will synchronize the contents of the /var/log folder on the
10.0.0.11 server to the /tmp folder on our server using the SSH
rsync -avz -e 'ssh' firstname.lastname@example.org:/var/log /var/log /tmp/log
Next we will carry out the same operation, but in this case with the
--delete tag, so that the files in destiny that do not exist in origin are erased:
rsync -avz --delete -e 'ssh' email@example.com:/var/log /var/log /tmp/log
In conclusion with rsync you can transmit file transfers over network connections or local directories. Its flexibility makes it a very good choice for many file-level operations.
A good command of rsync allows you to design complex backup operations and gain control over how and what is transferred.