In an enterprise, there are many servers, and a cluster, such as a website directory, needs to be synchronized. When the environment was just set up, it was impossible to write one file at a time or copy it to the server one by one. Here I wrote a script to automatically move files and copy the required files to the newly built server.
#!/bin/bash #Auto change server files #Author kanghui 2016-06-17 #Source server 192.168.33.10 #Target server 192.168.33.14 if [ ! -f ip.txt ];then echo -e "\033[33mplease create ip.txt files,the ip.txt contents as follows: \033[0m" cat <<EOF 192.168.149.128 192.168.149.129 EOF exit be if [ -z "$1" ];then #This will prompt you to enter and exit the source file directory, here we can do a key-free login #ssh-keygen keep pressing Enter, then copy the key to the target server #ssh-copy-id -i /root/.ssh.id_rsa.pub 192.168.33.14 Enter yes and the password will not be entered again #ssh-copy-id -i /root/.ssh.id_rsa.pub 192.168.33.10 Execute this command locally #Then execute the script install.log /tmp (install.log is the source file /tmp is the target directory, is it super simple) echo -e "\033[32m usage: $0 command ,example{Src_Files|Src_Dir} \033[0m" exit be count=`cat ip.txt |wc -l` rm -rf ip.txt.swp i=0 #i is less than count, then execute the command inside, 0 meets the requirements and then +1 while ((i< $count)) do i=`expr $i + 1` #Print this line, add a mark in front of him, which is 1 sed "${i}s/^/&${i} /g" ip.txt >>ip.txt.swp IP=`awk -v I="$i" '{if(I=$1)print $2}' ip.txt.swp` scp -f $1 root@${IP}:$2 #The following code is to keep the two directories consistent. If you want to use the following, just comment out the above one and open the following one. #For example, execute the command sh auto_scp.sh /tmp/ /tmp/ Here is the original directory and the target directory, if the prompt command is not found, then yu rsync #rsync -aP --delete $1 root@${IP}:$2 done
or