A. Introduction
I was assigned to a rather strange task (at least for myself), which is required to synchronize image data from 1 server to 1 (or 2, 3 other servers). At first, I did not understand the spec reader, in the directory of the server, in addition to the folder containing the image, there was nothing else (sad) so it was even more confusing because I thought that saving the image only happens when the same way. directory, or through a function of another server … bla bla but not another server. Later I stumbled across this rsync sister so this article I will introduce Rsync in Linux
B. Rsync
1. What is Rsync
- The rsync Linux command copies and syncs files or directories between a personal computer, a remote server, or any other computer. Anyone working with Linux needs to use this powerful function to support better management. With this article, you will quickly master the Rsync command
- Manually syncing folders or copying files is very time consuming. The rsync feature can do almost all of these things, saving you a lot of time. Even when you are disconnected during the conversion, the tool will pause, and will be reopened at that pause point when reconnecting.
2. Syntax barn engine
Rsync’s basic syntax looks like this:
1 2 | rsync [optional modifiers] [SRC] [DEST] |
There are different ways to use the Linux Rsync command. For example, optional modifiers
indicate which action will be performed, src
is the root directory, and dest
is the machine’s root directory.
3. Install Rsync
If your device does not have rsync, you can install it in 1 minute! Debian-based operating systems such as Ubuntu can use the following command to install Rsync:
1 2 | apt-get install rsync |
On a rpm distribution such as Fedora or CentOS, use the following command to install:
1 2 | yum install rsync |
Done! Rsync Linux is ready to sync, convert or delete files! See the installation results using the rsync version test command as before:
1 2 | rsync -version |
4. Operation on Rsync
- In this tutorial, I created 2 folders on Linux machine, named
Original
andDuplicate
. The original folder has 3 photos and the duplicate folder is empty. Now we will show you how to use Rsync Linux. - The following command, when typed into commanldn, will copy and sync the files located in the root directory to the destination directory.
1 2 | rsync original/* duplicate/ |
- Only rsync command know to sync the entire file in the root directory.
- If we put a new image or file into the root directory and run the same command again, it will only copy the new file to the destination directory.
- This feature is useful for copying files between networks without high bandwidth.
- Note that this command only copies the file in the root directory (Original in our example), it does not copy the subfolders in it.
- If you want to copy the entire subdirectory, use the following command:
1 2 | rsync -r original/ duplicate/ |
- The
-r
parameter tells rsync to copy everythingbao gồm thư muc con
and files from the root directory. - The
/
sign after theoriginal
directory refers to rsynccopy
content from the original directory to the duplicate directory. - If you want to
đồng bộ files
– sinceđồng bộ files
is a 2-way copy, the files in the duplicate folder that aren’t in the original directory will be copied back, use the following command:
1 2 | rsync -r original duplicate/ |
- With this command, you can be sure that both the root and target directories will contain the same files.
- Another useful parameter is
-a
. It represents the archive and can be combined with other commands. This means that it not only copies files, it copies properties such as permissions, modification time, and other types of dates. - To use the -a command would be as follows:
1 2 | rsync -av --dry-run Original/ Duplicate/ |
- First, the command above will only show the files to be copied without actually copying the file. You will see a list of such files on the screen.
- If the files display correctly for you, then run the command again but remove the
—dry-run
element. —dry-run
is used to run a test first so that you see the changes, but the copy action will not actually take place.-a
enable archive mode-v
add the after-a
to combine to the-av
parameter in the command, so I can see how the copy process is going on.- If you want to sync 2 folders, but delete the duplicate files that are not in the root directory, then add
–delete
, rsync Linux–delete
will look like this:
1 2 | rsync -av --delete original/ duplicate/ |
The last command we want to talk about is -z
. This command is used to nén (compress)
files to transfer over the network. Let’s see we use this command to transfer files from one server to another:
1 2 | rsync -za ~/Desktop/Original ip:~/tmp/ |
- As mentioned before,
-z
compresses the file,-a
creates an archive to ensure permissions inside files are copied. ~/Desktop/Original
is the source directory. It is on your computer, this directory is in the machine where you can enter it andip:~/tmp/
is the remote server’s ip and the servre’s destination directory
C. Finish
Above is my understanding of Rsync so in php how to be able to implement it (Requires highest level of permission) These are the 2 commands I use in my project as follows:
1. Perform data synchronization after image upload
1 2 3 | $command = sprintf('rsync %s --rsync-path="mkdir -p %s && rsync" %s:%s', $saveImagePath, $folderImage, $hostname, $saveImagePath); exec($command, [], $returnVal) |
- Uploading the image I will put in each directory for each model (product, post … etc) so have to divide by the same directory with the command
--rsync-path="mkdir -p %s && rsync"
it will create the necessary directory (if it is not created any more based on the-p
parameter) and the rest will be easy. - $ returnVal here is the return command value (true / false).
2. Delete the data in the destination folder
1 2 3 | $command = sprintf('rsync --delete %s:%s', $hostname, $fileName); $cmd = exec($command, [], $return_val); |
My article will end here, hopefully this article will be helpful in the project if you encounter it. Thank you for watching