day10、nfs+rsync全网备份及实时同步

时间:2022-04-24
本文章向大家介绍day10、nfs+rsync全网备份及实时同步,主要内容包括题目要求、第2章 搭建网站集群后端NFS共享存储搭建及优化解决方案、2.2 web01服务器部署、2.3 web02服务器部署、第3章 搭建网站集群全网备份服务器backup、第4章 实时数据同步部署、4.2 inotify方法、第5章 实现按时备份web服务器数据、5.2 第二步、写脚本、5.3 写定时任务、5.4 backup备份服务器部署、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

题目要求

注意:博主使用的系统为:

[root@web01 ~]# uname -a Linux web01 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

实例1-1 服务器及IP主机名规划

已知 4 台服务器主机名主机对应信息见下表:

服务器说明

外网 IP(NAT)

内网 IP(NAT)

主机名

web服务器

10.0.0.7/24

172.16.1.7/24

web02

web服务器

10.0.0.8/24

172.16.1.8/24

web01

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上分别挂载共享目录。

2)当在NFS客户端web01上的挂载点/data写入数据时,在NFS客户端web02上也可以删除或修改。

3)问答题:如何优化NFS服务?

实例1-3 搭建网站集群全网备份服务器backup

l 要求:在backup服务器上配置Rsync数据同步服务,从nfs01服务器上可以推送数据到backup服务器的/backup目录

l 具体要求:backup 服务器的备份目录必须为/backup。

实例1-4 实时数据同步要求

当用户通过任意台web服务器将数据写入到NFS服务器nfs01时,同时复制到备份服务器backup。

第2章 搭建网站集群后端NFS共享存储搭建及优化解决方案

2.1 NFS存储服务器部署

2.1.1 第一步、部署NFS服务端

2.1.1.1 一、查看nfs和rpc的安装

[root@nfs01 ~]# rpm -qa |grep -E "nfs|rpc"
nfs-utils-1.2.3-75.el6.x86_64
xmlrpc-c-1.16.24-1210.1840.el6.x86_64
xmlrpc-c-client-1.16.24-1210.1840.el6.x86_64
nfs-utils-lib-1.1.5-13.el6.x86_64
rpcbind-0.2.0-13.el6_9.1.x86_64
libtirpc-0.2.1-13.el6_9.x86_64

实例2-1 nfs和rpc服务进程的安装方法

yum install -y nfs-utils rpcbind

2.1.1.2 二、编写NFS配置文件

[root@nfs01 ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync)

2.1.1.3 三、创建共享目录,并且进行权限设定

[root@nfs01 ~]# mkdir /data -p
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data/
[root@nfs01 ~]# ll -d /data/
drwxr-xr-x 2 nfsnobody nfsnobody 24576 Oct 17 17:25 /data/
说明:NFS共享目录管理用户为nfsnobody,此用户不用创建;安装NFS软件的时候会自动创建

2.1.1.4 四、启动服务

首先启动rpc服务

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

其次启动nfs服务

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

查看可挂载的共享目录

[root@nfs01 ~]# showmount -e 10.0.0.31
Export list for 10.0.0.31:
/data 172.16.1.0/24

2.1.1.5 五、设置为开机自启动

chkconfig rpcbind on
chkconfig nfs on

2.1.2 第二步、优化NFS服务

思路:两台连接的web服务器上同时创建相同UID的www用户,NFS服务器上配置anonuid和anongid参数,设置不让全部用户进行映射,只映射root用户

配置文件修改:

[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,no_all_squash,root_squash,anonuid=666,anongid=666)

授权共享目录属主和属组为www权限

[root@nfs01 data]# chown www.www /data/
[root@nfs01 data]# ll -d /data/
drwxr-xr-x 2 www www 24576 Oct 18 17:25 /data/

平滑重启nfs服务

[root@nfs01 data]# /etc/init.d/nfs reload

2.1.3 第三步、rsync客户端部署

2.1.3.1 一、rsync软件的安装

[root@nfs01 tmp]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

2.1.3.2 二、建立认证文件,并改600权限

[root@nfs01 data]# echo "oldboy123" >/etc/rsync.password
[root@nfs01 data]# chmod 600 /etc/rsync.password
[root@nfs01 data]# ll -d /etc/rsync.password
-rw------- 1 root root 10 Oct 18 18:52 /etc/rsync.password

2.1.3.3 三、推送测试

nfs服务器:推送

