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:
rsync -version
In this first example we create the source
and 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
touch original/file{1..5}
source
directory with destination
by using rsync:rsync source/*destination
The *
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 rsync
:
-a
or --archive
this option enables us: -rlptgoD , which means:
-r
recursive-l
copy symbolic links as symbolic links.-p
preserve permissions.-t
preserve the modification/creation date of the file.-g
preserve the file group.-o
preserve the owner of the files.-D
preserve device files and preserve special files.-v
or --verbose
to display process information.-e
or --rsh
is used to specify a remote shell, for example, if you want to rsync between servers.--delete
deletes files in the destination directory if they do not exist in the source directory.-z
or --compress
compresses 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 shell
.
rsync -avz -e 'ssh' [email protected]:/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 protected]:/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.