web+nfs+rsync实时备份
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web+nfs+rsync实时备份相关的知识,希望对你有一定的参考价值。
网络结构
服务器及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客户端web01、web02上分别挂载共享目录。
#在web01 and web01 and nfs01上都安装nfs和rpcbind软件包
yum -y install nfs-utils.x86_64 rpcbind.x86_64
#查看一下软件包是否安装完成
[[email protected] ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-11.el6.x86_64
nfs-utils-1.2.3-39.el6.x86_64
在nfs01服务器上启动rpcbind和nfs服务:
/etc/init.d/rpcbind start && /etc/init.d/nfs start #先启动rpcbind服务在启动nfs服务
#把服务设置成开机自动启动
chkconfig --level 3 rpcbind on && chkconfig --level 3 nfs on
#查看两个服务的启动状态
[[email protected] ~]# /etc/init.d/rpcbind status
rpcbind (pid 3137) is running...
[[email protected] ~]# /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服务记录的端口
[[email protected] ~]# 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上创建uid为888的webuser用户
useradd -u 888 webuser -s /sbin/nologin –M
#更改/data目录的权限,使web有权对他进行读写
chown -R webuser:webuser /data/
#重启服务,平滑的重启
[[email protected] ~]# exportfs -arv
exporting 172.16.1.0/24:/data
#查看共享的文件夹
[[email protected] ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
#现在本地挂载
[[email protected] ~]# 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
[[email protected] ~]# 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上挂载,在两台服务器上分别执行下面
[[email protected] ~]# showmount -e 172.16.1.31 #先看看能不能看到挂载目录
Export list for 172.16.1.31:
/data 172.16.1.0/24
[[email protected] ~]# mkdir -p /mnt/data ;mount -t nfs 172.16.1.31:/data /mnt/data
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# ls /mnt/data/
web01_tets.txt web02_test.txt
[[email protected] ~]# cat /mnt/data/web02_test.txt
this is web02
[[email protected] ~]# rm -f /mnt/data/web02_test.txt #删除成功
[[email protected] ~]# ls /mnt/data/
web02上:
[[email protected] ~]# echo "this is web02" >>/mnt/data/web02_test.txt
[[email protected] ~]# ls /mnt/data/
web01_tets.txt web02_test.txt
[[email protected] ~]# cat /mnt/data/web01_tets.txt
this is web01
[[email protected] ~]# rm -f /mnt/data/web01_tets.txt
[[email protected] ~]# 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,不创建家目录
[[email protected] ~]# useradd rsync -s /sbin/nologin -M
#创建/backup备份目录,并把属主和属组改成rsync
[[email protected] ~]# mkdir /backup
[[email protected] ~]# chown -R rsync:rsync /backup
[[email protected] ~]# ll -d /backup/
drwxr-xr-x 2 rsync rsync 4096 May 12 10:44 /backup/
#创建同步用户的密码文件,并把权限设为600
[[email protected] ~]# echo "rsync_backup:password" >/etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password
#启动服务,并设置开机自启动
[[email protected] ~]# rsync –daemon
[[email protected] ~]#echo “rsync –daemon” >>/etc/rc.local
#客户端安装rsync软件包,生成密码文件
yum –y install rsync.x86_64
[[email protected] ~]# echo "password" > /etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# mkdir /backup
#测试能不能推送成功
[[email protected] ~]# 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服务器上
[[email protected] ~]# mv sersync2.5.4_64bit_binary_stable_final.tar.gz /tools/
[[email protected] ~]# ls
[[email protected] ~]# cd /tools/
[[email protected] tools]# tar -xzvf sersync2.5.4_64bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[[email protected] tools]# cd GNU-Linux-x86/
#然后备份编辑配置文件
[[email protected] GNU-Linux-x86]# cp confxml.xml{,.bak}
[[email protected] 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-->
#然后启动服务:
[[email protected] /]# ln -s /tools/GNU-Linux-x86/sersync2 /usr/sbin/sersync2 #创建一个连接文件
[[email protected] /]# sersync2 -dro /tools/GNU-Linux-x86/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: /tools/GNU-Linux-x86/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync 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
#开机自启动:
[[email protected] /]# echo "/usr/sbin/sersync2 -dro /tools/GNU-Linux-x86/confxml.xml" >> /etc/rc.local
#然后从任何一个web服务器在/mnt/data目录里面写入文件,查看一下rsync的/backup目录下是否存在,存在实时备份成功
[[email protected] data]# echo "tess haha " >>/mnt/data/test.txt
[[email protected] ~]# ls /backup/
test.txt
[[email protected] ~]# cat /backup/test.txt
123
tess haha
以上是关于web+nfs+rsync实时备份的主要内容,如果未能解决你的问题,请参考以下文章
NFS +inotify+rsync 实现数据的远程挂载与实时增量备份