Rsync同步、Rsync+Lsync实时同步
原创博文http://www.cnblogs.com/elvi/p/7658049.html
#!/bin/sh #Myde by Elven @2017 #centos 6x , centos 7x #Rsync同步、Rsync+Lsync实时同步 ### 全局配置 #Rsync服务端配置 RS_IP=192.168.20.1 BkDir=/www/backup #Lsync目录配置 Source="/www/backup/tobak/" Target="test@$RS_IP::test" ### rsync同步服务端(备份目标地址) function rs_s() { echo "#目录设置" mkdir -p $BkDir/test touch $BkDir/test/abc{1..8}.txt # useradd rsync -s /sbin/nologin -M # chown -R rsync /www/backup/web echo "#安装……" [[ `rpm -qa rsync|wc -l` == 0 ]] && { yum install rsync -y; } || echo rsync is OK #yum install -y rsync echo "#Rsync服务端配置" echo " # rsync配置 uid = root gid = root user chroot = no max connections = 200 timeout = 600 port = 873 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log hosts allow = 192.168.20.0/24 hosts deny = * secrets file = /etc/rsync.db ignore errors [backup] comment= backup server path=/www/backup/ read only = false list = true auth users = rsync [test] comment= backup test path=/www/backup/test/ read only = false list = true auth users = test ">/etc/rsyncd.conf #用户验证 echo "rsync:rsync2017">/etc/rsync.db echo "test:test2017">>/etc/rsync.db chown root:root /etc/rsync.db chmod 600 /etc/rsync.db chmod 600 /etc/rsyncd.conf [[ `uname -r` == *el6* ]] && { # centos6用xinetd管理rsync进程 #rsync --daemon --address=192.168.20.41 [[ `rpm -qa xinetd|wc -l` == 0 ]] && { yum install xinetd -y; } || echo xinetd is OK sed -in '6s#yes#no#p' /etc/xinetd.d/rsync # disable = yes 改成 disable = no service xinetd restart chkconfig xinetd on chkconfig rsync on }|| { #centos 7 systemctl restart rsyncd systemctl enable rsyncd >/dev/null 2>&1 } # netstat -antp|grep 873 [[ $? == 0 ]] && { echo "#rsync服务已启动……";exit ; } echo "#查看列表" rsync --list-only rsync://test@127.0.0.1 [[ `grep $(hostname) /etc/hosts|wc -l` == 0 ]] && echo "#需要把hostname添加到/etc/hosts" } ### rsync同步客户端(备份源) function rs_c() { echo "#安装……" [[ `rpm -qa rsync|wc -l` == 0 ]] && { yum install rsync -y; } || echo "rsync 已安装" echo "#查看列表" rsync --list-only rsync://test@$RS_IP echo "rsync2017">/etc/rsync.pwd echo "test2017">/etc/rsync.ps chmod 600 /etc/rsync.ps chmod 600 /etc/rsync.pwd } ###Lsync实时同步,客户端(备份源) function lsync_c() { mkdir -p $Source echo "#Lsync安装配置" [[ `rpm -qa epel-release|wc -l` == 0 ]] && { yum install epel-release -y; } [[ `rpm -qa lsyncd|wc -l` == 0 ]] && { yum install lsyncd -y; } || echo "lsyncd 已安装" [[ `rpm -qa lua lua-devel|wc -l` != 2 ]] && { yum install lua lua-devel -y; } || echo "lua 已安装" echo ' -- #全局配置 settings { logfile ="/var/log/lsyncd.log", statusFile = "/tmp/lsyncd.stat", statusInterval = 10, inotifyMode = "CloseWrite or Modify", maxProcesses = 8, maxDelays = 5, } -- #远程目录实时同步,rsync模式 sync { default.rsync, source = "'$Source'", target = "'$Target'", delete = running, exclude = { ".*",".tmp" }, delay = 3, init = false, -- #默认 true 当init = false ,只同步启动后发生改动事件 rsync = { binary = "/usr/bin/rsync", archive = true, compress = true, verbose = true, copy_links = true, password_file = "/etc/rsync.ps", -- _extra = {"--bwlimit=200"} } } -- 本地目录同步测试,rsync模式 sync { default.rsync, source = "/tmp/src", target = "/tmp/dest", delay = 1, rsync = { binary = "/usr/bin/rsync", archive = true, compress = false, --bwlimit = 10240 } } '>/etc/lsyncd.conf #创建测试目录 mkdir /tmp/{src,dest} >/dev/null 2>&1 #启动服务 [[ `uname -r` == *el6* ]] && { service lsyncd restart chkconfig lsyncd on } || { systemctl restart lsyncd systemctl enable lsyncd >/dev/null 2>&1 } [[ `ps -ef|grep lsync|grep -v grep|wc -l` == 1 ]] && { echo 'Lsync启动成功……' echo "本地目录同步测试" echo "rm -f /tmp/{src,dest}/ #清空目录" rm -f /tmp/{src,dest}/* echo "touch /tmp/src/luck-{1..8}.txt #创建文件,测试同步" touch /tmp/src/luck-{1..8}.txt sleep 2 echo "#查看同步" echo "ls -l /tmp/src";ls -l /tmp/src echo "ls -l /tmp/dest";ls -l /tmp/dest } || { echo 'Lsync启动失败……' echo "请检查配置文件 /etc/lsyncd.conf " } } function rs_help() { echo " # rsync -a参数 源 目标 #rsync -avzP --delete /root/test/ /www/backup/test/ #查看列表 rsync --list-only rsync://test@$RS_IP #测试,拉取 rsync -avzP --delete --password-file=/etc/rsync.pwd test@$RS_IP::test /tmp/rsync/ #测试,推送 rm -rf /tmp/rsync/* touch /tmp/rsync/abc{1..9}.txt ll /tmp/rsync/ rsync -avzP --delete --password-file=/etc/rsync.pwd /tmp/rsync/ test@$RS_IP::backup #使用ssh隧道 rsync -avzP -e ' ssh -p 22' root@$RS_IP:/www/backup/test/ /test/ #服务端设置固定IP rsync --daemon --address=192.168.20.41 " } ################ function mks() { #Rsync编译安装参考 #建议先yum安装后,在编辑安装升级 cd /tmp #wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz tar xf rsync-3*.tar.gz cd rsync-3* ./configure make && make install #替换yum安装文件/usr/bin/rsync [[ -f /usr/bin/rsync ]] && { mv /usr/bin/rsync /usr/bin/rsyncd ln -s /usr/local/bin/rsync /usr/bin/rsync /usr/bin/rsync --version|head -1 } || { /usr/bin/rsync --version|head -1 } } ################ case "$1" in "s") rs_s exit ;; "c") rs_c exit ;; "l") rs_c lsync_c exit ;; "h") rs_help exit ;; *) echo " $0 + { s|c|l|h }" echo " s #安装Rsync服务端" echo " c #安装Rsync客户端" echo " l #安装Rsync+Lsync实时同步客户端" echo " h #rsync 同步示例" exit ;; esac