Rsync同步、Rsync+Lsync實時同步
阿新 • • 發佈:2017-10-23
devel 查看 users ls -l eve 編譯 var 清空 創建
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 = falselist = 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://[email protected] [[ `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
Rsync同步、Rsync+Lsync實時同步