1. 程式人生 > >web+nfs+rsync實時備份

web+nfs+rsync實時備份

NFS web rsync sersync

網絡結構

技術分享圖片

服務器及IP主機名稱規劃

使用的4臺服務器主機名IP對應信息見下表

服務器說明

外網IP

內網IP

主機名稱

web服務器

10.0.0.8/24

172.16.1.8/24

web01

web服務器

10.0.0.7/24

172.16.1.7/24

web02

NFS存儲服務器

10.0.0.31/24

172.16.1.31/24

nfs01

rsync備份服務器

10.0.0.41/24

172.16.1.41/24

backup

實例1-2 搭建網站集群後端NFS共享存儲搭建及優化解決方案

1、配置 NFS 服務:

要求:

1)NFS服務端nfs01上共享/data目錄,允許從NFS客戶端web01web02上分別掛載共享目錄。

#web01 and web01 and nfs01

上都安裝nfsrpcbind軟件包

yum -y install nfs-utils.x86_64 rpcbind.x86_64

#查看一下軟件包是否安裝完成

[root@web01 ~]# rpm -qa nfs-utils rpcbind

rpcbind-0.2.0-11.el6.x86_64

nfs-utils-1.2.3-39.el6.x86_64

nfs01服務器上啟動rpcbindnfs服務:

/etc/init.d/rpcbind start && /etc/init.d/nfs start #先啟動rpcbind服務在啟動nfs服務

#把服務設置成開機自動啟動

chkconfig --level 3 rpcbind on && chkconfig --level 3 nfs on

#查看兩個服務的啟動狀態

[root@nfs01 ~]# /etc/init.d/rpcbind status

rpcbind (pid 3137) is running...

[root@nfs01 ~]# /etc/init.d/nfs status

rpc.svcgssd is stopped

rpc.mountd (pid 3167) is running...

nfsd (pid 3182 3181 3180 3179 3178 3177 3176 3175) is running...

#查看rpc服務記錄的端口

[root@nfs01 ~]# rpcinfo -p 172.16.1.31

program vers proto port service

100000 4 tcp 111 portmapper

100000 3 tcp 111 portmapper

100000 2 tcp 111 portmapper

100000 4 udp 111 portmapper

100000 3 udp 111 portmapper

100000 2 udp 111 portmapper

100005 1 udp 42356 mountd

100005 1 tcp 47452 mountd

100005 2 udp 43716 mountd

100005 2 tcp 57425 mountd

100005 3 udp 42301 mountd

100005 3 tcp 48047 mountd

100003 2 tcp 2049 nfs

100003 3 tcp 2049 nfs

100003 4 tcp 2049 nfs

100227 2 tcp 2049 nfs_acl

100227 3 tcp 2049 nfs_acl

100003 2 udp 2049 nfs

100003 3 udp 2049 nfs

100003 4 udp 2049 nfs

100227 2 udp 2049 nfs_acl

100227 3 udp 2049 nfs_acl

100021 1 udp 43733 nlockmgr

100021 3 udp 43733 nlockmgr

100021 4 udp 43733 nlockmgr

100021 1 tcp 36504 nlockmgr

100021 3 tcp 36504 nlockmgr

100021 4 tcp 36504 nlockmgr

#然後創建共享的目錄/data

mkdir /data

#然後更改nfs服務端的配置文件/etc/exports

/data 172.16.1.0/24(rw,sync,root_squash,no_all_squash,anonuid=888,anongid=888)

#然後分別在web01 and web02 and nfs01上創建uid888webuser用戶

useradd -u 888 webuser -s /sbin/nologin –M

#更改/data目錄的權限,使web有權對他進行讀寫

chown -R webuser:webuser /data/

#重啟服務,平滑的重啟

[root@nfs01 ~]# exportfs -arv

exporting 172.16.1.0/24:/data

#查看共享的文件夾

[root@nfs01 ~]# showmount -e 172.16.1.31

Export list for 172.16.1.31:

/data 172.16.1.0/24

#現在本地掛載

[root@nfs01 ~]# df -Th

