網站首頁 編程語言 正文
服務器環境
- 服務端:172.16.57.26 centos6.7 rsync-server 接收文件
- 客戶端:172.16.57.25 centos6.7 sersync+rsync-client 發送文件
服務端rsync
安裝
# 首先確認軟件是否安裝:
rpm -qa rsync
# 未安裝
yum install rsync
編輯配置文件 /etc/rsyncd.conf
vim /etc/rsyncd.conf
##全局配置
uid = rsync #用戶
gid = rsync #用戶組
#監聽端口
port = 873
use chroot = no #安全相關
max connections = 200 #最大鏈接數
timeout = 300 #超時時間
pid file = /var/run/rsyncd.pid #進程對應的進程號文件
lock file = /var/run/rsync.lock #鎖文件
log file = /var/log/rsyncd.log #日志文件,顯示出錯信息
##模塊配置
[backup] #模塊名稱
path = /home/a_dest #模塊對應的位置(路徑)
ignore errors #忽略錯誤程序
read only = false #是否只讀
list = false #是否可以列表
hosts allow = 172.16.57.0/24 #準許訪問rsync服務器的客戶范圍
hosts deny = 0.0.0.0/32 #禁止訪問rsync服務器的客戶范圍
auth users = rsync_backup #不存在的用戶;只用于認證
secrets file = /etc/rsync.password #設置進行連接認證的密匙文件
path = /data 同步的目的路徑
auth users = rsync_user1 rsync的用戶名為rsync_user1
secrets file = /etc/rsync.password 存儲該用戶的密碼文件為 /etc/rsync.password.
執行配置文件必要準備
# 同步的目的路徑
mkdir -p /home/a_dest
# 添加用戶
useradd -M -s /sbin/nologin rsync
# 驗證
cat /etc/passwd | grep rsync
cat /etc/group | grep rsync
# 啟動rsync服務
rsync --daemon
# 驗證
netstat -antup | grep rsync
# 更改同步目的路徑的擁有者
chown -R rsync /home/a_dest
chgrp -R rsync /home/a_dest
# 驗證
ls -ld /home/a_dest
# 添加rsync程序的賬戶名和密碼
echo "rsync_user1:123456" >/etc/rsync.password
chmod 600 /etc/rsync.password #必須為600,否則失敗
# 設置開機啟動柜
echo "rsync --daemon" >> /etc/rc.local
如何重啟rsync服務?
pkill rsync #關閉rsync服務
rsync --daemon #啟動rsync服務
客戶端
# 安裝
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同步命令
官網地址:https://lsyncd.github.io/lsyncd/
Lsyncd 旨在將緩慢變化的本地目錄樹同步到遠程鏡像。Lsyncd 對于將數據從安全區域同步到不太安全的區域特別有用
Lysncd 不會妨礙本地文件系統性能,可以通過配置文件實現細粒度的自定義
Lysncd 實際上是 Lua 語言封裝了 inotify 和 rsync 工具
安裝lsyncd
yum install lsyncd -y
配置lsyncd
vim /etc/lsyncd.conf
# logfile 定義日志文件
# stausFile 定義狀態文件
# statusInterval 將lsyncd的狀態寫入上面的statusFile的間隔,默認10秒
# nodaemon=true 表示不啟用守護模式,默認
# inotifyMode 指定inotify監控的事件,默認是CloseWrite,還可以是Modify或CloseWrite or Modify
# maxProcesses 同步進程的最大個數。假如同時有20個文件需要同步,而maxProcesses = 8,則最大能看到有8個rysnc進程
# maxDelays 累計到多少所監控的事件激活一次同步,即使后面的delay延遲時間還未到
# sync定義同步參數
# default.rsync 本地目錄之間同步,使用rsync,也可以達到使用ssh形式的遠程rsync效果,或daemon方式連接遠程rsyncd進程;
# default.direct 本地目錄之間同步,使用cp、rm等命令完成差異文件備份;
# default.rsyncssh 同步到遠程主機目錄,rsync的ssh模式,需要使用key來認證
# 目錄設置
# source 同步的源目錄,使用絕對路徑。
# target 定義目的地址.對應不同的模式有幾種寫法:
# /tmp/dest 本地目錄同步,可用于direct和rsync模式
# 172.29.88.223:/tmp/dest 同步到遠程服務器目錄,可用于rsync和rsyncssh模式
# excludeFrom 排除選項,后面指定排除的列表文件,如excludeFrom = "/etc/lsyncd.exclude",如果是簡單的排除,可以使用exclude = LIST。
# delete 參數為了保持target與souce完全同步,Lsyncd默認會delete = true來允許同步刪除。它除了false,還有startup、running
# true Lsyncd將在目標上刪除任何不在源中的內容。 在啟動時和正常操作中被刪除的內容。
#false 在lsyncd啟動后將在目標上不刪除任何不在源中的內容, 在啟動時和正常操作中被刪除的內容。
#startup 啟動時將執行一次完全文件同步,保證完全一致;正常運行過程中不會刪除target中的文件
#running 啟動前,增加的會同步,刪除的不同步;正常運行過程中會刪除target中的文件
# rsync選項
# bwlimit 限速,單位kb/s,與rsync相同(這么重要的選項在文檔里竟然沒有標出)
# compress 壓縮傳輸默認為true。在帶寬與cpu負載之間權衡,本地目錄同步可以考慮把它設為false
# perms 默認保留文件權限。
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. 遠程目錄同步,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. 遠程目錄同步,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,注意如果是普通用戶,必須擁有寫權限
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. 遠程目錄同步,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"/> #是否開啟調試模式
<fileSystem xfs="false"/> #監控的是否為xfs文件系統
<filter start="false"> #是否開啟文件類型過濾,開啟后如下篩選的文件類型將不監控
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> #監控的事件,默認監控的是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"> #監控的數據目錄,這里是confluence的附件文件目錄
<remote ip="172.16.20.10" name="backup"/> #備機的ip地址和rsync daemon模塊名稱,所以備機上需要已daemon模式運行rsync
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avz"/> #rsync的參數
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> #開啟rsync的認證模式
<userDefinedPort start="true" port="8787"/> #指定備機上rsync監聽的端口號
<timeout start="true" time="100"/> #開啟認證超時時間
<ssh start="false"/> #安全起見,不建議使用ssh的方式認證
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #傳輸失敗時會重新傳輸,再次失敗會寫入rsync_fail_log中,每隔一段時間(timeToExecute)執行腳本再次傳輸
<crontab start="true" schedule="600"><!--600mins--> #對監控目錄與目標服務器每隔一段時間進行一次整體同步,默認600分鐘,根據個人情況是否開啟
<crontabfilter start="false"> #如果之前開啟了文件過濾,這里也要設置過濾
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command"> #下面就是插件的設置(不做過多說明)
<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 啟用線程數量
# -d daemon方式啟動
# -o 指定配置文件
sersync -r #可以全量同步監控的目錄
原文鏈接:https://blog.csdn.net/qq_23564667/article/details/132081364
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-11-26 Ubuntu?環境下安裝?Docker環境詳解_docker
- 2023-01-20 pandas中df.groupby()方法深入講解_python
- 2022-05-18 解決iOS驗證碼顯示在左邊問題_IOS
- 2022-04-19 在html中src和href的區別,以及img中的srcset的作用是什么?
- 2021-12-22 Docker部署Microsoft?Sql?Server詳細步驟_docker
- 2022-04-25 一篇文章帶你了解C語言的文件操作_C 語言
- 2023-06-04 Flask框架中的session設置詳解_python
- 2022-05-04 詳解Python使用apscheduler定時執行任務_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支