rsync實時備份

實驗環境redhat或者centos

Ip192.168.18.67

Ip192.168.18.167

瞭解rsync

[root@localhost~]#mkdir-pv/tmp/a

mkdir:已創建目錄/tmp/a

[root@localhost~]#mkdir-pv/tmp/b

mkdir:已創建目錄/tmp/b

[root@localhost~]#cd/tmp/

[root@localhosttmp]#cda

[root@localhosta]#touch123

[root@localhosta]#rsync/tmp/a/tmp/b

skippingdirectory/tmp/a

[root@localhosta]#rsync/tmp/a/tmp/b-r

[root@localhosta]#cd/tmp/b/

[root@localhostb]#ls

a

[root@localhostb]#cda

[root@localhosta]#ls

123

實驗演示過程

mkdir-pv/etc/rsyncd這個目錄起初是沒有的,需要手動建立

ls-l

總計0

vim/etc/rsyncd/rsyncd.conf

uid=root

gid=root

port=873

maxconnections=0#limitclientconection

logfile=/var/log/rsyncd.log

pidfile=/var/run/rsyncd.pid

lockfile=/var/run/rsyncd.lock

motdfile=/etc/rsyncd/rsyncd.motd

readonly=yes

####limituserconn######

hostsallow=192.168.18.0/255.255.255.0

hostsdeny=*

#transferlogging=yes

#logformat=%t%a%m%f%b

#syslogfacility=local3

#timeout=300

[www]

path=/var/www/html/

list=yes

ignoreerrors

authusers=zhang

###username

secretsfile=/etc/rsyncd/rsyncd.secrets

comment=wwwdirectory

exclude=a/b/

~

[root@localhostrsyncd]#mkdir-pv/var/www/html/{a,b,c}

mkdir:已創建目錄/var/www/html/a

mkdir:已創建目錄/var/www/html/b

mkdir:已創建目錄/var/www/html/c

/etc/rsyncd

vimrsyncd.motd創建廣告文件

################################

zhangzhanlan

################################

vimrsyncd.secrets

zhang:123

chmod600rsyncd.secrets

rsync--daemon--config=/etc/rsyncd/rsyncd.conf啓動服務

lsof-i:873

COMMANDPIDUSERFDTYPEDEVICESIZENODENAME

rsync5766root4uIPv620761TCP*:rsync(LISTEN)

rsync5766root5uIPv420762TCP*:rsync(LISTEN)

tail-f/var/log/rsyncd.log

2013/10/0922:41:50[5766]rsyncdversion2.6.8starting,listeningonport873

客戶端

echo123>/etc/rsync.password

chmod600/etc/rsync.password

[root@localhost~]#mkdir-pv/tmp/bak

mkdir:已創建目錄/tmp/bak

rsync-avzP--delete--password-file=/etc/[email protected]::www/tmp/bak

實驗效果測試

67touch/var/www/html/{1,2,3}創建文件及數據文件

167上執行上述的命令

rsync-avzP--delete--password-file=/etc/[email protected]::www/tmp/bak

################################

zhangzhanlan

################################

receivingfilelist...

5filestoconsider

./

1