Filesystem Type Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root ext4 18G 989M 16G 6% /

tmpfs tmpfs 491M 0 491M 0% /dev/shm

/dev/sda1 ext4 485M 33M 427M 8% /boot

/dev/sr0 iso9660 4.2G 4.2G 0 100% /media/cdrom

172.16.1.31:/data nfs 18G 989M 16G 6% /mnt

[root@nfs01 ~]# grep mnt /proc/mounts

172.16.1.31:/data/ /mnt nfs4 rw,relatime,vers=4,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.31,minorversion=0,local_lock=none,addr=172.16.1.31 0 0

#本地掛載成功,然後去web01 and web02上掛載,在兩臺服務器上分別執行下面

[root@web01 ~]# showmount -e 172.16.1.31 #先看看能不能看到掛載目錄

Export list for 172.16.1.31:

/data 172.16.1.0/24

[root@web01 ~]# mkdir -p /mnt/data ;mount -t nfs 172.16.1.31:/data /mnt/data

[root@web01 ~]# df -Th

Filesystem Type Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root ext4 18G 887M 16G 6% /

tmpfs tmpfs 491M 0 491M 0% /dev/shm

/dev/sda1 ext4 485M 33M 427M 8% /boot

/dev/sr0 iso9660 4.2G 4.2G 0 100% /media/cdrom

172.16.1.31:/data nfs 18G 989M 16G 6% /mnt/data

[root@web01 ~]# grep /mnt/data /proc/mounts

172.16.1.31:/data/ /mnt/data nfs4 rw,relatime,vers=4,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.8,minorversion=0,local_lock=none,addr=172.16.1.31 0 0

#然後把掛載命令放到/etc/rc.local裏面達到開機自啟的效果

echo "mount -t nfs 172.16.1.31:/data /mnt/data" >>/etc/rc.local

2)當在NFS客戶端web01上的掛載點/data寫入數據時,NFS客戶端web02上也可以刪除或修改。

#然後在web01/mnt/data 下創建一個web01_tets.txt的文件,內容為“this is web01”,在web02/mnt/data 下創建一個web02_tets.txt的文件,內容為“this is web02”,然後分別在對端看一下能否看到對端創建的文件,然後測試是否能修改刪除

web01上:

echo "this is web01" >>/mnt/data/web01_tets.txt

[root@web01 ~]# ls /mnt/data/

web01_tets.txt web02_test.txt

[root@web01 ~]# cat /mnt/data/web02_test.txt

this is web02

[root@web01 ~]# rm -f /mnt/data/web02_test.txt #刪除成功

[root@web01 ~]# ls /mnt/data/

web02上:

[root@web02 ~]# echo "this is web02" >>/mnt/data/web02_test.txt

[root@web02 ~]# ls /mnt/data/

web01_tets.txt web02_test.txt

[root@web02 ~]# cat /mnt/data/web01_tets.txt

this is web01

[root@web02 ~]# rm -f /mnt/data/web01_tets.txt

[root@web02 ~]# ls /mnt/data/

web02_test.txt

實例1-3 搭建網站集群全網備份服務器backup

l 要求:在backup服務器上配置Rsync數據同步服務,從nfs01服務器上可以推送數據到backup服務器的/backup目錄

l 具體要求:backup 服務器的備份目錄必須為/backup

服務端:

yum –y install rsync.x86_64

創建rsync服務端的配置文件

cat >>/etc/rsyncd.conf<<EOF

#rsync_config________start

##rsyncd.conf start##

uid = rsync

gid = rsync

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

[backup]

path = /backup

ignore errors

read only = false

list = false

hosts allow = 172.16.1.0/24

#hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

#rsync_config________end

EOF

#創建虛擬用戶rsync,不創建家目錄

[root@backup ~]# useradd rsync -s /sbin/nologin -M

#創建/backup備份目錄,並把屬主和屬組改成rsync

[root@backup ~]# mkdir /backup

[root@backup ~]# chown -R rsync:rsync /backup

[root@backup ~]# ll -d /backup/

