INTRODUCTION
While we work in industrial environments, data often have to be synchronized in order to keep it updated at all times.
In this post, we will learn to use the Rsync tool, with which you can synchronize all your local and remote data!
Latest Posts
RSYNC
In these blogs posts, we show you how to send files between a Raspberry PLC and your computer, Windows or Linux:
Rsync o 'remote sync' is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon.
It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied.
It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.
Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.
SYNOPSIS
The synopsis like cp, scp or ssh, are the same:
IP ADDRESSES
These are the IP addreses of our devices.
On the one hand, our laptop based on Linux has the IP address: 10.10.10.60 for the Ethernet interface.
On the other hand, the Raspberry PLC has the IP address: 10.10.10.20 for the Ethernet interface.
FROM LOCAL TO REMOTE
Based on this, we are going to do the following:
1- We will create a local folder with 10 files on our laptop called testDir.
mkdir testDir
touch testDir/file{0..9}
ls testDir/
With the ls command, we will list the files in the folder and check that they have been created correctly.
2- We will create a folder in the Raspberry PLC called remDir, to be able to send files from local to remote. With the ls command we will list the files and check that it is empty:
mkdir remDir
ls remDir
3- Now, in order to sync the data from local to remote directory, we will execute the following command:
rsync -a testDir/ pi@10.10.10.20:/home/pi/remDir
the -a option is the archive mode. Equals -rlptoD:
-r --recursive: recurse into directories
-l --links: Copy symlinks as symlinks
-p --perms: Preserve permissions
-t --times: Preserve modification times
-o --owner: Preserve owner (super-user only)
-D: Preserve device files (super-user only) and special files.
Now, go to the Raspberry PLC, where you should be able to see the file in the remote directory.
FROM REMOTE TO LOCAL
Now, we are going to do the opposite. From our local laptop, we are going to get the data from the Raspberry PLC.
We will use the remDir/ that we created in the Raspberry PLC, to send files to a new and empty directory of the laptop.
1- Let's create a new directory in our computer, called newDir:
mkdir newDir
ls newDir
2- Now, we are going to rsync from the remote host to the local host.
rsync -a pi@10.10.10.20:/home/pi/remDir/ newDir/
In this step, we are going to emphasize the / mark next to the remDir. In this example, we will see the difference.
Once the newDir is created, we rsync from remote to local without the / at the end of the remDir. If we miss the / mark, we will be sending the entire directory, not the content of it.
Otherwise, if we add the / mark at the end of the remDir, we will be sending the content of the directory.
TEST
Now, type the following command:
man rsync
And discover all the options available in rsync command for you to optimize your file copies!