CentOS 7.2 Ubuntu 18部署Rsync + Lsyncd服务实现文件实时同步/备份
發送端配置:
一、配置密鑰
1.?主/從服務器之間啟用基于密鑰的身份驗證。登錄發送端服務器并用 " ssh-keygen " 命令生成公共或私有的密鑰。
2. 使用 " ssh-copy-id " 復制密鑰文件到接收端服務器。
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。
解決方案:將權限由0644降低為0600
chmod 0600 ~/.ssh/id_rsa
問題2:ssh免密登錄出現問題再重新寫入key的時
解決辦法是加參數ssh-copy-id -f root@IP強制重新寫入或者寫入新的key?ssh-keygen -R IP(B),前者可行 寫入后能正常登錄
?加-f的時候需要嘗試登錄一下------>實際上?
客戶端執行ssh-copyid 服務端用戶名@服務端ip此步驟需要輸入密碼。
執行成功后會在服務端生成~/.ssh/authorized_keys文件,文件內容和客戶端生成的id_rsa.pub內容完全一致。
再次執行ssh 用戶名@ip?就不用輸入密碼了。
二、安裝rsync + lsyncd
centos7: yum -y install lsyncdubuntu18: apt install lsyncd
看具體情況 安裝lsyncd依賴包 centos的配置文件是/etc/lsyncd.confubuntu的配置文件是/etc/lsyncd/lsyncd.conf.lua且ubuntu要手動創建/etc/lsyncd文件夾、log文件、status文件,centos不用 其他都一樣
三、配置lsyncd
? ? ? 1.如果有example案例文件復制成配置文件
cp /usr/share/doc/lsyncd-2.1.5/examples/lrsync.lua /etc/lsyncd.conf2. 編輯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負載之間權衡,本地目錄同步可以考慮把它設為falseverbose = true},ssh = {port = 22} }
四、啟動服務,并設置開機啟動
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轉載于:https://www.cnblogs.com/shione/p/10775649.html
總結
以上是生活随笔為你收集整理的CentOS 7.2 Ubuntu 18部署Rsync + Lsyncd服务实现文件实时同步/备份的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一键备份pe版怎么用 如何使用一键备份P
- 下一篇: Ubuntu下搭建Kubernetes集