drwxr-xr-x 2 rsync rsync 4096 May 12 10:44 /backup/

#創建同步用戶的密碼文件,並把權限設為600

[root@backup ~]# echo "rsync_backup:password" >/etc/rsync.password

[root@backup ~]# chmod 600 /etc/rsync.password

#啟動服務,並設置開機自啟動

[root@backup ~]# rsync –daemon

[root@backup ~]#echo “rsync –daemon” >>/etc/rc.local

#客戶端安裝rsync軟件包,生成密碼文件

yum –y install rsync.x86_64

[root@nfs01 ~]# echo "password" > /etc/rsync.password

[root@nfs01 ~]# chmod 600 /etc/rsync.password

[root@nfs01 ~]# mkdir /backup

#測試能不能推送成功

[root@nfs01 ~]# rsync -avz /backup/ [email protected]::backup/ --password-file=/etc/rsync.password

sending incremental file list

./

test.txt

sent 78 bytes received 30 bytes 216.00 bytes/sec

total size is 0 speedup is 0.00

實例1-4 實時數據同步要求

當用戶通過任意臺web服務器將數據寫入到NFS服務器nfs01時,同時復制到備份服務器backup

在需要實時同步的客戶端上執行操作:(NFS服務器上)

下載sersync的歸檔壓縮包

下載地址:https://code.google.com/archive/p/sersync/downloads

下載完成後使用rz命令上傳到NFS服務器上

技術分享圖片

[root@nfs01 ~]# mv sersync2.5.4_64bit_binary_stable_final.tar.gz /tools/

[root@nfs01 ~]# ls

[root@nfs01 ~]# cd /tools/

[root@nfs01 tools]# tar -xzvf sersync2.5.4_64bit_binary_stable_final.tar.gz

GNU-Linux-x86/

GNU-Linux-x86/sersync2

GNU-Linux-x86/confxml.xml

[root@nfs01 tools]# cd GNU-Linux-x86/

#然後備份編輯配置文件

[root@nfs01 GNU-Linux-x86]# cp confxml.xml{,.bak}

[root@nfs01 GNU-Linux-x86]# vim confxml.xml

<sersync>

<localpath watch="/data(要同步的目錄)">

<remote ip="172.16.1.41(rsync主機地址)" name="backup(模塊名)"/>

</localpath>

<rsync>

<commonParams params="-az"/>

<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>

<userDefinedPort start="false" port="874"/><!-- port=874 -->

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

<ssh start="false"/>

</rsync>

<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every

60mins execute once-->

<crontab start="true" schedule="30"><!--600mins-->

#然後啟動服務:

[root@nfs01 /]# ln -s /tools/GNU-Linux-x86/sersync2 /usr/sbin/sersync2 #創建一個連接文件

[root@nfs01 /]# sersync2 -dro /tools/GNU-Linux-x86/confxml.xml(配置文件的位置)

set the system param

executeecho 50000000 > /proc/sys/fs/inotify/max_user_watches

executeecho 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 /tools/GNU-Linux-x86/confxml.xml

daemon thread num: 10

parse xml config file

host ip : localhost host port: 8008

daemon startsersync run behind the console

Start the crontab Every 30 minutes rsync all the files to the remote servers entirely

use rsync password-file :

user is rsync_backup (用戶名)

passwordfile is /etc/rsync.password(密碼文件)

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 -az -R --delete ./ [email protected]::backup --password-file=/etc/rsync.password >/dev/null 2>&1

run the sersync:

watch path is: /data

#開機自啟動:

[root@nfs01 /]# echo "/usr/sbin/sersync2 -dro /tools/GNU-Linux-x86/confxml.xml" >> /etc/rc.local

#然後從任何一個web服務器在/mnt/data目錄裏面寫入文件,查看一下rsync/backup目錄下是否存在,存在實時備份成功

[root@web01 data]# echo "tess haha " >>/mnt/data/test.txt

[root@backup ~]# ls /backup/

test.txt

[root@backup ~]# cat /backup/test.txt

123

tess haha


web+nfs+rsync實時備份