Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the specific method of using rsync

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the specific use of rsync. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Rsync is a tool that enables incremental backups. With task planning, rsync can achieve timing or interval synchronization, and with inotify or sersync, you can achieve trigger real-time synchronization. Here's how to use rsync.

Rsync feature

Speed: the first rsync synchronizes the entire content directory of the source to the target location. After that, rsync passes only the changed blocks or bytes to the target location, making the transfer very fast.

Security: rsync allows data to be encrypted using the ssh protocol during transmission.

Less bandwidth: rsync compresses and decompresses data at the sender and receiver, respectively. So using rsync to transfer data always uses less bandwidth than using other file transfer protocols.

Privileges: no special permissions are required to install and execute rsync

Rsync syntax

$rsync options source destination

Source and destination can be local or remote. In the remote case, you need to specify the login name, the name of the remote server, and the location.

Example of using rsync

1. Synchronize two directories on the local server

Use rsync-zvr to synchronize two directories on the local server

$rsync-zvr / var/opt/installation/inventory/ / root/tempbuilding file list... Donesva.xmlsvB.xml.sent 26385 bytes received 1098 bytes 54966.00 bytes/sectotal size is 44867 speedup is 1.63

In the above rsync example:

-z: compression is allowed

-v: verbose

-r: recursive

Note: Rsync does not synchronize the timestamp of the source file or directory

$ls-l / var/opt/installation/inventory/sva.xml / root/temp/sva.xml-r--r--r-- 1 bin bin 949 Jun 18 2009 / var/opt/installation/inventory/sva.xml-r--r--r-- 1 root bin 949 Sep 2 2009 / root/temp/sva.xml

two。 Synchronize timestamps with rsync-a

The-an option of rsync means archive, and when added, it does the following:

Recursive mode

Synchronous soft link

Synchronization permissions

Synchronization timestamp

Synchronize owners and subordinate groups

Now, we execute the same command as example 1, except that we use the-an option.

$rsync-azv / var/opt/installation/inventory/ / root/temp/building file list... Done./sva.xmlsvB.xml.sent 26499 bytes received 1104 bytes 55206.00 bytes/sectotal size is 44867 speedup is 1.63

We can see that rsync synchronizes the timestamps of the files in the source directory.

$ls-l / var/opt/installation/inventory/sva.xml / root/temp/sva.xml-r--r--r-- 1 root bin 949 Jun 18 2009 / var/opt/installation/inventory/sva.xml-r--r--r-- 1 root bin 949 Jun 18 2009 / root/temp/sva.xml

3. Synchronize only one file

Specify the file name and synchronize only one file

$rsync-v / var/lib/rpm/Pubkeys / root/temp/Pubkeyssent 42 bytes received 12380 bytes 3549.14 bytes/sectotal size is 12288 speedup is 0.99

4. Synchronize local files to a remote server

When synchronizing to the remote server, we need to specify the user name, the IP address of the remote server, and the destination path. Format: username@remote_server_ip:path

$rsync-avz / root/temp/ linuxprobe@192.168.200.10:/home/linuxprobe/temp/Password:building file list... Done./rpm/rpm/Basenamesrpm/Conflictnamesent 15810261 bytes received 412 bytes 2432411.23 bytes/sectotal size is 45305958 speedup is 2.87

5. Synchronize remote files to the local server

$rsync-avz linuxprobe@192.168.200.10:/var/lib/rpm / root/tempPassword:receiving file list... Donerpm/rpm/Basenames.sent 406 bytes received 15810230 bytes 2432405.54 bytes/sectotal size is 45305958 speedup is 2.87

6. Specify to use a certain shell synchronization

When synchronizing remotely, Rsync can specify that a shell is used for secure transmission. For example, use rsync-e ssh to select ssh for secure connection.

$rsync-avz-e ssh linuxprobe@192.168.200.10:/var/lib/rpm / root/tempPassword:receiving file list... Donerpm/rpm/Basenamessent 406 bytes received 15810230 bytes 2432405.54 bytes/sectotal size is 45305958 speedup is 2.87

7. Do not overwrite files whose target location has changed

If the file in the target location has been modified and we do not want the old file to overwrite it, we can use the-u option. In the following example, the file Basenames at the target location has been modified, using the-u option so that it is not overwritten by the old file.

$ls-l / root/temp/Basenamestotal 39088-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames$ rsync-avzu linuxprobe@192.168.200.10:/var/lib/rpm / root/tempPassword:receiving file list. Donerpm/sent 122 bytes received 505 bytes 114.00 bytes/sectotal size is 45305958 speedup is 72258.31$ ls-lrttotal 39088-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames

8. Synchronize only the directory tree structure (excluding files)

Use the rsync-d option to synchronize the directory tree only from the source location to the destination location. In the following example, only the directory tree is synchronized recursively, not the files in the directory.

