Rsync+sersync實現數據實時同步(小白的“升級打怪”成長之路)

目錄

一、rsync部署

push推數據

1、編寫rsync配置文件

2、備份測試

3、檢驗結果

二、rsync+sersync 實現數據實時同步

1、安裝sersync服務

2、檢驗結果

pull拉取數據

1、編寫rsync配置文件

2、檢驗結果

三、腳本編寫

1、客戶端腳本編寫

2、服務器腳本編寫


一、rsync部署

  • 服務器:source 192.168.58.130 (進行數據備份的主機)

  • 客戶端:targetpc 192.168.58.131 (備份存儲的主機)

push推數據

1、編寫rsync配置文件

#192.168.58.131
# 在客戶端上編寫rsync配置文件,創建一個存放備份的同步目錄
[root@targetpc ~]# vim /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress ? = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# ? ? ?  path = /home/ftp
# ? ? ?  comment = ftp export area
?
?
#在這些內容后面加上下面的內容
#加上
port=873
address = 192.168.58.131
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.58.0/24
[data]
path = /data/backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
#192.168.58.131
# 創建密碼文件,格式 ? 用戶名:密碼
# 設置密碼文件權限為600或者700
[root@targetpc ~]# touch /etc/rsync.passwd
[root@targetpc ~]# vim /etc/rsync.passwd 
rsyncuser:123456
[root@targetpc ~]# chmod 600 /etc/rsync.passwd
#192.168.58.131
# 創建歡迎信息文件
[root@targetpc ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd
?
[root@targetpc ~]# systemctl start rsyncd
[root@targetpc ~]# mkdir -p /data/backup
[root@targetpc ~]# cd /data/backup

2、備份測試

#192.168.58.130
[root@source ~]# mkdir /data
[root@source ~]# cd /data
[root@source data]# touch 1.txt
[root@source data]# mkdir aaa
[root@source data]# rsync -avz /data/* rsyncuser@192.168.58.131::data
Welcome to Backup Server
?
Password: 
sending incremental file list
1.txt
aaa/
?
sent 124 bytes  received 43 bytes  9.03 bytes/sec
total size is 0  speedup is 0.00
[root@source data]# touch 2.txt
[root@source data]# ls
1.txt  2.txt  aaa
[root@source data]# rsync -avz /data/* rsyncuser@192.168.58.131::data
Welcome to Backup Server
?
Password: 
sending incremental file list
2.txt
?
sent 137 bytes  received 36 bytes  9.89 bytes/sec
total size is 0  speedup is 0.00

3、檢驗結果

#192.168.58.131
[root@targetpc backup]# ls
1.txt  2.txt  aaa
?
?
[root@targetpc backup]# touch 3.txt
[root@targetpc backup]# ls
1.txt  2.txt  3.txt  aaa

#192.168.58.130
[root@source data]# rsync -avz --delete /data/ rsyncuser@192.168.58.131::data
Welcome to Backup Server
?
Password: 
sending incremental file list
deleting 3.txt
?
sent 125 bytes  received 22 bytes  8.91 bytes/sec
total size is 0  speedup is 0.00

#192.168.58.131
[root@targetpc backup]# ls
1.txt  2.txt  aaa

非交互式:

如果想不輸入密碼,可以使用--password-file參數指定密碼

#192.168.58.130
[root@source data]# vim /etc/rsync.passwd
123456
[root@source data]# chmod 600 /etc/rsync.passwd
[root@source data]#  rsync -avz --delete /data/ rsyncuser@192.168.58.131::data --password-file=/etc/rsync.passwd
Welcome to Backup Server
?
sending incremental file list
deleting data/aaa/
deleting data/2.txt
deleting data/1.txt
deleting data/
./
?
sent 132 bytes  received 70 bytes  13.93 bytes/sec
total size is 0  speedup is 0.00

#192.168.58.131
[root@targetpc backup]# ls
1.txt  2.txt  aaa

二、rsync+sersync 實現數據實時同步

1、安裝sersync服務

#192.168.58.130
[root@source data]# cd
[root@source ~]# rz
rz waiting to receive.**[root@source ~]# ls
anaconda-ks.cfg  ceph-release-1-1.el7.noarch.rpm  init.sh  sersync2.5.4_64bit_binary_stable_final.tar.gz  shell
?
[root@source ~]# ls
anaconda-ks.cfg  ceph-release-1-1.el7.noarch.rpm  init.sh  sersync2.5.4_64bit_binary_stable_final.tar.gz  shell
[root@source ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz 
[root@source ~]# ls
anaconda-ks.cfg ? ? ? ? ? ? ? ?  GNU-Linux-x86  sersync2.5.4_64bit_binary_stable_final.tar.gz
ceph-release-1-1.el7.noarch.rpm  init.sh ? ? ?  shell
[root@source ~]# cd GNU-Linux-x86/
[root@source GNU-Linux-x86]# ls
confxml.xml  sersync2
[root@source GNU-Linux-x86]# vim confxml.xml 
<inotify><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><localpath watch="/opt/tongbu"><remote ip="127.0.0.1" name="tongbu1"/><!--<remote ip="192.168.8.39" name="tongbu"/>--><!--<remote ip="192.168.8.40" name="tongbu"/>--></localpath><rsync><commonParams params="-artuz"/><auth start="false" users="root" passwordfile="/etc/rsync.pas"/><userDefinedPort start="false" port="874"/><!-- port=874 --><timeout start="false" time="100"/><!-- timeout=100 --><ssh start="false"/></rsync>
<inotify><delete start="true"/><createFolder start="true"/><createFile start="true"/><closeWrite start="true"/><moveFrom start="true"/><moveTo start="true"/><attrib start="true"/><modify start="true"/></inotify><sersync><localpath watch="/data"><remote ip="192.168.58.131" name="data"/><!--<remote ip="192.168.8.39" name="tongbu"/>--><!--<remote ip="192.168.8.40" name="tongbu"/>--></localpath><rsync><commonParams params="-artuz"/><auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/><userDefinedPort start="false" port="874"/><!-- port=874 --><timeout start="false" time="100"/><!-- timeout=100 --><ssh start="false"/></rsync>
#192.168.58.130
# 開啟sersync守護進程同步數據,即實時同步。
# -r參數,先做一次完全同步,再做差異同步。
[root@source GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml.xml 
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d ? ?  run as a daemon
option: -r ? ?  rsync all the local files to the remote servers before the sersync work
option: -o ? ?  config xml name:  ./confxml.xml
daemon thread num: 10
parse xml config file 
host ip : localhost ? ? host port: 8008
will ignore the inotify createFile event 
daemon start,sersync run behind the console 
use rsync password-file :
user is rsyncuser
passwordfile is ? ? ? ? /etc/rsync.passwd
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./ rsyncuser@192.168.58.131::data --password-file=/etc/rsync.passwd >/dev/null 2>&1
?
#或者
[root@source GNU-Linux-x86]# nohup ./sersync2 -r -o ./confxml.xml &
[1] 117240

2、檢驗結果

#192.168.58.130
#更改名稱,查看變化
[root@source data]# mv aaa AAA
root@source data]# ls
1.txt  2.txt  AAA
#192.168.58.131
#檢測到名稱已變
[root@targetpc backup]# ls
1.txt  2.txt  AAA

pull拉取數據

以上的同步過程都是服務端主動推送數據給目標主機(服務器),這里演示下目標主機主動拉取數據進行同步。

1、編寫rsync配置文件

#192.168.58.130
[root@source ~]# vim /etc/rsyncd.conf
#在這個文件里加入下面內容#
ort=873
address = 192.168.58.130
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.58.0/24
[data]
path = /data
comment = master data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
?
# 認證文件
[root@source ~]# vim /etc/rsync.passwd
rsyncuser:123456
# 服務端上啟動rsync
[root@source ~]# systemctl start rsyncd
[root@source ~]# netstat -antup | grep 873
tcp ? ? ?  0 ? ?  0 192.168.58.130:873 ? ?  0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ?  15646/rsync ? ? 
# 看到以上結果,說明服務端的rsync服務已經配置完成。
# 開放873端口號,允許通過873端口號拉取數據
[root@source ~]# cd /data
[root@source data]# mkdir BBB
[root@source data]# ls
1.txt  2.txt  AAA  BBB

2、檢驗結果

#192.168.58.131
# 目標主機上拉取數據
[root@targetpc backup]#  rsync -avz rsyncuser@192.168.58.130::data /data/backup/
?
Password: 
receiving incremental file list
./
BBB/
?
sent 32 bytes  received 145 bytes  9.57 bytes/sec
total size is 0  speedup is 0.00
[root@targetpc backup]# ls
1.txt  2.txt  AAA  BBB

三、腳本編寫

計劃將服務器的/data目錄的文件實時發送到客戶端/data/backup目錄中,操作前請檢查服務器是否有該目錄

1、客戶端腳本編寫

#!/bin/bash
#客戶端配置
#編寫rsync配置文件
read -p "請輸入符合此掩碼的當前主機IP(默認掩碼為255.255.255.0):" ip
ip1=$(echo $ip | awk -F. '{print $1"."$2"."$3}')
echo "
port=873
address = $ip
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = $ip1.0/24
[data]
path = /data/backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
" > /etc/rsyncd.conf
touch /etc/rsync.passwd
echo "rsyncuser:123456" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
echo "Welcome to Backup Server" > /etc/rsyncd.motd
systemctl restart rsyncd
mkdir -p /data/backup

2、服務器腳本編寫

#!/bin/bash
#服務端配置
read -p "請輸入符合此掩碼的客戶端主機IP(默認掩碼為255.255.255.0):" ip
echo "123456" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
rsync -avz --delete /data/ rsyncuser@$ip::data --password-file=/etc/rsync.passwd

看到感覺有幫助的朋友,勞煩動動發財的小手給博主點個贊

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/87509.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/87509.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/87509.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

用 python 開發一個可調用工具的 AI Agent,實現電腦配置專業評價

在人工智能時代&#xff0c;AI Agent憑借其強大的任務處理能力&#xff0c;逐漸成為開發人員手中的得力工具。今天&#xff0c;我們就來一起動手&#xff0c;用Python打造一個能夠調用工具的AI Agent&#xff0c;實現根據電腦信息對電腦配置進行專業評價的功能。 一、項目創建…

WSL 安裝使用和常用命令

參考官方使用說明&#xff1a; https://learn.microsoft.com/zh-cn/windows/wsl/ 安裝wsl: wsl --install --no-distribution --no-distribution&#xff1a;安裝 WSL 時不要安裝分發版 更新 wsl: wsl --update 設置wsl 默認版本&#xff1a; wsl --set-default-version <…

720全景VR拍攝制作實戰教程

720全景VR拍攝制作實戰教程 720全景VR拍攝制作是近年來興起的一種沉浸式影像制作技術。它通過多角度拍攝&#xff0c;并將畫面拼接成一個全景視角&#xff0c;使觀眾獲得身臨其境的觀看體驗。本教程將帶你從準備階段到拍攝階段&#xff0c;再到后期處理階段&#xff0c;一步步…

什么真正的云原生開發?如何區別本地開發后部署到云端?

以下是關于云原生開發的深度解析&#xff0c;以及與本地開發后遷移上云的本質區別&#xff1a; 一、真正的云原生開發&#xff1a;從理念到實踐的全面革新 1. 定義與核心思想 云原生開發是一種以云計算能力為核心的架構設計和開發方法論&#xff0c;其本質是讓應用從誕生之初…

從代碼學習深度學習 - 詞的相似性和類比任務 PyTorch版

提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 文章目錄 前言加載預訓練詞向量TokenEmbedding 類詳解預訓練詞向量簡介 (GloVe)具體含義總結建議應用預訓練詞向量詞相似度knn 函數get_similar_tokens 函數相似詞查找示例詞類比get_analogy 函數詞類比任務…

ubuntu 22.04 安裝部署elk(elasticsearch/logstash/kibana) 7.10.0詳細教程

安裝部署elk7.10.0詳細教程 一、安裝jdk 11環境二、安裝elasticsearch 7.10.0三、安裝kibana 7.10.0四、安裝logstash 7.10.0五、安裝ik7.10.0分詞六、開啟安全功能1. 開啟用戶名密碼登錄2. 開啟es安全加密通信3. 開啟Kibana安全功能 七、注意事項和常見錯誤八、其它操作及命令…

技術文章: 基板的吸水率

PCB基板或覆銅板的吸水率是一個重要的性能指標&#xff0c;它衡量了覆銅板在特定條件下&#xff08;通常是浸水后&#xff09;吸收水分的能力&#xff0c;通常用指定條件下吸水后與吸水前相比&#xff0c;質量增加的百分比來表示。當材料暴露扎起在潮濕空氣中或浸沒在水中時其抵…

九日集訓第三天

目錄 搜索旋轉排序數組 搜索旋轉排序數組|| 尋找旋轉排序中的數組最小值 爬樓梯 斐波那契數 第N個泰波那契數 差的絕對值為K的數對數目 猜數字 拿硬幣 山峰數組的峰頂索引 搜索旋轉排序數組 class Solution { public:int search(vector<int>& nums, int t…

CppCon 2017 學習:folly::Function A Non-copyable Alternative to std::function

你說的內容是關于 C 中 可調用類型&#xff08;Callable Types&#xff09; 的基礎知識&#xff0c;我幫你理清并補充理解。 Callable Types&#xff08;可調用類型&#xff09;簡介 C 中任何可以用 () 括號操作符“調用”的對象&#xff0c;都叫做 可調用類型。典型包括&…

PyTorch 中Tensor常用數據結構(int, list, numpy array等)互相轉換和實戰示例

在 PyTorch 中&#xff0c;tensor 是一種強大且靈活的數據結構&#xff0c;可以與多種 Python 常用數據結構&#xff08;如 int, list, numpy array 等&#xff09;互相轉換。下面是詳細解釋和代碼示例&#xff1a; 1. Tensor ? int / float 轉為 int / float&#xff08;前提…

計算機網絡與數據通信基礎

第一章 計算機網絡概述 1. 計算機網絡的核心概念 1.1 定義 將 地理分散 的、具有 獨立處理能力 的計算機系統&#xff08;主機/Host&#xff09;&#xff0c;通過 傳輸介質 與 網絡設備 互連&#xff0c;在 網絡協議 和 軟件 支持下實現 資源共享 與 數據通信 的系統。 關鍵術…

【統計術語】

文章目錄 基礎概念術語基期與現期增長量與增長率環比與同比 比重術語平均數術語特殊增長術語其他常用術語 基礎概念術語 基期與現期 基期&#xff1a;作為基礎參照的時期&#xff0c;一般指過去的時間 現期&#xff1a;與基期對比的時期&#xff0c;一般指現在的時間 示例&am…

XXE(XML外部實體注入)詳解

目錄 一、XXE漏洞簡介 二、XML詳解 (一) XML文檔結構 1. 文檔聲明 2. XML文檔類型定義&#xff08;DTD&#xff09; 3. XML文檔元素 4. XML文檔示例 三、XXE漏洞類型 四、XXE漏洞挖掘技巧 五、XXE漏洞危害 (一) 文件讀取 (二) 內網探測 1. 端口探測 2. 主機存活探…

深入解析JVM字節碼執行引擎

JVM 字節碼執行引擎。它是 JVM 核心組件之一&#xff0c;負責實際執行加載到內存中的字節碼指令。你可以將它想象成 JVM 的“CPU”。 核心職責&#xff1a; 加載待執行的字節碼&#xff1a; 從方法區&#xff08;元空間&#xff09;獲取已加載類的方法字節碼。創建和管理棧幀…

華為OD機試-MELON的難題-DFS(JAVA 2025A卷)

題意是從N快雨花石中找出最少拿出雨花石的塊數&#xff0c;使得雨花石可以均分&#xff0c;直接使用dfs解決此類組合問題 package com.example.demo.bean;import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner;public class YuHuaStone {public s…

鴻蒙數據庫操作

一、使用關系型數據庫實現數據持久化&#xff0c;需要獲取一個RdbStore&#xff0c;其中包括建庫、建表、升降級等操作。 const STORE_CONFIG: relationalStore.StoreConfig {name: AnyOffice.db, // 數據庫文件名securityLevel: relationalStore.SecurityLevel.S1, // 數據庫…

基于ARM SoC的半導體測試

ARM SoC&#xff08;System on Chip&#xff09; 是一種集成了多個關鍵計算組件的單片系統芯片&#xff0c;廣泛應用于移動設備、嵌入式系統、物聯網&#xff08;IoT&#xff09;和半導體測試設備等領域。它的核心設計理念是“高度集成”&#xff0c;將處理器、內存、外設接口等…

JavaEE->多線程2

目錄 一、線程安全&#xff08;重點&#xff09; 1.線程安全演示 2.線程不安全的原因 1.線程是搶占式執行的&#xff08;執行順序是隨機的&#xff09; 2.多個線程同時修改了同一個變量 3.原子性 4.內存可見性 5.指令重排序&#xff08;有序性&#xff09; 二、解決線…

Flutter TCP通信

啟動TCP服務 Future<void> startServer() async {final server await ServerSocket.bind(InternetAddress.anyIPv4, 12345);print(Server listening on ${server.address}:${server.port});server.listen((Socket socket) {print(Client connected: ${socket.remoteAddr…

flask拆分計劃

兩個啟動鏈接&#xff0c;看日志提示是因為2次啟動&#xff0c;一次是database&#xff0c;一次是xmind2&#xff0c;去掉一次就可以&#xff0c;如何去掉一次&#xff1f; 這里啟動也調用了一次&#xff0c;所以測試環境注釋掉&#xff0c;如下圖&#xff0c;也就調用了一次