0100%0.00kB/s0:00:00(xfer#1,to-check=3/5)

2

0100%0.00kB/s0:00:00(xfer#2,to-check=2/5)

3

0100%0.00kB/s0:00:00(xfer#3,to-check=1/5)

sent169bytesreceived360bytes1058.00bytes/sec

totalsizeis0speedupis0.00

Youhavemailin/var/spool/mail/root

進入到指定的目錄裏查看就可以查看到備份的數據及文件

要想讓167自動檢測到67上的數據集文件可以自動備份,就需要將67167的服務器和客戶端的角色互換,

首先將

167上的/tmp/bak目錄下的數據及文件清空

rm-fr./*

在查看67上的rsync的進程是否還存在,如果存在先將進程結束

[root@localhosthtml]#lsof-i:873

COMMANDPIDUSERFDTYPEDEVICESIZENODENAME

rsync5766root4uIPv620761TCP*:rsync(LISTEN)

rsync5766root5uIPv420762TCP*:rsync(LISTEN)

[root@localhosthtml]#killallrsync

167上創建rsync目錄

mkdir-pv/etc/rsync

mkdir:已創建目錄/etc/rsyncd

[root@localhost~]#mkdir-pv/etc/rsyncd

mkdir:已創建目錄/etc/rsyncd

[root@localhost~]#cd/etc/rsyncd

[root@localhostrsync]#pwd

/etc/rsync

vim/etc/rsyncd/rsyncd.secrets

zhang:123

chmod600/etc/rsyncd/rsyncd.secrets

vim/etc/rsyncd/rsyncd.conf編輯配置文件

uid=root

gid=root

port=873

maxconnections=0#limitclientconection

usechroot=no

logfile=/var/log/rsyncd.log

pidfile=/var/run/rsyncd.pid

lockfile=/var/run/rsyncd.lock

motdfile=/etc/rsyncd/rsyncd.motd

readonly=no###############!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

####limituserconn######

hostsallow=192.168.18.0/255.255.255.0

hostsdeny=*

#transferlogging=yes

#logformat=%t%a%m%f%b

#syslogfacility=local3

#timeout=300

[www]

path=/tmp/bak

list=yes

ignoreerrors

authusers=zhang

###username!!

secretsfile=/etc/rsyncd/rsyncd.secrets

comment=wwwdirectory

readonly=no

開啓服務

[root@localhost~]#rsync--daemon--config=/etc/rsyncd/rsyncd.conf

[root@localhost~]#lsof-i:873

COMMANDPIDUSERFDTYPEDEVICESIZENODENAME

rsync14364root3uIPv647367TCP*:rsync(LISTEN)

rsync14364root5uIPv447368TCP*:rsync(LISTEN)

67上安裝這個工具。主要是監聽內核的,會出發rsyncclient端。

[root@localhosttmp]#tarfvxzsersync.tar.gz

GNU-Linux-x86/

GNU-Linux-x86/sersync2

GNU-Linux-x86/confxml.xml

[root@localhosttmp]#cdGNU-Linux-x86/

[root@localhostGNU-Linux-x86]#ls

confxml.xmlsersync2

[root@localhostGNU-Linux-x86]#pwd

/tmp/GNU-Linux-x86

vimconfxml.xml修改監聽條件

主要修改的地方是

<sersync>

<localpathwatch="/var/www/html">#本地的需要備份文件的路徑

<remoteip="192.168.18.167"name="www"/>#備份機的ip及共享名

<!--<remoteip="192.168.8.39"name="tongbu"/>-->

<!--<remoteip="192.168.8.40"name="tongbu"/>-->

</localpath>

<rsync>

<commonParamsparams="-artuz"/>

<authstart="true"users="zhang"passwordfile="/etc/rsyncd/rsyncd.secrets"/>

#密碼文件

<userDefinedPortstart="false"port="874"/><!--port=874-->

<timeoutstart="false"time="100"/><!--timeout=100-->

<sshstart="false"/>

</rsync>

<failLogpath="/tmp/rsync_log"timeToExecute="60"/><!--defaultevery60minsexecuteonce-->#錯誤日誌存放路徑(自定義)

在整個配置中的true,全部是監聽,false是不監聽。以上的這些信息都是可以自己修改的

67上開啓監聽

[root@localhostGNU-Linux-x86]#./sersync2-r

setthesystemparam

executeecho50000000>/proc/sys/fs/inotify/max_user_watches

executeecho327679>/proc/sys/fs/inotify/max_queued_events

parsethecommandparam

option:-rrsyncallthelocalfilestotheremoteserversbeforethesersyncwork

daemonthreadnum:10

parsexmlconfigfile

hostip:localhosthostport:8008

usersyncpassword-file:

useriszhang

passwordfileis/etc/rsyncd/rsyncd.secrets

configxmlparsesuccess

pleaseset/etc/rsyncd.confmaxconnections=0Manually

sersyncworkingthread12=1(primarythread)+1(failretrythread)+10(daemonsubthreads)

Maxthreadsnumbersis:22=12(Threadpoolnums)+10(Subthreads)

pleaseaccordingyourcpuuse-nparamtoadjustthecpurate

------------------------------------------

rsyncthedirectoryrecursivlytotheremoteserversonce

workingpleasewait...

executecommand:cd/var/www/html&&rsync-artuz-R--delete./[email protected]::www--password-file=/etc/rsyncd/rsyncd.secrets>/dev/null2>&1

runthesersync:

watchpathis:/var/www/html

這樣就可以成功了

167上查看

[root@localhostbak]#ls

123abc

錯誤日誌路徑

tail-f/tmp/rsync_log

如果不成功,說明是在67上的/etc/rsyncd/rsyncd.secrets中多寫用戶名,將用戶名刪掉就可以

如:zhang123寫成123保存即可

當目錄層級比較多的時候這個備份不是很好用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章