服务器群秒级别文件同步(ssh+SHELL)
Posted vijayfly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了服务器群秒级别文件同步(ssh+SHELL)相关的知识,希望对你有一定的参考价值。
1.介绍
\\
2.业务服务器远程更新浏览服务器文件的脚本
#!/bin/bash operate=$1 ip=$2 conf_file="/var/www/html/test/ip_list" file_chsum="/var/www/html/test/cksvalue" file_ser="10.60.10.62" cksum(){ ssh ${file_ser} "cksum ${conf_file}|awk \'{print\\$1}\' > ${file_chsum}" if [ $? -ne 0 ];then #生成cksum值失败 echo "10005" fi } if [[ ! $ip =~ ^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$ ]];then #IP不合法 echo "10003"; exit 1; fi if [[ "${operate}" == "add" ]];then ips=`ssh ${file_ser} "cat $conf_file|grep ${ip}/32|wc -l"` if [ ${ips} -eq 0 ];then ssh ${file_ser} "echo "${ip}/32" >> $conf_file" if [ $? -eq 0 ];then cksum #添加IP成功 echo "10000" exit 0; else #添加IP失败 echo "10001" exit 1; fi else #添加IP成功(IP已存在) echo "10000"; exit 0; fi elif [[ "${operate}" == "del" ]];then ssh ${file_ser} "sed -i \'/${ip}\\/32/d\' $conf_file" if [ $? -eq 0 ];then cksum #删除IP成功 echo "10000" exit 0; else #删除IP失败 echo "10002" exit 1; fi else #参数1($1)不合法 echo "10004" exit 1; fi
3.浏览服务器采用http的方式提供配置文件和cksum值的浏览和下载
4.VPS主机群每隔30校验一次配置文件的cksum值来保持配置文件的最新状态
* * * * * sleep 10 && sh /tmp/sync_customIP.sh * * * * * sleep 40 && sh /tmp/sync_customIP.sh
#!/bin/bash conf_file_url="http://10.60.10.62:8060/test/ip_list" cksum_url="http://10.60.10.62:8060/test/cksvalue" local_conf_file="/tmp/ip_list" time=`date` log="/tmp/sync_customIP.log" get_cksum() { remote_cksum=`curl -s "${cksum_url}"` local_cksum=`cksum ${local_conf_file}|awk \'{print\\$1}\'` } sync_file() { cat ${local_conf_file} > /tmp/.tmp_ip.txt wget -q ${conf_file_url} -O ${local_conf_file}; } get_cksum if [[ "$remote_cksum" != "$local_cksum" ]];then sync_file get_cksum if [[ "$remote_cksum" != "$local_cksum" ]];then cat /tmp/.tmp_ip.txt > ${local_conf_file} echo "${time} sync fail.." >> $log else echo "${time} sync success.." >> $log fi else echo "${time} there are no need to sync.. ">> $log fi log_count=`cat $log |wc -l` if [ $log_count -gt 2000 ];then sed -i \'1,1000d\' $log fi
以上是关于服务器群秒级别文件同步(ssh+SHELL)的主要内容,如果未能解决你的问题,请参考以下文章