Pre-knowledge
rsync can use SSH commands to log in to other machines on the current machine to operate other machines to execute specified commands
Log in and execute the command, and return the command result. Note that the login person's identity
ssh root@zjj102 ls /usr/local
prints out the content under the /usr/local path on the zjj102 machine.
[root@zjj101 soft]# ssh root@zjj102 ls /usr/local
apache-tomcat-8.5.38
sentinelLog.log
share
src
startUpSentinel.sh
[root@zjj101 soft]#
Write distribution script
If multiple machines are synchronizing files, you cannot manually execute the rsync command to synchronize the files. This will be exhausted. Write a synchronization script directly to synchronize the configuration in batches, which is equivalent to encapsulating the commands in a shell script, so you don't need to do it every time Write the command again, it is also very convenient to use.
The following script is that I can synchronize the files in the folder on the zjj102 and zjj103 machines through the zjj101 machine.If there is a soft link in the folder, the files in the corresponding directory of the soft link will also be synchronized.
The content of the xsync file:
#!/bin/bash
#校验参数是否合法
if(($#==0))
then
echo 请输入要分发的文件!
exit;
fi
# -P 如果是软连接也能获取分发文件的绝对路径
dirpath=$(cd `dirname $1`; pwd -P)
filename=`basename $1`
echo 要分发的文件的路径是:$dirpath/$filename
#循环执行rsync分发文件到集群的每条机器
# for循环适当修改一下, 也可以在这里写死你自己的服务名字.
# 我这里的服务器名字是 zjj102和 zjj103
for((i=102;i<=103;i++))
do
echo ---------------------zjj$i---------------------
rsync -rvlt $dirpath/$filename root@zjj$i:$dirpath
done
Use rsync to distribute scripts
The "/root/soft" path under the zjj101 machine has a demo folder,
while the "/root/soft" path under the zjj102 and zjj103 machines does not have the demo folder.
Start to synchronize the things in the demo folder under the zjj101 file to zjj102 And above zjj103.
Ready to work
Create a new demo folder under the zjj101 machine/root/soft folder, and then create a new demoSCP.txt file, and write something in it.
zjj101 machine
[root@zjj101 soft]# pwd
/root/soft
[root@zjj101 soft]# ls
data demo docker hadoop-2.7.2 hive-1.2.1 movie_info.txt myconf sortby-result tmp xsync
[root@zjj101 soft]# cd demo/
[root@zjj101 demo]# ls
demoSCP.txt
[root@zjj101 demo]# cat demoSCP.txt
sdklasjlkd
[root@zjj101 demo]#
zjj102 machine
[root@zjj102 soft]# pwd
/root/soft
[root@zjj102 soft]# ls
hadoop-2.7.2
[root@zjj102 soft]#
zjj103 machine
[root@zjj103 soft]# pwd
/root/soft
[root@zjj103 soft]# ls
hadoop-2.7.2 myconf
[root@zjj103 soft]#
Start syncing
relative path
zjj101
[root@zjj101 soft]# sh xsync demo/
要分发的文件的路径是:/root/soft/demo
---------------------zjj102---------------------
sending incremental file list
demo/
demo/demoSCP.txt
sent 136 bytes received 39 bytes 350.00 bytes/sec
total size is 11 speedup is 0.06
---------------------zjj103---------------------
sending incremental file list
demo/
demo/demoSCP.txt
sent 136 bytes received 39 bytes 116.67 bytes/sec
total size is 11 speedup is 0.06
[root@zjj101 soft]#
zjj102View the results
[root@zjj102 soft]# ls
demo hadoop-2.7.2
[root@zjj102 soft]# cd demo/
[root@zjj102 demo]# ls
demoSCP.txt
[root@zjj102 demo]# cat demoSCP.txt
sdklasjlkd
[root@zjj102 demo]#
At this time, it is found that the contents of the demo folder have been synchronized, zjj103 does not paste the shell command results, and the same result as zjj102, the demo is also synchronized.
Absolute path
A new demo2.txt was added to the zjj101 machine
and then synchronized with the rsync script. At this time, the absolute path "sh xsync /root/soft/demo" was entered. The zjj102 and zjj103 machines found that they had been synchronized in the demo folder. New documents.
[root@zjj101 demo]# pwd
/root/soft/demo
[root@zjj101 demo]# ls
demo2.txt demoSCP.txt
[root@zjj101 demo]# cd ../
[root@zjj101 soft]# ls
data demo docker hadoop-2.7.2 hive-1.2.1 movie_info.txt myconf sortby-result tmp xsync
[root@zjj101 soft]# sh xsync /root/soft/demo
要分发的文件的路径是:/root/soft/demo
---------------------zjj102---------------------
sending incremental file list
demo/
demo/demo2.txt
sent 158 bytes received 39 bytes 131.33 bytes/sec
total size is 20 speedup is 0.10
---------------------zjj103---------------------
sending incremental file list
demo/
demo/demo2.txt
sent 158 bytes received 39 bytes 394.00 bytes/sec
total size is 20 speedup is 0.10
[root@zjj101 soft]#
zjj102
[root@zjj102 demo]# ls
demo2.txt demoSCP.txt
[root@zjj102 demo]#