博文目錄
一、rsync概述
1、rsync命令的基本用法
二、配置rsync
1、配置同步身份驗證的rsync
2、rsync定期同步
3、配置inotify+rsync實時同步目前成都創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設(shè)計、華龍網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
rsync(Remote Sync,遠(yuǎn)程同步)是一個開源的快速備份工具,可以在不同主機之間鏡像同步整個目錄樹,支持增量備份,保持鏈接和權(quán)限,且采用優(yōu)化的同步算法,傳輸前執(zhí)行壓縮,因此非常適用于異地備份、鏡像服務(wù)器等應(yīng)用。rsync的官方站點是http://rsync.samba.org/ 作為一種常用的文件備份工具,rsync往往是Linux和UNIX系統(tǒng)默認(rèn)安裝的基本組件之一。
[root@centos01 ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
在遠(yuǎn)程同步任務(wù)中,負(fù)責(zé)發(fā)起rsync同步 操作的客戶機稱為發(fā)起端,而負(fù)責(zé)響應(yīng)來自客戶機的rsync同步操作的服務(wù)器稱為同步源。在同步過程中,同步源負(fù)責(zé)提供文檔的原始位置,發(fā)起端應(yīng)對該位置具有讀取權(quán)限。rsync作為同步源時以守護進程運行,為其他客戶機提供備份源。配置rsync同步源需要建立配置文件rsync.conf,創(chuàng)建備份賬號,然后將rsync程序以守護進程(“--daemon”選項)方式運行。
絕大多數(shù)的備份程序要求指定原始位置、目標(biāo)位置,rsync命令也一樣。最簡單的rsync用法類似于cp命令。例如,可以將文件/etc/fstab、目錄/boot/grub同步備份到/opt目錄下,其中,“-r”選項表示遞歸整個目錄樹,“-l”選項用來備份鏈接文件。
備份的基本格式為“rsync [選項] 原始位置 目標(biāo)位置”,其中常用的一些命令選項如下:
- -r:遞歸模式,包含目錄及子目錄中的所有文件;
- -l:對于符號鏈接文件仍然復(fù)制為符號鏈接文件;
- -v:顯示同步過程的詳細(xì)信息;
- -a:歸檔模式,保留文件的權(quán)限、屬性等信息,等同于組合選項“-rlptgoD”;
- -z:在傳輸文件時進行壓縮;
- -p:保留文件的權(quán)限標(biāo)記;
- -t:保留文件的時間標(biāo)記;
- -g:保留文件的屬組標(biāo)記(僅超級用戶使用);
- -o:保留文件的屬主標(biāo)記(僅超級用戶使用);
- -H:保留硬連接文件;
- -A:保留ACL屬性信息;
- -D:保留設(shè)備文件及其他特殊文件;
- --delete:刪除目標(biāo)位置有而原始位置沒有的文件;
- --checksum:根據(jù)校驗和(而不是文件大小、修改時間)來決定是否跳過文件;
[root@centos01 ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf.bak <!--備份rsync主配置文件-->
[root@centos01 ~]# vim /etc/rsyncd.conf <!--編輯主配置文件-->
uid = nobody <!--管理rsync的用戶-->
gid = nobody <!--管理rsync的組-->
port 873 <!--rsync的端口號-->
pid file = /var/run/rsyncd.pid <!--rsync進程id位置-->
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 <!--同步在壓縮的文件類型-->
auth users = bob <!--驗證賬戶-->
secrest file = /etc/rsync_user.db <!--密碼數(shù)據(jù)庫-->
address = 192.168.100.10 <!--rsync服務(wù)監(jiān)聽的ip地址-->
hosts allow = 192.168.100.0/24 <!--允許192.168.100.0網(wǎng)段訪問-->
read only = yes <!--允許讀取權(quán)限-->
[root@centos01 ~]# rsync --daemon <!--啟動rsync服務(wù)-->
[root@centos01 ~]# netstat -anptu | grep rsync <!--監(jiān)聽rsync服務(wù)是否正常啟動-->
tcp 0 0 192.168.100.10:873 0.0.0.0:* LISTEN 1422/rsync
[root@centos01 ~]# kill 1422 <!--停止服務(wù)使用kill結(jié)束進程-->
[root@centos01 ~]# vim /etc/rc.d/rc.local <!--設(shè)置開機自動啟動rsync服務(wù)-->
/usr/bin/rsync --daemon <!--rsync啟動服務(wù)添加到配置文件中-->
[root@centos01 ~]# chmod +x /etc/rc.d/rc.local <!--添加執(zhí)行權(quán)限-->
[root@centos01 ~]# mkdir centos7 <!--創(chuàng)建centos7目錄-->
[root@centos01 ~]# rsync -alv /mnt/* ./centos7/
<!--將mnt目錄下的文件復(fù)制到centos7目錄里-->
[root@centos01 ~]# mkdir benet <!--創(chuàng)建目錄-->
[root@centos01 ~]# mkdir xsh <!--創(chuàng)建目錄-->
[root@centos01 ~]# echo "11111" > ./benet/1.txt <!--寫入數(shù)據(jù)-->
[root@centos01 ~]# echo "22222" > ./xsh/2.txt <!--寫入數(shù)據(jù)-->
[root@centos01 ~]# rsync -av --delete ./benet/ ./xsh
<!--將源benet目錄中數(shù)據(jù)同步到目錄xsh目錄,刪除xsh目錄中的歷史數(shù)據(jù)-->
sending incremental file list
./
deleting 2.txt
1.txt
sent 92 bytes received 34 bytes 252.00 bytes/sec
total size is 6 speedup is 0.05
[root@centos01 ~]# cd xsh <!--進入xsh目錄-->
[root@centos01 xsh]# ls <!--查看是否同步成功-->
1.txt
[root@centos01 ~]# rsync -av ./xsh/ root@192.168.100.20:/
<!--將本地xsh目錄中的數(shù)據(jù),同步到遠(yuǎn)程主機192.168.100.20的根目錄中-->
The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes-->
Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.
root@192.168.100.20's password: <!--輸入密碼-->
sending incremental file list
./
1.txt
sent 92 bytes received 34 bytes 19.38 bytes/sec
total size is 6 speedup is 0.05
[root@centos02 ~]# cd / <!--進入根目錄-->
[root@centos02 /]# ls <!--查看目錄下文件-->
1.txt boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
[root@centos01 ~]# vim /etc/rsync_user.db
<!--創(chuàng)建rsync驗證數(shù)據(jù)庫,賬戶是bob,密碼是pwd@123-->
bob:pwd@123
[root@centos01 ~]# chmod 600 /etc/rsync_user.db <!--驗證數(shù)據(jù)庫文件添加600權(quán)限-->
[root@centos01 ~]# vim /etc/rsyncd.conf <!--修改rsync主配置文件創(chuàng)建共享-->
[accp] <!--同步共享模塊名字-->
path = /accp <!--同步物理目錄-->
comment = test <!--描述-->
auth users bob <!--驗證賬戶-->
secrets file = /etc/rsync_user.db <!--驗證的數(shù)據(jù)庫-->
read only = yes <!--允許只讀權(quán)限-->
[root@centos01 ~]# mkdir /accp <!--創(chuàng)建同步物理目錄-->
[root@centos01 ~]# echo "accp.com" > /accp/qq.txt <!--寫入測試數(shù)據(jù)-->
[root@centos01 ~]# rsync -av bob@192.168.100.10::accp ./xsh/
<!--客戶端同步數(shù)據(jù),將遠(yuǎn)程服務(wù)器192.168.100.10的accp數(shù)據(jù)同步到當(dāng)前位置的xsh目錄-->
receiving incremental file list
./
aa.txt
sent 48 bytes received 118 bytes 332.00 bytes/sec
total size is 4 speedup is 0.02
[root@centos01 ~]# rsync -av rsync://bob@192.168.100.10/accp ./xsh/ <!--第二種方式同步-->
receiving incremental file list
./
aa.txt
sent 48 bytes received 118 bytes 332.00 bytes/sec
total size is 4 speedup is 0.02
[root@centos01 ~]# cd xsh/ <!--驗證是否同步成功-->
[root@centos01 xsh]# ls
aa.txt
[root@centos01 ~]# mount /dev/cdrom /mnt/ <!--切換Linux光盤安裝inotify-->
mount: /dev/sr0 寫保護,將以只讀方式掛載
[root@centos01 ~]# tar zxvf /mnt/inotify-tools-3.14.tar.gz -C /usr/src/ <!--解壓縮inotify-->
[root@centos01 ~]# cd /usr/src/inotify-tools-3.14/ <!--進入inotify目錄-->
[root@centos01 inotify-tools-3.14]# ./configure <!--配置inotify-->
[root@centos01 inotify-tools-3.14]# make && make install <!--編譯安裝inotify-->
[root@centos01 ~]# cat /proc/sys/fs/inotify/max_queued_events
<!--查看inotify監(jiān)控事件隊列-->
16384
[root@centos01 ~]# cat /proc/sys/fs/inotify/max_user_instances <!--查看最多監(jiān)控實例數(shù)-->
128
[root@centos01 ~]# cat /proc/sys/fs/inotify/max_user_watches <!--查看每個實例最多監(jiān)控文件數(shù)-->
8192
[root@centos01 ~]# vim /etc/sysctl.conf <!--修改inotify配置文件,加大三個參數(shù)的值-->
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@centos01 ~]# sysctl -p <!--更新內(nèi)核參數(shù)-->
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@centos01 ~]# inotifywait -mrq -e modify,create,delete,move,attrib /
accp/
<!--配置一次性監(jiān)控,監(jiān)控/accp目錄發(fā)生的變化-->
[root@centos01 ~]# cd /accp/
<!--進入accp目錄,修改、刪除、創(chuàng)建數(shù)據(jù)測試是否監(jiān)控成功-->
[root@centos01 accp]# ls
aa.txt
[root@centos01 accp]# touch 11.txt
[root@centos01 accp]# echo "111" > 1.txt
[root@centos01 accp]# rm -rf 11.txt
[root@centos01 ~]# inotifywait -mrq -e modify,create,delete,move,attrib /
accp/
<!--查看監(jiān)控狀態(tài)-->
/accp/ CREATE 11.txt
/accp/ ATTRIB 11.txt
/accp/ CREATE 1.txt
/accp/ MODIFY 1.txt
/accp/ DELETE 11.txt
[root@centos01 ~]# vim rsync.sh <!--創(chuàng)建編寫實時同步腳本-->
#!/bin/bash
INW="inotifywait -mrq -e modify,create,delete,move,attrib /accp/"
RSYNC="rsync -avzH /accp/ root@192.168.100.20:/baidu/ --delete"
$INW | while read DIRECTORY EVENT FILE;do
$RSYNC &> /dev/null
done
[root@centos01 ~]# chmod +x rsync.sh <!--腳本添加執(zhí)行權(quán)限-->
[root@centos01 ~]# ssh-keygen -t rsa <!--配置密鑰對-->
[root@centos01 ~]# ssh-copy-id -i ./.ssh/id_rsa.pub root@192.168.100.20
<!--上傳ssh客戶端的公鑰到ssh服務(wù)器端-->
[root@centos01 ~]# netstat -anptu | grep rsync <!--停止rsync服務(wù)重新啟動-->
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7657
tcp6 0 0 :::873 :::* LISTEN 765
[root@centos01 ~]# kill 7657 <!--停止rsync服務(wù)-->
[root@centos01 ~]# rsync --daemon <!--重新啟動-->
[root@centos02 ~]# mkdir baidu <!--服務(wù)器端創(chuàng)建baidu目錄-->
[root@centos02 ~]# cd baidu/ <!--進入baidu目錄-->
[root@centos02 baidu]# echo "111" > 333.txt <!--插入數(shù)據(jù)-->
[root@centos02 baidu]# ls <!--查看-->
333.txt
[root@centos01 ~]# ./rsync.sh & <!--執(zhí)行腳本-->
[3] 11160
[root@centos02 ~]# cd /baidu/
<!--服務(wù)器端查看baidu目錄是否刪除歷史數(shù)據(jù)插入客戶端accp目錄下的數(shù)據(jù)-->
[root@centos02 baidu]# ls
w.txt
[root@centos01 ~]# vim /etc/rc.d/rc.local
<!--將rsync實時同步的腳本添加到開機自動啟動配置文件中-->
/root/rsync.sh & <!--執(zhí)行腳本的路徑添加進來-->
[root@centos02 ~]# kill 7984 <!--停止rsync服務(wù)-->
[root@centos02 ~]# rsync --daemon <!--重新啟動-->
文章名稱:Centos配置rsync遠(yuǎn)程同步及使用inotify+rsync實時備份
URL分享:http://aaarwkj.com/article6/gghsog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、App開發(fā)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)