Rsync incremental copy tool for Linux

Brief introduction

scp is a safe copy tool. It is a full copy. The
full copy means that all the things in the directory will be copied over, even if your directory has not been modified after the first copy, it will still be given when you execute it the second time. All the directories you specify are copied, so scp is copied in full.

For example, the files in my hello directory are relatively large, and I have copied them all to another machine before, but then due to business adjustments, some of my hello files have made minor changes. I hope you When copying, just copy the modified files to another machine, that is, incremental copy. At this time, scp is not suitable. You need to use the rsync tool.

The rsync tool can only synchronize files on this machine to other machines.

rsync features

Rsync can just synchronize changed files, that is, incremental synchronization.

How can you tell if you are a changed file? Generally, it is to compare the modification time of the file. If the modification time of the file with the same file name under the same path on the two machines is different, rsync will think that the file has changed.

Use command description

	 rsync -rvlt   path1    目标文件用户名B@主机名2:path2
 **说明**
	-r: 递归,复制目录
	-v: 显示复制的过程
	-l:  同步软连接
	-t:  基于文件的修改时间进行对比,只同步修改时间不同的文件

	path1     是本机文件目录.

Pay attention to the directory slash problem

Note: rsync -rvlt path1 target file user name B@host name2: path2

path1 is a directory, the directory ends with / , only the content in the directory will be synchronized, not the directory itself!
path1 is a directory. The directory does not end with / . If you synchronize the contents of the directory, you will also synchronize the directory itself!
There are cases below to see the difference between adding and not adding.

Case

Install plugin

The synchronized machine needs to install the plug-in, and the synchronized machine also needs to install the plug-in

zjj101 and zjj102 install this plug-in
shell separately:

yum -y install rsync

If you don't install this plugin, you will get an error after entering the password of the target server, which is the following error.

bash: rsync: 未找到命令
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2]

Prepare data

zjj101

[root@zjj101 demo]# pwd
/root/soft/demo
[root@zjj101 demo]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 14 19:31 demoSCP.txt
[root@zjj101 demo]# 

zjj102

[root@zjj102 soft]# pwd
/root/soft
[root@zjj102 soft]# ls
hadoop-2.7.2

Start syncing

Add a slash at the end of the starting directory path

[root@zjj101 demo]# rsync -rvlt /root/soft/demo/     root@zjj102:/root/soft
root@zjj102's password: 
sending incremental file list
./
demoSCP.txt

sent 110 bytes  received 38 bytes  59.20 bytes/sec
total size is 0  speedup is 0.00
[root@zjj101 demo]# 

Check the zjj102 machine
at this time and then generate it in the soft directory

[root@zjj102 soft]# ls
demoSCP.txt  hadoop-2.7.2
[root@zjj102 soft]# pwd
/root/soft
[root@zjj102 soft]# 

No slash at the end of the starting directory path

zjj101

[root@zjj101 demo]# rsync -rvlt /root/soft/demo     root@zjj102:/root/soft 
root@zjj102's password: 
sending incremental file list
demo/
demo/demoSCP.txt

sent 123 bytes  received 39 bytes  64.80 bytes/sec
total size is 0  speedup is 0.00
[root@zjj101 demo]# 

zjj102 machine

Delete the demo folder and delete the effect after retesting.

[root@zjj102 soft]# ls
demo  hadoop-2.7.2
[root@zjj102 soft]# pwd
/root/soft
[root@zjj102 soft]#  cd demo
[root@zjj102 demo]# ls
demoSCP.txt
[root@zjj102 demo]#  pwd
/root/soft/demo
[root@zjj102 demo]# 

The demo folder is also synchronized

Guess you like

Origin blog.csdn.net/qq_41489540/article/details/109081656