In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Linux data synchronization tool rsync how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Linux data synchronization tool rsync how to use" it!
Before explaining the usage of rsync, in order to give you an overall understanding of this command, here is an example:
[root@localhost] # rsync-av / etc/passwd / tmp/1.txtsending incremental file listsent 34 bytes received 15 bytes 98.00 bytes/sectotal size is 1432 speedup is 29.2
In this example, by executing the rsync command, the / etc/passwd file is locally synchronized to the / tmp/ directory and renamed to 1.txt.
In addition, the rsync command supports remote data synchronization, that is, backing up local data to a remote machine. For example, if we know that the IP address of the remote machine is 192.168.188.128, the execute command to back up the passwd file using the rsync command is:
[root@localhost] # rsync-av / etc/passwd 192.168.188.128:/tmp/1.txt The authenticity of host '192.168.188.128 (192.168.188.128)' can't be established. ECDSA key fingerprint is 26:e3:97:e7:bb:ae:17:33:ea:aa:Oc:5f:37:Oe:9e:fa. Are you sure you want to continue connecting (yes/no)? Yes Warning: Permanently added '192.l68.l88.l28' (ECDSA) to the list of known hosts. Root@192.168.188.128's password:
Sent 31 bytes received 12 bytes 7.82 bytes/sec total size is 1432 speedup is 54.91
❝
Note that when you connect remotely for the first time, you will be prompted whether you want to continue the connection, just enter yes. In addition, when the connection is successfully established, you need to enter the root password of the target system.
Through the above two examples, readers should be able to have an intuitive understanding of "rsync supports both local and remote backup data". So, how do you use the rsync command?
There are several basic formats for rsync commands, which are:
[root@localhost ~] # rsync [OPTION] SRC DEST [root@localhost ~] # rsync [OPTION] SRC [USER@] HOST:DEST [root@localhost ~] # rsync [OPTION] [USER@] HOST:SRC DEST [root@localhost ~] # rsync [USER@] HOST::SRC DEST [root@localhost ~] # rsync [OPTION] SRC [USER@] HOST::DEST
Rsync has five different working modes for the above five command formats:
The first is for backing up data locally only; the second is for backing up local data to a remote machine; the third is for backing up data from a remote machine to a local machine; and the fourth is relative to the third. Similarly, the fifth and the second are relative, and the difference between them lies in the different authentication methods used when logging in to authentication.
You know, before using rsync to transfer data remotely (backup data), login authentication is required, and this process can only be completed with the help of ssh protocol or rsync protocol. In the rsync command, if you use a single colon (:), the ssh protocol is used by default; conversely, if you use two colons (::), the rsync protocol is used.
❝
The difference between ssh protocol and rsync protocol is that rsync protocol requires additional configuration, which increases the workload, but the advantage is that it is more secure; on the contrary, ssh protocol is easy to use, does not need to be configured, but has the risk of leaking server password.
In addition, the meanings of the parameters in the above formats are as follows:
SRC: used to indicate the location (path) of the target data to be backed up; DEST: used to indicate where the data will be backed up; when USER@: is used as a remote synchronization operation, you need to specify the user name of the system login. If it is not specified, the default is to log in to the system as root and complete the synchronization operation.
The rsync command provides the OPTION and functions used as shown in Table 1.
OPTION option function-a this is the archiving mode, which means that the file is transferred recursively and all attributes are maintained, which is equivalent to the-r,-l,-p,-t,-g,-o,-D options. The-an option can be followed by a-no-OPTION to turn off one of-r,-l,-p,-t,-g,-o,-D options, such as-a-no-l equals-r,-p,-t,-g,-o,-D options. -r means to deal with subdirectories in recursive mode, which is mainly for directories. If you send a file separately, you don't need to add the-r option, but you must add it when you transfer the directory. -v means to print some information, such as the list of files, the number of files, etc. -l means that the soft connection is reserved. -L means to handle soft connections as if they were regular files. If there is a soft connection file in SRC, if this option is added, the target file that the soft connection points to will be copied to DEST. -p means to maintain file permissions. -o means to keep the file owner information. -g means to keep the file belonging to the group information. -D means to keep the device file information. -t means to keep file time information. -delete means to delete files in DEST that are not available in SRC. -exclude=PATTERN indicates that files that do not need to be transferred are specified, and the equal sign is followed by a file name, which can be a wildcard pattern (such as * .txt). -progress indicates that you can see the process status of synchronization during synchronization, such as counting the number of files to be synchronized, the file transfer speed of synchronization, and so on. -u means that files in DEST that are newer than SRC are excluded and will not be overwritten. -z with this option, it will be compressed during transmission.
Show details
These are just a few of the options commonly used in the async command, and for beginners, remember the most commonly used options, such as-a,-v,-z,-delete, and-exclude.
❝
If you want to see all the options provided by async, you can execute the async command directly.
In order to better demonstrate the functionality of each option, you need to do some preparatory work by executing the following command:
# create a new rsync directory [root@localhost ~] # mkdir rsync
[root@localhost ~] # cd rsync # create a test1 directory in the rsync directory
[root@localhost rsync] # mkdir test1
[root@localhost rsync] # cd test1 # create files named 1, 2, 3, / root.123.txt in the test1 directory
[root@localhost test1] # touch 123 / root/123.txt
[root@localhost test1] # ln-s / root/123.txt. / 123.txt
[root@localhost test1] # ls-l total 0-rw-r-r-. 1 root root 0 0ct 23 07:34 1 lrwxrwxrwx. 1 root root 13 0ct 23 08:34 123.txt-> / root/123.txt-rw-r-r-. 1 root root 0 0ct 23 07:34 2-rw-r-r-. 1 root root 0 0ct 23 07:34 3
[root@localhost test1] # cd.. # go back to the rsync directory
[root@localhost rsync] #
On this basis, here are several commonly used OPTION options to give you examples of their usage.
Rsync-an option
First, let's take a look at the use of the-an option, as follows:
[root@localhost rsync] # rsync-a test1 test2
[root@localhost rsync] # ls test2 test1
[root@localhost rsync] # ls test2/test1/ 1 123.txt 23
There is a problem here. We wanted to put the contents of the test1 directory directly into the test2 directory, but instead the rsync command created a new test2 directory and put test1 in the test2.
If you want to back up the contents of the test1 directory directly to the test2 directory, modify the above command as follows:
[root@localhost rsync] # rm-rf test2 [root@localhost rsync] # rsync-a test1/ test2/ [root@localhost rsync] # ls test2/ 1 123.txt 23
As you can see, just add a / slash to the test1 and test2 directories.
As mentioned earlier, using the-an option is equivalent to using the-r,-l,-p,-t,-g,-o,-D options at the same time, and-a can also be used with-no-OPTION. Let's take a look at the effect of the-l option:
[root@localhost rsync] # rm-rf test2
[root@localhost rsync] # rsync-av test1/ test2/ sending incremental file list created directory test2. / 1 skipping non-regular file "123.txt" 23
Sent 200 bytes received 72 bytes 544.00 bytes/sec total size is 13 speedup is 0.05
Using the-v option here, you can see that the non-ordinary file 123.txt is skipped during the copy process. In fact, 123.txt is a soft-linked file. If you do not use the-l option, the system will ignore the soft-linked file.
Rsync-delete option
As you can see in Table 1, the-delete option is used for-delete to delete files that SRC does not have in DEST. For example:
# copy the data in the test1 directory
[root@localhost rsync] # rsync-a test1/ test2 # Delete test1/123.txt file
[root@localhost rsync] # rm-f test1/123.txt [root@localhost rsync] # ls test1/123
[root@localhost rsync] # rsync-av test1/ test2/ sending incremental file list. /
Sent 55 bytes received 15 bytes 140.00 bytes/sec total size is 0 speedup is 0.00
[root@localhost rsync] # ls test2/ 1 123.txt 23
As you can see, when the 123.txt file is deleted from the test1 directory, the backup again does not have any effect on the 123.txt file in the test2 directory.
"using the-delete option, execute the copy command again, as follows:"
[root@localhost rsync] # rsync-av-delete test1/ test2/ sending incremental file list deleting 123.txt
Sent 52 bytes received 12 bytes 128.00 bytes/sec total size is 0 speedup is 0.00
[root@localhost rsync] # ls test2/ 1 2 3
As you can see, when you use the-delete option to back up data, once the test1 directory is changed, test2 will change accordingly.
Not only that, if you add files to DEST that are not included in SRC, those files added to DEST will be deleted when you do a synchronous backup with the-delete option. For example:
[root@localhost rsync] # touch test2/4 [root@localhost rsync] # ls test1/ 1 2 3
[root@localhost rsync] # ls test2/ 1 2 3 4
[root@localhost rsync] # rsync-a-delete test1/ test2/
[root@localhost rsync] # ls test2/ 1 2 3
Thank you for reading, the above is the content of "how to use Linux data synchronization tool rsync". After the study of this article, I believe you have a deeper understanding of how to use Linux data synchronization tool rsync, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.