$rsync-v-d linuxprobe@192.168.200.10:/var/lib/. Password: receiving file list. Donelogrotate.statusCAM/YaST2/acpi/sent 240 bytes received 1830 bytes 318.46 bytes/sectotal size is 956 speedup is 0.46

9. View rsync progress

Use the rsync-progress option to check the progress of rsync, such as how many files have been copied, what is the completion rate, etc.

Rsync-avz-- progress linuxprobe@192.168.200.10:/var/lib/rpm/ / root/temp/Password:receiving file list... 19 files to consider./Basenames 5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19) Conflictname 12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19)... sent 406 bytes received 15810211 bytes 2108082.27 bytes/sectotal size is 45305958 speedup is 2.87

10. Delete files created at the target location

If we want to delete files that do not exist in the source location but exist in the destination location when we rsync, we can use the-delete option.

In this example, we create a new file named new-file.txt., at the target location, and during synchronization, the-delete option deletes the new-file.txt file

# Source and target are in sync. Now creating new file at the target.$ > new-file.txt$ rsync-avz-- delete linuxprobe@192.168.200.10:/var/lib/rpm/. Password: receiving file list. Donedeleting new-file.txt./sent 26 bytes received 390 bytes 48.94 bytes/sectotal size is 45305958 speedup is 108908.55

11. Do not create a new file at the target location

Use the-existing option to synchronize only files that exist at the target location during synchronization, without creating new files.

# create a new file new-file.txt [/ var/lib/rpm] $> new-file.txt# synchronization directory $rsync-avz-- existing root@192.168.1.2:/var/lib/rpm/. Root@192.168.1.2's password:receiving file list at the source location. Done./sent 26 bytes received 419 bytes 46.84 bytes/sectotal size is 88551424 speedup is 198991.96

12. View different files or directories of the source and destination

Source location:

$ls-l / var/lib/rpm-rw-r--r-- 1 root root 5357568 2010-06-24 08:57 Basenames-rw-r--r-- 1 root root 12288 2008-05-28 22:03 Conflictname-rw-r--r-- 1 root root 1179648 2010-06-24 08:57 Dirnames

Target location:

$ls-l / root/temp-rw-r--r-- 1 root root 12288 May 28 2008 Conflictname-rw-r--r-- 1 bin bin 1179648 Jun 24 05:27 Dirnames-rw-r--r-- 1 root root 0 Sep 3 06:39 Basenames

There are two differences between source and destination: 1. File Dirnames owner and subordinate group are different 2. The size of the file Basenames varies

Use the-I option to show the differences, as shown below

$rsync-avzi linuxprobe@192.168.200.10:/var/lib/rpm/ / root/temp/Password:receiving file list... Done > f.st. Basenames.f....og. Dirnamessent 48 bytes received 2182544 bytes 291012.27 bytes/sectotal size is 45305958 speedup is 20.76In the output it displays some 9 letters in front of the file name or directory name indicating the changes.

As you can see, there are nine characters before the file or directory name to represent the difference. In this example, the characters before Basenames and Dirnames mean the following:

Indicates that the file has been transferred to the local host. F: represents a file s: represents a change in file size t: represents a change in timestamp o: the owner changes g: the group changes

13. Specify to include or exclude certain files during transfer

Transfer only files or directories that begin with'P'(using rsync include), not any other files (using rsync exclude)

$rsync-avz-- include 'playing'-- exclude'* 'linuxprobe@192.168.200.10:/var/lib/rpm/ / root/temp/Password:receiving file list. Done./PackagesProvidenameProvideversionPubkeyssent 129 bytes received 10286798 bytes 2285983.78 bytes/sectotal size is 32768000 speedup is 3.19

14. Do not transfer large files

When the rsync-max-size option is used, rsync will not transfer files larger than the specified size

$rsync-avz-max-size='100K' linuxprobe@192.168.200.10:/var/lib/rpm/ / root/temp/Password:receiving file list... Done./ConflictnameGroupInstalltidNameSha1headerSigmd5Triggernamesent 252 bytes received 123081 bytes 18974.31 bytes/sectotal size is 45305958 speedup is 367.35

15. Transfer the entire file

One of the main features of rsync is that it transfers only the changed blocks to the target location, not the entire file. If network bandwidth is not a problem for you (CPU has), you can use the-w option to transfer the entire file. It accelerates the rsync process because it does not have to perform a checksum at the source and destination locations.

# rsync-avzW linuxprobe@192.168.200.10:/var/lib/rpm/ / root/tempPassword:receiving file list... Done./BasenamesConflictnameDirnamesFilemd5sGroupInstalltidNamesent 406 bytes received 15810211 bytes 2874657.64 bytes/sectotal size is 45305958 speedup is 2.87 above is the specific usage of rsync shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report