10.28 Introduction to rsync Tool
yum install -y rsync //安装rsync
rsync -av /etc/passwd /tmp/1.txt //Copy the file to the tmp directory and rename it 1.txt
rsync -av /etc/passwd [email protected]:/tmp/1.txt // remote copy
rsync command format:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]IP:DEST
rsync [OPTION]... [USER@]IP:SRC DEST
rsync [OPTION].. . [USER@]IP::SRC DEST
rsync [OPTION]... SRC [USER@]IP::DEST
OPTION indicates options, such as -av, SRC indicates the original directory or file, DEST indicates the destination directory or file, square brackets The user@ in [] can be omitted.
Corresponding to the above six command formats, rsync has six different working modes:
- Copy local files. This mode of operation is enabled when neither SRC nor DES path information contains a single colon ":" separator. Such as:
rsync -a /data /backup
- Use a remote shell program (such as rsh , ssh ) to copy the contents of the local machine to the remote machine. This mode is activated when the DST path address contains a single colon ":" separator. Such as:
rsync -avz *.c foo:src
- Use a remote shell program (such as rsh, ssh) to copy the contents of the remote machine to the local machine. This mode is enabled when the SRC address path contains a single colon ":" separator. Such as:
rsync -avz foo:src/bar /data
- Copy files from remote rsync server to local machine. This mode is enabled when the SRC path information contains the "::" separator. Such as:
rsync -av [email protected]::www /databack
- Copy files from local machine to remote rsync server. This mode is activated when the DST path information contains the "::" separator. Such as:
rsync -av /databack [email protected]::www
- List the files of the remote machine. This is similar to an rsync transfer, except that the local machine information is omitted from the command. Such as:
rsync -v rsync://192.168.78.192/www
Options
-a archive mode, which means to transfer files recursively and keep all attributes, equivalent to -rlptgoD, -a option can be followed by a --no-OPTION which means close, one of -rlptgoD such as -a--no -l is equivalent to -rptgoD
-r The subdirectories are processed in a recursive mode, mainly for directories. If you pass a file alone, you do not need to add -r, but if you transmit a directory, you must add the -r option, similar to cp. -r option
-v print some information, such as rate, number of files, etc., know the process of synchronization
-l keep soft links
-L treat soft links like regular files, if there is a soft link file in the original directory or file , then adding this option will copy the target file pointed to by the soft link to the target directory or file
-p (lowercase) keep the permission attribute of the
file -o keep the owner information of the
file -g keep the group information of the file
- D keep the device file information
-t keep the file time attribute
--delete delete the files that are not in the original file in the target file
--exclude filter the specified file, such as --exclude "logs" will filter out the file or directory whose file name contains logs, not Synchronize. The file name can be in wildcard mode (such as .txt)
-P (uppercase) shows the synchronization process, such as the rate, which is more detailed than -v
--progress You can see the synchronization process status during the synchronization process, such as statistics to be synchronized The number of files, the speed of synchronized file transfer, etc.
-u After adding this option, if the files in the target directory are newer than the files in the original directory, do not synchronize
-z Compress when transferring
test:
Copy the directory root directory 111 to the tmp/111_dest directory
The command rsync -av /root/111/ /tmp/111_dest/ synchronizes the directory /111/ under the original directory /root/ to the /tmp/ directory and changes the name to the /111_dest/ directory. The role of -av here is to include options such as copy directories, soft links, owners, groups, and file permissions to visualize the copy process.
Lys3 is a soft link file, pointing to the original file /tmp/lys2
The function of l in the a option retains the soft link file. Corresponding to l is L, then operate the L option, enter the command rsync -avL /root/111/ /tmp/111_dest/, entering L here is equivalent to overwriting the l in the a option
Enter the content in the file /tmp/lys2, then synchronize the directory, and then check the content of the directory /tmp/111_dest/lys3, you will find that it is the content of the file lys2. This is the role of the L option, which can copy the original file pointed to by the soft link to the target file
In the directory /tmp/111_dest/ directory, create a new file new.txt, and then use the synchronization command --delete option, delete new.txt appears during the execution of the command, delete this file, and then see the new new The .txt file is gone. That's what --delete does. rsync -avL --delete /root/111/ /tmp/111_dest/
Command rsync -avL --exclude ".txt" /root/111/ /tmp/111dest/ , the file with the suffix .txt cannot be seen during the execution, and the result is that there is no file with .txt. In multiple cases, enter the command rsync -avL --exclude ".txt" --exclude "lys" /root/111/ /tmp/111dest/
-P show show the synchronization process, such as rate
-u After adding this option, if the file in the target directory is newer than the file in the original directory, it will not be synchronized