網(wǎng)站首頁 編程語言 正文
linux服務(wù)器使用rsync 和 inotify或者sersync 實(shí)現(xiàn)服務(wù)器之間文件實(shí)時同步
作者:小吳-斌 更新時間: 2023-08-30 編程語言服務(wù)器環(huán)境
- 服務(wù)端:172.16.57.26 centos6.7 rsync-server 接收文件
- 客戶端:172.16.57.25 centos6.7 sersync+rsync-client 發(fā)送文件
服務(wù)端rsync
安裝
# 首先確認(rèn)軟件是否安裝:
rpm -qa rsync
# 未安裝
yum install rsync
編輯配置文件 /etc/rsyncd.conf
vim /etc/rsyncd.conf
##全局配置
uid = rsync #用戶
gid = rsync #用戶組
#監(jiān)聽端口
port = 873
use chroot = no #安全相關(guān)
max connections = 200 #最大鏈接數(shù)
timeout = 300 #超時時間
pid file = /var/run/rsyncd.pid #進(jìn)程對應(yīng)的進(jìn)程號文件
lock file = /var/run/rsync.lock #鎖文件
log file = /var/log/rsyncd.log #日志文件,顯示出錯信息
##模塊配置
[backup] #模塊名稱
path = /home/a_dest #模塊對應(yīng)的位置(路徑)
ignore errors #忽略錯誤程序
read only = false #是否只讀
list = false #是否可以列表
hosts allow = 172.16.57.0/24 #準(zhǔn)許訪問rsync服務(wù)器的客戶范圍
hosts deny = 0.0.0.0/32 #禁止訪問rsync服務(wù)器的客戶范圍
auth users = rsync_backup #不存在的用戶;只用于認(rèn)證
secrets file = /etc/rsync.password #設(shè)置進(jìn)行連接認(rèn)證的密匙文件
path = /data 同步的目的路徑
auth users = rsync_user1 rsync的用戶名為rsync_user1
secrets file = /etc/rsync.password 存儲該用戶的密碼文件為 /etc/rsync.password.
執(zhí)行配置文件必要準(zhǔn)備
# 同步的目的路徑
mkdir -p /home/a_dest
# 添加用戶
useradd -M -s /sbin/nologin rsync
# 驗(yàn)證
cat /etc/passwd | grep rsync
cat /etc/group | grep rsync
# 啟動rsync服務(wù)
rsync --daemon
# 驗(yàn)證
netstat -antup | grep rsync
# 更改同步目的路徑的擁有者
chown -R rsync /home/a_dest
chgrp -R rsync /home/a_dest
# 驗(yàn)證
ls -ld /home/a_dest
# 添加rsync程序的賬戶名和密碼
echo "rsync_user1:123456" >/etc/rsync.password
chmod 600 /etc/rsync.password #必須為600,否則失敗
# 設(shè)置開機(jī)啟動柜
echo "rsync --daemon" >> /etc/rc.local
如何重啟rsync服務(wù)?
pkill rsync #關(guān)閉rsync服務(wù)
rsync --daemon #啟動rsync服務(wù)
客戶端
# 安裝
yum install rsync
# 配置rsync的密碼文件
echo "123456" > /etc/rsync.password
chmod 600 /etc/rsync.password #必須為600,否則失敗
示例
# 推送
rsync -avz --delete /home/b_source/ rsync_user1@192.168.197.129::backup --password-file=/etc/rsync.password
# 拉取
rsync -avzP rsync_user1@192.168.197.129::backup /home/b_source/ --password-file=/etc/rsync.password
同步失敗
# 開放873端口
iptables -A INPUT -p tcp --dport 873 -j ACCEPT
lsyncd同步命令
官網(wǎng)地址:https://lsyncd.github.io/lsyncd/
Lsyncd 旨在將緩慢變化的本地目錄樹同步到遠(yuǎn)程鏡像。Lsyncd 對于將數(shù)據(jù)從安全區(qū)域同步到不太安全的區(qū)域特別有用
Lysncd 不會妨礙本地文件系統(tǒng)性能,可以通過配置文件實(shí)現(xiàn)細(xì)粒度的自定義
Lysncd 實(shí)際上是 Lua 語言封裝了 inotify 和 rsync 工具
安裝lsyncd
yum install lsyncd -y
配置lsyncd
vim /etc/lsyncd.conf
# logfile 定義日志文件
# stausFile 定義狀態(tài)文件
# statusInterval 將lsyncd的狀態(tài)寫入上面的statusFile的間隔,默認(rèn)10秒
# nodaemon=true 表示不啟用守護(hù)模式,默認(rèn)
# inotifyMode 指定inotify監(jiān)控的事件,默認(rèn)是CloseWrite,還可以是Modify或CloseWrite or Modify
# maxProcesses 同步進(jìn)程的最大個數(shù)。假如同時有20個文件需要同步,而maxProcesses = 8,則最大能看到有8個rysnc進(jìn)程
# maxDelays 累計到多少所監(jiān)控的事件激活一次同步,即使后面的delay延遲時間還未到
# sync定義同步參數(shù)
# default.rsync 本地目錄之間同步,使用rsync,也可以達(dá)到使用ssh形式的遠(yuǎn)程rsync效果,或daemon方式連接遠(yuǎn)程rsyncd進(jìn)程;
# default.direct 本地目錄之間同步,使用cp、rm等命令完成差異文件備份;
# default.rsyncssh 同步到遠(yuǎn)程主機(jī)目錄,rsync的ssh模式,需要使用key來認(rèn)證
# 目錄設(shè)置
# source 同步的源目錄,使用絕對路徑。
# target 定義目的地址.對應(yīng)不同的模式有幾種寫法:
# /tmp/dest 本地目錄同步,可用于direct和rsync模式
# 172.29.88.223:/tmp/dest 同步到遠(yuǎn)程服務(wù)器目錄,可用于rsync和rsyncssh模式
# excludeFrom 排除選項(xiàng),后面指定排除的列表文件,如excludeFrom = "/etc/lsyncd.exclude",如果是簡單的排除,可以使用exclude = LIST。
# delete 參數(shù)為了保持target與souce完全同步,Lsyncd默認(rèn)會delete = true來允許同步刪除。它除了false,還有startup、running
# true Lsyncd將在目標(biāo)上刪除任何不在源中的內(nèi)容。 在啟動時和正常操作中被刪除的內(nèi)容。
#false 在lsyncd啟動后將在目標(biāo)上不刪除任何不在源中的內(nèi)容, 在啟動時和正常操作中被刪除的內(nèi)容。
#startup 啟動時將執(zhí)行一次完全文件同步,保證完全一致;正常運(yùn)行過程中不會刪除target中的文件
#running 啟動前,增加的會同步,刪除的不同步;正常運(yùn)行過程中會刪除target中的文件
# rsync選項(xiàng)
# bwlimit 限速,單位kb/s,與rsync相同(這么重要的選項(xiàng)在文檔里竟然沒有標(biāo)出)
# compress 壓縮傳輸默認(rèn)為true。在帶寬與cpu負(fù)載之間權(quán)衡,本地目錄同步可以考慮把它設(shè)為false
# perms 默認(rèn)保留文件權(quán)限。
settings {
logfile ="/var/log/lsyncd/lsyncd.log",
statusFile ="/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/data",
target = "rsync_backup@172.16.57.26::backup",
excludeFrom="/etc/lsyncd_exclude.lst",
delete = false,
delay=1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
--delete = true,
password_file="/etc/rsync.passwd",
_extra={"--bwlimit=200,--password-file=/etc/rsync.passwd"}
}
}
lsyncd.conf可以有多個sync,各自的source,各自的target,各自的模式,互不影響。
啟動lsyncd
systemctl start lsyncd
各種模式下配置示例
settings {
logfile ="/usr/local/lsyncd-2.1.5/var/lsyncd.log",
statusFile ="/usr/local/lsyncd-2.1.5/var/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
-- I. 本地目錄同步,direct:cp/rm/mv。 適用:500+萬文件,變動不大
sync {
default.direct,
source = "/tmp/src",
target = "/tmp/dest",
delay = 1
maxProcesses = 1
}
-- II. 本地目錄同步,rsync模式:rsync
sync {
default.rsync,
source = "/tmp/src",
target = "/tmp/dest1",
excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
}
}
-- III. 遠(yuǎn)程目錄同步,rsync模式 + rsyncd daemon
sync {
default.rsync,
source = "/tmp/src",
target = "syncuser@172.29.88.223::module1",
delete="running",
exclude = { ".*", ".tmp" },
delay = 30,
init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsyncd.d/rsync.pwd",
_extra = {"--bwlimit=200"}
}
}
-- IV. 遠(yuǎn)程目錄同步,rsync模式 + ssh shell
sync {
default.rsync,
source = "/tmp/src",
target = "172.29.88.223:/tmp/dest",
-- target = "root@172.29.88.223:/remote/dest",
-- 上面target,注意如果是普通用戶,必須擁有寫權(quán)限
maxDelays = 5,
delay = 30,
-- init = true,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
-- rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
-- 如果要指定其它端口,請用上面的rsh
}
}
-- V. 遠(yuǎn)程目錄同步,rsync模式 + rsyncssh,效果與上面相同
sync {
default.rsyncssh,
source = "/tmp/src2",
host = "172.29.88.223",
targetdir = "/remote/dir",
excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
-- maxDelays = 5,
delay = 0,
-- init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
_extra = {"--bwlimit=2000"},
},
ssh = {
port = 1234
}
}
sersync同步命令
wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync
echo 'export PATH=$PATH:/usr/local/sersync' >> ~/.bash_profile
source ~/.bash_profile
# 配置文件
cd /usr/local/sersync
vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/> #是否開啟調(diào)試模式
<fileSystem xfs="false"/> #監(jiān)控的是否為xfs文件系統(tǒng)
<filter start="false"> #是否開啟文件類型過濾,開啟后如下篩選的文件類型將不監(jiān)控
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> #監(jiān)控的事件,默認(rèn)監(jiān)控的是delete/close_write/moved_from/moved_to/create folder
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync> #rsync命令配置段
<localpath watch="/var/atlassian/application-data/confluence/attachments/ver003"> #監(jiān)控的數(shù)據(jù)目錄,這里是confluence的附件文件目錄
<remote ip="172.16.20.10" name="backup"/> #備機(jī)的ip地址和rsync daemon模塊名稱,所以備機(jī)上需要已daemon模式運(yùn)行rsync
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avz"/> #rsync的參數(shù)
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> #開啟rsync的認(rèn)證模式
<userDefinedPort start="true" port="8787"/> #指定備機(jī)上rsync監(jiān)聽的端口號
<timeout start="true" time="100"/> #開啟認(rèn)證超時時間
<ssh start="false"/> #安全起見,不建議使用ssh的方式認(rèn)證
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #傳輸失敗時會重新傳輸,再次失敗會寫入rsync_fail_log中,每隔一段時間(timeToExecute)執(zhí)行腳本再次傳輸
<crontab start="true" schedule="600"><!--600mins--> #對監(jiān)控目錄與目標(biāo)服務(wù)器每隔一段時間進(jìn)行一次整體同步,默認(rèn)600分鐘,根據(jù)個人情況是否開啟
<crontabfilter start="false"> #如果之前開啟了文件過濾,這里也要設(shè)置過濾
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command"> #下面就是插件的設(shè)置(不做過多說明)
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
# 啟動sersync
sersync2 -n 10 -d -o /usr/local/sersync/confxml.xml
# -n 啟用線程數(shù)量
# -d daemon方式啟動
# -o 指定配置文件
sersync -r #可以全量同步監(jiān)控的目錄
原文鏈接:https://blog.csdn.net/qq_23564667/article/details/132081364
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-09-13 conda虛擬環(huán)境默認(rèn)路徑的修改方法_python
- 2023-08-30 Git忽略已經(jīng)提交過一次文件Git忽略文件
- 2022-11-15 python列表中常見的一些排序方法_python
- 2024-01-15 Stream流 - 獲取Stream和轉(zhuǎn)換操作(含基本數(shù)據(jù)類型流)
- 2022-10-05 redis?哨兵集群搭建的實(shí)現(xiàn)_Redis
- 2022-09-30 python計算列表元素與乘積詳情_python
- 2023-01-01 利用Python腳本實(shí)現(xiàn)傳遞參數(shù)的三種方式分享_python
- 2022-09-22 nodeSelector:Pod 定向調(diào)度
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支