前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >web+nfs+rsync实时备份

web+nfs+rsync实时备份

作者头像
张琳兮
发布2018-09-10 11:35:31
2.1K0
发布2018-09-10 11:35:31
举报
文章被收录于专栏:首富手记首富手记

网络结构

1.png
1.png

服务器及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

#查看一下软件包是否安装完成

[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服务器上启动rpcbind和nfs服务:

/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上创建uid为888的webuser用户

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/ rsync_backup@172.16.1.41::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

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 ./ rsync_backup@172.16.1.41::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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档