[root@nfs01 data]# rsync -avz ts.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password 
sending incremental file list
ts.txt
sent 63 bytes  received 27 bytes  180.00 bytes/sec
total size is 0  speedup is 0.00

backup服务器:查看

[root@backup backup]# ll
total 8
drwxr-xr-x 2 rsync rsync 4096 Oct 18 00:00 172.16.1.31
drwxr-xr-x 2 rsync rsync 4096 Oct 13 20:03 172.16.1.8
-rw-r--r-- 1 rsync rsync    0 Oct 18 18:53 ts.txt

2.2 web01服务器部署

2.2.1 第一步、部署NFS客户端

2.2.1.1 一、确认nfs和rpc的安装

[root@web01 ~]# rpm -qa|grep -E "nfs|rpc"
nfs-utils-1.2.3-75.el6.x86_64
xmlrpc-c-1.16.24-1210.1840.el6.x86_64
xmlrpc-c-client-1.16.24-1210.1840.el6.x86_64
nfs-utils-lib-1.1.5-13.el6.x86_64
rpcbind-0.2.0-13.el6_9.1.x86_64
libtirpc-0.2.1-13.el6_9.x86_64

2.2.1.2 二、启动rpcbind服务

[root@web01 ~]# /etc/init.d/rpcbind start

2.2.1.3 三、检查可挂载的共享目录

[root@web01 ~]# showmount -e 10.0.0.31
Export list for 10.0.0.31:
/data 172.16.1.0/24

2.2.1.4 四、挂载共享目录

