CentOS 7.2 Ubuntu 18部署Rsync + Lsyncd服務實現文件實時同步/備份

發送端配置:php

1、配置密鑰css

1. 主/從服務器之間啓用基於密鑰的身份驗證。登陸發送端服務器並用 " ssh-keygen " 命令生成公共或私有的密鑰。ubuntu

2. 使用 " ssh-copy-id " 複製密鑰文件到接收端服務器。centos

ssh-copy-id -i /root/.ssh/id_rsa.pub root@IP
把生成的公鑰發送到對方的主機上去,用ssh-copy-id命令,自動保存在對方主機的/root/.ssh/authorized_keys文件中去
[root@localhost ~ 09:16:45&&26]# ssh 192.168.1.160 #須要登陸密碼 
[root@localhost ~ 09:16:45&&26]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.160 
[root@localhost ~ 09:16:45&&26]# ssh 192.168.1.160 #免密登陸 

 

輸入登陸密碼服務器

問題1:ssh:Permissions 0644 for ‘/root/.ssh/id_rsa’ are too open

若提示「Permissions 0644 for ‘/root/.ssh/id_rsa’ are too open」,而且斷開鏈接。ssh

仔細閱讀了一下ssh文檔和這句提示,大概的意思時ssh的私有密鑰的權限開放尺度太大了,能夠供人隨意欣賞了,ssh自身的策略關閉了ssh。post

解決方案:將權限由0644下降爲0600lua

chmod 0600 ~/.ssh/id_rsacentos7

問題2:ssh免密登陸出現問題再從新寫入key的時spa

解決辦法是加參數ssh-copy-id -f root@IP強制從新寫入或者寫入新的key ssh-keygen -R IP(B),前者可行 寫入後能正常登陸

 加-f的時候須要嘗試登陸一下------>實際上 

客戶端執行ssh-copyid 服務端用戶名@服務端ip此步驟須要輸入密碼。
執行成功後會在服務端生成~/.ssh/authorized_keys文件,文件內容和客戶端生成的id_rsa.pub內容徹底一致。

再次執行ssh 用戶名@ip 就不用輸入密碼了。

2、安裝rsync + lsyncd

centos7:  yum -y install lsyncd   
ubuntu18: apt install lsyncd

看具體狀況 安裝lsyncd依賴包
centos的配置文件是/etc/lsyncd.conf

ubuntu的配置文件是/etc/lsyncd/lsyncd.conf.lua

且ubuntu要手動建立/etc/lsyncd文件夾、log文件、status文件,centos不用 其餘都同樣

3、配置lsyncd

      1.若是有example案例文件複製成配置文件

cp /usr/share/doc/lsyncd-2.1.5/examples/lrsync.lua /etc/lsyncd.conf

  2. 編輯lsyncd配置文件

centos 7   
位置:/etc/lsyncd.conf
---- -- User configuration file for lsyncd. -- -- Simple example for default rsync, but executing moves through on the target. -- -- For more examples, see /usr/share/doc/lsyncd*/examples/ -- -- 分發服務器 settings { logfile ="/var/log/lsyncd/lsyncd.log", ---->須要配置或建立具體的文件目錄或者文件 statusFile = "/var/log/lsyncd/lsyncd.stat", statusInterval = 1,
maxProcesses = 10, ---->這裏變成10會報錯 只能1個進程
nodaemon = false,
maxDelays = 7
} sync{ default.rsyncssh, source="/www/wwwroot/www.xxx.com", host="192.168.0.1", init = false, --->通常爲false 服務啓動的時候不會報錯 targetdir="/www/wwwroot/test.com", delete = true, delay = 0, rsync = { binary = "/usr/bin/rsync", archive = true, --歸檔 compress = true, --壓縮 verbose = true, owner = true, --屬主 perms = true, --權限 _extra = {"--bwlimit=2000"}, }, ssh = { port = 22 } } sync{ default.rsyncssh, source="/www/wwwroot/www.xxx.com", host="192.168.0.2", init = false, targetdir="/www/wwwroot/test.com", delete = true, delay = 0, rsync = { binary = "/usr/bin/rsync", archive = true, --歸檔 compress = true, --壓縮 verbose = true, owner = true, --屬主 perms = true, --權限 _extra = {"--bwlimit=2000"}, }, ssh = { port = 22 } }

雙向同步

ubuntu 18
文件位置: /etc/lsyncd/lsyncd.conf.lua
settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status" } sync { default.rsyncssh, source = "/www/wwwroot/test.com", host = "47.244.107.27", targetdir = "/www/wwwroot/www.XXX.com", init = false, delay=0, rsync = { binary = "/usr/bin/rsync", archive = true, compress = true, --壓縮傳輸默認爲true。在帶寬與cpu負載之間權衡,本地目錄同步能夠考慮把它設爲false verbose = true }, ssh = { port = 22 } }

4、啓動服務,並設置開機啓動

1. 啓動lsyncd服務

-----centos7
lsyncd /etc/lsyncd.conf   ---->檢查配置信息是否正確
systemctl start lsyncd
systemctl status lsyncd
systemctl restart lsyncd
-----ubuntu18
/etc/init.d/lsyncd restart
/etc/init.d/lsyncd start
/etc/init.d/lsyncd status ----------------------------

2. 啓動完成查看lsyncd狀態,確保lsync啓動成功

3. 設置開機啓動

systemctl enable lsyncd
相關文章
相關標籤/搜索