[root@web01 ~]# mount -t nfs 172.16.1.31:/data/ /mnt/
[root@web01 ~]# ll -d /mnt/
drwxr-xr-x 2 nfsnobody nfsnobody 24576 Oct 17  2017 /mnt/
[root@web01 ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda3           8.8G  1.5G  6.9G  18% /
tmpfs               238M     0  238M   0% /dev/shm
/dev/sda1           190M   35M  146M  19% /boot
172.16.1.31:/data/  8.8G  1.5G  6.9G  18% /mnt

2.2.1.5 五、测试

web01服务器:

[root@web01 ~]# cd /mnt/
[root@web01 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18  2017 oldboy.txt
[root@web01 mnt]# touch web01.txt
[root@web01 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18  2017 oldboy.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18  2017 web01.txt

nfs存储服务器:

[root@nfs01 data]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18 17:23 oldboy.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18 17:25 web01.txt

2.2.1.6 六、设置开机自动挂载

利用/etc/fstab文件结合netfs服务,实现自动挂载

先编辑/etc/fstab文件

172.16.1.31:/data/r    /data/r               nfs    defaults  0 0 

在开机自启动netfs服务

[root@web01 mnt]# chkconfig netfs on
[root@web01 mnt]# chkconfig |grep "netfs"
netfs           0:off       1:off       2:on       3:on       4:on       5:on       6:off

2.2.2 优化

2.2.2.1 创建www用户设置uid为666

[root@web01 mnt]# useradd -u 666 www
[root@web01 mnt]# id www
uid=666(www) gid=666(www) groups=666(www)

2.2.2.2 重新挂载

[root@web01 ~]# umount /mnt/
[root@web01 ~]# umount /mnt/
umount: /mnt/: not mounted
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/ /mnt/
[root@web01 ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda3           8.8G  1.5G  6.9G  18% /
tmpfs               238M     0  238M   0% /dev/shm
/dev/sda1           190M   35M  146M  19% /boot
172.16.1.31:/data/  8.8G  1.5G  6.9G  18% /mnt

2.2.2.3 测试

root用户测试:可以进行操作

[root@web01 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18  2017 oldboy.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18  2017 web01.txt
[root@web01 mnt]# rm -f web01.txt 
[root@web01 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18  2017 oldboy.txt

www用户测试:可以进行操作

[www@web01 mnt]$ touch web01.txt
[www@web01 mnt]$ ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18  2017 oldboy.txt
-rw-rw-r-- 1 www       www       0 Oct 18  2017 web01.txt

其他用户测试:不可操作(提升了安全性)

[root@web01 ~]# useradd lyq
[root@web01 ~]# echo "123456"|passwd --stdin lyq
Changing password for user lyq.
passwd: all authentication tokens updated successfully.
[root@web01 ~]# su - lyq
[lyq@web01 ~]$ cd /mnt/
[lyq@web01 mnt]$ rm -f web01.txt 
rm: cannot remove `web01.txt': Permission denied

2.3 web02服务器部署

2.3.1 第一步、部署NFS客户端

2.3.1.1 一、确认nfs和rpc安装

[root@web02 ~]# yum install -y nfs-utils rpcbind
[root@web02 ~]# rpm -qa |egrep  "nfs|rpcbind"
nfs-utils-1.2.3-75.el6.x86_64
nfs-utils-lib-1.1.5-13.el6.x86_64
rpcbind-0.2.0-13.el6_9.1.x86_64

2.3.1.2 二、启动rpcbind服务

[root@web02 ~]# /etc/init.d/rpcbind start
Starting rpcbind:                           [  OK  ]

2.3.1.3 三、检查可挂载的共享目录

[root@web02 ~]# showmount -e 10.0.0.31
Export list for 10.0.0.31:
/data 172.16.1.0/24

2.3.1.4 四、挂载共享目录

[root@web02 ~]# mount -t nfs 172.16.1.31:/data/ /mnt/
[root@web02 ~]# ll -d /mnt/
drwxr-xr-x 2 nfsnobody nfsnobody 24576 Oct 17 17:25 /mnt/
[root@web02 ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda3           8.8G  1.5G  6.9G  18% /
tmpfs               238M     0  238M   0% /dev/shm
/dev/sda1           190M   35M  146M  19% /boot
172.16.1.31:/data/  8.8G  1.5G  6.9G  18% /mnt

2.3.1.5 五、测试

web02服务器:

[root@web02 ~]# cd /mnt/
[root@web02 mnt]# ll
total 0
[root@web02 mnt]# touch oldboy.txt
[root@web02 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18 17:23 oldboy.txt

nfs存储服务器:

[root@nfs01 ~]# cd /data/
[root@nfs01 data]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18 17:23 oldboy.txt

2.3.1.6 六、设置开机自动挂载

利用/etc/fstab文件结合netfs服务,实现自动挂载

先编辑/etc/fstab文件

172.16.1.31:/data/r    /data/r               nfs    defaults  0 0 

在开机自启动netfs服务

[root@web02 mnt]# chkconfig netfs on
[root@web02 mnt]# chkconfig |grep "netfs"
netfs           0:off       1:off       2:on       3:on       4:on       5:on       6:off

2.3.2 优化

2.3.2.1 创建www用户,设置uid为666

[root@web02 mnt]# useradd -u 666 www
[root@web02 mnt]# id www
uid=666(www) gid=666(www) groups=666(www)

2.3.2.2 重新挂载

[root@web02 ~]# umount /mnt
[root@web02 ~]# umount /mnt
umount: /mnt: not mounted
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/ /mnt
[root@web02 ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda3           8.8G  1.5G  6.9G  18% /
tmpfs               238M     0  238M   0% /dev/shm
/dev/sda1           190M   35M  146M  19% /boot
172.16.1.31:/data/  8.8G  1.5G  6.9G  18% /mnt

2.3.2.3 测试

root用户测试:可以操作

[root@web02 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 18 17:23 oldboy.txt
-rw-rw-r-- 1 www       www       0 Oct 18 18:26 web01.txt
[root@web02 mnt]# rm -f oldboy.txt 
[root@web02 mnt]# ll
total 0
-rw-rw-r-- 1 www www 0 Oct 18 18:26 web01.txt

www用户测试:可以操作

[www@web02 mnt]$ touch web02.txt
[www@web02 mnt]$ ll
total 0
-rw-rw-r-- 1 www www 0 Oct 18 18:26 web01.txt
-rw-rw-r-- 1 www www 0 Oct 18 18:32 web02.txt

其他用户:不可以操作

[root@web02 ~]# useradd lyq
[root@web02 ~]# echo "123456"|passwd --stdin lyq
Changing password for user lyq.
passwd: all authentication tokens updated successfully.
[root@web02 ~]# su - lyq
[lyq@web02 ~]$ cd /mnt
[lyq@web02 mnt]$ rm -f web02.txt 
rm: cannot remove `web02.txt': Permission denied

第3章 搭建网站集群全网备份服务器backup

要求:在backup服务器上配置Rsync数据同步服务,从nfs01服务器上可以推送数据到backup服务器的/backup目录

具体要求:backup 服务器的备份目录必须为/backup。

3.1 第一、rsync服务部署

3.1.1 一、确认rsync服务软件安装

[root@backup ~]# rpm -qa |grep rsync
rsync-3.0.6-12.el6.x86_64

3.1.2 二、编写配置文件

默认没有这个文件

[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
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
[backup]
comment = "backup dir by oldboy"
path = /backup

3.1.3 三、创建rsync服务,管理用户

[root@backup ~]# useradd -s /sbin/nologin rsync -M
[root@backup ~]# id rsync
uid=501(rsync) gid=501(rsync) groups=501(rsync)

3.1.4 四、创建备份目录,并设置属主和属组为rsync

[root@backup ~]# mkdir -p /backup
[root@backup ~]# chown rsync.rsync /backup/
[root@backup ~]# ll -d /backup/

drwxr-xr-x 4 rsync rsync 4096 Oct 14 10:03 /backup/

3.1.5 五、创建认证密码文件,并设置600权限

[root@backup ~]# echo "rsync_backup:oldboy123" >/etc/rsync.password
[root@backup ~]# cat /etc/rsync.password
rsync_backup:oldboy123
[root@backup ~]# chmod 600 /etc/rsync.password
[root@backup ~]# ll /etc/rsync.password
-rw------- 1 root root 23 Oct 18 18:46 /etc/rsync.password

3.1.6 六、启动服务

[root@backup ~]# rsync --daemon
[root@backup ~]# ps -ef |grep rsync
root      32060      1  0 18:47 ?        00:00:00 rsync --daemon
root      32062  30842  0 18:48 pts/0    00:00:00 grep --color=auto rsync
[root@backup ~]# netstat -lntup |grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      32060/rsync         
tcp        0      0 :::873                      :::*                        LISTEN      32060/rsync

第4章 实时数据同步部署

4.1 serync方法

4.1.1 安装sersync软件(二进制包安装)

a 将软件上传到服务器

rz -E

b 解压软件

[root@nfs01 tools]# unzip sersync_installdir_64bit.zip 

软件结构

[root@nfs01 tools]# tree sersync_installdir_64bit
sersync_installdir_64bit
└── sersync
 ├── bin
 │ └── sersync
 ├── conf
 │ └── confxml.xml
 └── logs

c 移动

[root@nfs01 ~]# tree /usr/local/sersync/
/usr/local/sersync/
├── bin
│ └── sersync
├── conf
│ └── confxml.xml
└── logs
3 directories, 2 files

4.1.2 编写sersync配置文件

[root@nfs01 ~]# vim /usr/local/sersync/conf/confxml.xml
24         <localpath watch="/data">
25             <remote ip="172.16.1.41" name="backup"/>
26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28         </localpath>
29         <rsync>
30             <commonParams params="-az"/>
31             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
33             <timeout start="false" time="100"/><!-- timeout=100 -->
34             <ssh start="false"/>
35         </rsync>

4.1.3 启动sersync服务

[root@nfs01 ~]# cd /usr/local/sersync/
[root@nfs01 sersync]# cd bin
[root@nfs01 bin]# ll
total 1768
-rw-r--r-- 1 root root 1810128 Oct 26  2011 sersync
[root@nfs01 bin]# chmod +x sersync 
[root@nfs01 bin]# ll
total 1768
-rwxr-xr-x 1 root root 1810128 Oct 26  2011 sersync

启动命令:(参数o一定要在参数最后)

[root@nfs01 bin]# ./sersync -dro /usr/local/sersync/conf/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:  /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost       host port: 8008
daemon start,sersync run behind the console 
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::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data

4.1.4 测试

web02服务器:

[root@web02 mnt]# ll
total 0
-rw-r--r-- 1 www www 0 Oct 18 19:18 ceshi.txt
-rw-rw-r-- 1 www www 0 Oct 18 18:26 web01.txt
-rw-rw-r-- 1 www www 0 Oct 18 18:32 web02.txt
[root@web02 mnt]# rm -f ./*
[root@web02 mnt]# ll
total 0

nfs服务器:

[root@nfs01 bin]# inotify wd:1      name:ceshi.txt      mask:512
cd /data && rsync -az -R --delete ./   --include="ceshi.txt" --exclude=*  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
inotify wd:1 name:web01.txt   mask:512
inotify wd:1 name:web02.txt   mask:512
cd /data && rsync -az -R --delete ./   --include="web01.txt" --exclude=*  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
cd /data && rsync -az -R --delete ./   --include="web02.txt" --exclude=*  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

backup服务器:

[root@backup backup]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 Oct 18 19:18 ceshi.txt
-rw-rw-r-- 1 rsync rsync 0 Oct 18 18:26 web01.txt
-rw-rw-r-- 1 rsync rsync 0 Oct 18 18:32 web02.txt
[root@backup backup]# ll
total 0

4.2 inotify方法

4.2.1 安装inotify软件

[root@nfs01 data]# yum install -y inotify-tools
[root@nfs01 data]# rpm -ql inotify-tools
/usr/bin/inotifywait
/usr/bin/inotifywatch

4.2.2 进行监控测试

创建事件(create)

创建文件监控事件:

/data/ CREATE oldboy.txt
/data/ OPEN oldboy.txt
/data/ ATTRIB oldboy.txt
/data/ CLOSE_WRITE,CLOSE oldboy.txt

4.2.3 编写脚本,实现rsync+inotify软件功能结合

编写脚本:inotify.sh

#!/bin/bash
inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write|while read line
do
rsync -az --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
done

4.2.4 后台运行脚本

[root@nfs01 data]# sh /server/scripts/inotify.sh &

设置脚本开机自启动

第5章 实现按时备份web服务器数据

要求:每天晚上00点整在Web服务器上打包备份系统配置文件、网站程序目录及访问日志并通过rsync命令推送备份服务器 backup上备份保留(备份思路可以是先在本地按日期打包,然后再推到备份服务器 backup上),NFS存储服务器同Web服务器,实际工作中就是全部的服务器。

具体要求:

1)所有服务器的备份目录必须都为/backup。

2)要备份的系统配置文件包括但不限于:

a.定时任务服务的配置文件(/var/spool/cron/root)(适合 web 和 nfs 服务器)。

b.开机自启动的配置文件(/etc/rc.local)(适合 web 和 nfs 服务器)。

c.日常脚本的目录 (/server/scripts)。

d.防火墙 iptables 的配置文件(/etc/sysconfig/iptables)。

e.自己思考下还有什么需要备份呢?

3)Web 服务器站点目录假定为(/var/html/www)。

4)Web 服务器 A 访问日志路径假定为(/app/logs)

5)Web 服务器保留打包后的 7 天的备份数据即可(本地留存不能多于7天,因为太多硬盘会满)

6)备份服务器上,保留每周一的所有数据副本,其它要保留 6个月的数据副本。

7)备份服务器上要按照备份数据服务器的内网IP为目录保存备份,备份的文件按照时间名字保存。

8)*需要确保备份的数据尽量完整正确,在备份服务器上对备份的数据进行检查,把备份的成功及失败结果信息发给系统管理员邮箱中。

5.1 第一步、命令测试

5.1.1 查看是否建好备份目录了

web01服务器:

[lyq@web01 mnt]$ ll -d /backup/
drwxr-xr-x 3 root root 4096 Oct 13 20:10 /backup/

web02:

[root@web02 ~]# ll -d /backup/
drwxr-xr-x 2 root root 4096 Oct 18 19:49 /backup/

nfs

[root@nfs01 backup]# cd /backup/
[root@nfs01 backup]#

5.1.2 测试打包备份系统配置文件命令

web01:

[root@web01 ~]# tar zchf /backup/sys_backup_`date +%F_%w`.tar.gz /var/spool/cron/root /etc/rc.local /server/scripts/ /etc/sysconfig/iptables           ##因为/etc/rc.local文件是软连接文件所以加参数-h
tar: Removing leading `/' from member names
[root@web01 backup]# ll
total 4
-rw-r--r-- 1 root root 1385 Oct 16 20:28 sys_backup_2017-10-16_1.tar.gz

web02:

[root@web02 ~]# tar zchf /backup/sys_backup_`date +%F_%w`.tar.gz /var/spool/cron/root /etc/rc.local /server/scripts/ /etc/sysconfig/iptables           ##因为/etc/rc.local文件是软连接文件所以加参数-h
tar: Removing leading `/' from member names
[root@web02 ~]# cd /backup/
[root@web02 backup]# ll
total 4
-rw-r--r-- 1 root root 746 Oct 18 19:55 sys_backup_2017-10-18_3.tar.gz
nfs[root@nfs01 backup]#  tar zchf /backup/sys_backup_`date +%F_%w`.tar.gz /var/spool/cron/root/etc/rc.local /server/scripts/ /etc/sysconfig/iptables               ##因为/etc/rc.local是软链接,所以加一个h参数
tar: Removing leading `/' from member names

5.1.3 推送数据测试

web01:

[root@web01 backup]# rsync -avz sys_backup_2017-10-16_1.tar.gz rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password 
sending incremental file list
sent 51 bytes  received 8 bytes  118.00 bytes/sec
total size is 1385  speedup is 23.47

web02:

[root@web02 backup]# vim /etc/rsync.password
oldboy123
[root@web02 backup]# chmod 600 /etc/rsync.password
[root@web02 backup]# rsync -avz sys_backup_2017-10-18_3.tar.gz rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
sys_backup_2017-10-18_3.tar.gz
sent 841 bytes  received 27 bytes  1736.00 bytes/sec
total size is 746  speedup is 0.86

nfs

[root@nfs01 backup]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password 
sending incremental file list
./
sys_backup_2017-10-14_6.tar.gz
sent 842 bytes  received 30 bytes  1744.00 bytes/sec
total size is 730  speedup is 0.84

backup服务器:

[root@backup backup]# ll
total 8
-rw-r--r-- 1 rsync rsync 1385 Oct 16 20:28 sys_backup_2017-10-16_1.tar.gz
-rw-r--r-- 1 rsync rsync  746 Oct 18 19:55 sys_backup_2017-10-18_3.tar.gz

5.1.4 删除大于7天命令测试

web01:

[root@web01 backup]# find /backup/ -type f -mtime +7 -name "*.tar.gz" |xargs rm -f

web02:

[root@web02 backup]# find /backup/ -type f -mtime +7 -name "*.tar.gz" |xargs rm -f

nfs

[root@nfs01 backup]# find /backup/ -type f -mtime +7 -name "*.tar.gz" |xargs rm -f

5.1.5 利用md5sum指纹进行验证

web01:

[root@web01 backup]# md5sum sys_backup_2017-10-16_1.tar.gz >/backup/finger.txt
[root@web01 backup]# md5sum -c finger.txt 
sys_backup_2017-10-16_1.tar.gz: OK

web02:

[root@web02 backup]# md5sum sys_backup_2017-10-18_3.tar.gz >/backup/finger.txt
[root@web02 backup]# md5sum -c finger.txt 
sys_backup_2017-10-18_3.tar.gz: OK
[root@nfs01 backup]# md5sum sys_backup_2017-10-14_6.tar.gz >/backup/finger.txt
[root@nfs01 backup]# md5sum -c finger.txt 
sys_backup_2017-10-14_6.tar.gz: OK

5.2 第二步、写脚本

5.2.1 web01:

[root@web01 backup]# vim /server/scripts/backup.sh
Backup="/backup"
Host_IP=`hostname -i`
Date_inof=`date +%F_%w -d "-1day"`   #因为要求晚上12点备份,如果不减一天,就是12点后一天,这样的话备份的文件就得减一天
#创建备份目录 
mkdir -p $Backup/$Host_IP    #按要求,在/backup下创建以备份服务器ip做名字的目录,好区分不同服务器发来的数据 
#打包备份
cd /
tar zchf $Backup/$Host_IP/sys_backup_${Date_inof}.tar.gz var/spool/cron/root etc/rc.local server/scripts/ etc/sysconfig/i
ptables    #因为/etc/rc.local是软连接,所以加一个h参数 
tar zcf $Backup/$Host_IP/www_${Date_inof}.tar.gz var/html/www/ app/logs/
#删除大于7天的数据
find $Backup -type f -mtime +7 -name "*.tar.gz" |xargs rm -f
#利用md5sum指纹进行检验
md5sum $Backup/$Host_IP/sys_backup_${Date_inof}.tar.gz $Backup/$Host_IP/www_${Date_inof}.tar.gz >$Backup/$Host_IP/finger.
txt
#推送数据
 rsync -az $Backup/$Host_IP rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

5.2.2 web02:

[root@web02 backup]# cat /server/scripts/backup.sh
#参数
Backup="/backup"
Host_IP=`hostname -i`
Date_inof=`date +%F_%w -d "-1day"`   #因为要求晚上12点备份,如果不减一天,就是12点后一天,这样的话备份的文件就得减一天
#1.备份目录创建
mkdir -p $Backup/$Host_IP
#2.打包备份配置文件
cd /
tar zchf $Backup/$Host_IP/sys_backup_${Date_inof}.tar.gz var/spool/cron/root etc/rc.local server/scripts/ etc/sysconfig/iptables
tar zcf $Backup/$Host_IP/www_${Date_inof}.tar.gz var/html/www/ app/logs/
#3.删除大于7天的数据
find $Backup -type f -mtime +7 -name "*.tar.gz" |xargs rm -f
#4.指纹验证信息
md5sum $Backup/$Host_IP/sys_backup_${Date_inof}.tar.gz $Backup/$Host_IP/www_${Date_inof}.tar.gz >$Backup/$Host_IP/finger.txt
#5.推送数据
rsync -az $Backup/$Host_IP rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

5.2.3 nfs服务器

[root@nfs01 data]# cat /server/scripts/backup.sh 
#参数
Backup="/backup"
Host_IP=`hostname -i`
Date_inof=`date +%F_%w -d "-1day"`   #因为要求晚上12点备份,如果不减一天,就是12点后一天,这样的话备份的文件就得减一天
#创建备份目录 
mkdir -p $Backup/$Host_IP   #按要求,在/backup下创建以备份服务器ip做名字的目录,好区分不同服务器发来的数据 
#打包备份
cd /
tar zchf $Backup/$Host_IP/sys_backup_${Date_inof}.tar.gz var/spool/cron/root etc/rc.local server/scripts/ etc/sysconfig/iptables #因为/etc/rc.local是软连接,所以加一个h参数 
#删除大于7天的数据
find $Backup -type f -mtime +7 -name "*.tar.gz" |xargs rm -f
#利用md5sum指纹进行检验
md5sum $Backup/$Host_IP/sys_backup_${Date_inof}.tar.gz >$Backup/$Host_IP/finger.txt
#推送数据
 rsync -az $Backup/$Host_IP rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

5.3 写定时任务

5.3.1 web01

[root@web01 backup]# crontab -l
#time sync by oldboy at 2010-2-1
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
#backup
00 00 * * * /bin/sh /server/scripts/backup.sh &>/dev/null

5.3.2 web02

[root@web02 backup]# crontab -l
#time sync by oldboy at 2010-2-1
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
#backup
00 00 * * * /bin/sh /server/scripts/backup.sh &>/dev/null

5.3.3 nfs服务器

[root@nfs01 data]# crontab -l
#time sync by oldboy at 2010-2-1
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
#backup
00 00 * * * /bin/sh /server/scripts/backup.sh &>/dev/null

5.4 backup备份服务器部署

5.4.1 指纹对不对

[root@backup 172.16.1.8]# md5sum -c finger.txt 
/backup/172.16.1.8/sys_backup_2017-10-12_4.tar.gz: OK
/backup/172.16.1.8/www_2017-10-12_4.tar.gz: OK
/backup/172.16.1.8/logs_2017-10-12_4.tar.gz: OK

5.4.2 发送检验邮件

实例5-1 邮件配置:

第一步、先对邮件服务的配置文件进行备份

[root@backup backup]# cp /etc/mail.rc{,.bak}

第二步、修改邮件服务配置文件

添加内容到最后就可以:

set from=zxpo@163.com smtp=smtp.163.com smtp-auth-user=zxpo smtp-auth-password=此处写授权密码 smtp-auth=login

第三步、进行服务的启动

[root@backup backup]# /etc/init.d/postfix restart
Shutting down postfix:                                     [  OK  ]
Starting postfix:                                          [  OK  ]

第四步、发送邮件进行测试

[root@backup backup]# echo "miss" |mail -s "ccc" 110@qq.com

5.4.3 删除180以前的数据文件,保留所有周一的文件

[root@backup backup]# find /backup/ -type f -mtime +180 ! -name "*_1.tar.gz"|xargs rm -f 

5.4.4 编写脚本

[root@backup backup]# vim /server/scripts/check.sh
#指纹验证
find /backup -type f -name "finger.txt" |xargs md5sum -c >/tmp/cyq_check.txt
#发送邮件信息
mail -s "Ccc" 110@qq.com </tmp/ccc_check.txt
#删除180天以前数据,保留所有周一数据
find /backup/ -type f -mtime +180 ! -name "*_1.tar.gz"|xargs rm -f

5.4.5 写定时任务

[root@backup backup]# crontab -e
#backup因为要等客户端晚上12点推送过来数据后在进行检查所以定时时间一定要在晚上12点以后
00 02 * * * /bin/sh /server/scripts/check.sh &>/dev/null