某公司里有一台NFS服务器,里面的数据很重要,但是如果硬盘坏了,数据就会丢失,现在领导要求你把数据在其他 机器上做一个周期性定时备份。要求如下:
每天晚上00点整在NFS服务器nfs01上打包备份网站程序目录等并通过rsync命令推送到备份服务器backup上备份保存(备份思路 可以是先在本地按IP地址+日期打包,然后再利用rsync推送到备份服务器上)。
具体要求如下:
1)NFS服务器nfs01和备份服务器backup的备份目录必须都为/backup;
2)NFS服务器站点目录假定为(/var/www/html);
3)NFS服务器脚本目录假定为(/server/scripts);
4)NFS服务器站点日志目录假定为(/app/logs);
5)NFS服务器个人配置文件目录路径(/etc/rc.d/rc.local);
6)NFS服务器定时任务文件路径为(/var/spool/cron/root);
7)将以上文件或目录打包(2-6条所示)推送到备份服务器backup ;
8)NFS服务器本地仅保留7天内的备份;
9)备份服务器上检查备份结果是否正常,并将每天的备份结果发给管理员信箱;
10)备份服务器上每周六的数据都保留,其他备份仅保留180天备份。
================================================以下为答案===========================================================================
一、主机网络参数
主机名 网卡eth0 网卡eth1 用途
backup 10.0.0.41 172.16.1.41 rsync服务端
nfs01 10.0.0.31 172.16.1.31 NFS存储服务器客户端
二、开始部署backup服务器
1、Rsync服务端:
(1)检查rsync软件是否安装:
[[email protected] ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
(2)编写rsyncd.conf文件
########################rsync_configstart#######################################################################################################
#created by ywx 2019-03-20
#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/rsync.lock
#日志文件位置
log file = /var/log/rsyncd.log
############################################上面的内容可以放在模块下,同理模块下的内容也可以放在全局################################
[backup]
#使用目录
path = /backup/
#有错误时忽略
ignore errors
#可读可写(true或false)(yes或no)
read only = false
#阻止远程列表(不让通过远程方式看服务端有啥)
list = false
#允许IP
hosts allow = 172.16.1.0/24
#禁止IP
hosts deny = 0.0.0.0/32
#虚拟用户(认证用户)
auth users = rsync_backup
#存放用户和密码的文件
secrets file = /etc/rsync.password
###################################################rsync_configend__end#########################################################################################
(3)创建rsync账户及共享目录并修改目录属主为rsync
[[email protected] ~]# useradd -M -s /sbin/nologin rsync
[[email protected] ~]# mkdir /backup
[[email protected] ~]# chown -R rsync.rsync /backup
(4)启动Rsync服务并设置为开机启动
启动rsync服务
[[email protected] ~]# rsync --daemon
[[email protected] ~]# netstat -lntup | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1264/rsync
tcp 0 0 :::873 :::* LISTEN 1264/rs
开机启动在/etc/rc.loacl下添加
rsync --daemon
(5)创建rsync虚拟账户名和密码,并赋予密码文件600权限
[[email protected] ~]# echo "rsync_backup:ywxking" > /etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
rsync_backup:ywxking
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 21 Mar 20 10:40 /etc/rsync.password
三、在NFS上部署Rsync客户端
(1)检查rsync软件是否安装:
[[email protected] ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
(2)只需要创建密码文件(只包含密码即可),并赋予密码文件600权限
[[email protected] ~]# echo "ywxking" > /etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password
(3)创建共享目录backup
[[email protected] ~]# mkdir /backup
(4)在客户端进行推送测试
[[email protected] backup]# touch ywx{1..10}
[[email protected] backup]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx1
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx10
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx2
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx3
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx4
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx5
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx6
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx7
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx8
-rw-r--r-- 1 root root 0 Mar 20 22:12 ywx9
[[email protected] backup]# rsync -avzP /backup/ [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
./
ywx1
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=9/11)
ywx10
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=8/11)
ywx2
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=7/11)
ywx3
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=6/11)
ywx4
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=5/11)
ywx5
0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=4/11)
ywx6
0 100% 0.00kB/s 0:00:00 (xfer#7, to-check=3/11)
ywx7
0 100% 0.00kB/s 0:00:00 (xfer#8, to-check=2/11)
ywx8
0 100% 0.00kB/s 0:00:00 (xfer#9, to-check=1/11)
ywx9
0 100% 0.00kB/s 0:00:00 (xfer#10, to-check=0/11)
sent 462 bytes received 201 bytes 1326.00 bytes/sec
total size is 0 speedup is 0.00
(5)nfs01服务器打包脚本nfsbak.sh
vim /server/scripts/nfsbak.sh
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
######自定义变量
Path=/backup
backup_Server=172.16.1.41
IP=`/sbin/ifconfig eth1|awk -F"[ :]+" ‘NR==2{print $4}‘`
Dir=${IP}_$(date +%F_%w)
######如果需要备份的文件和目录存在,则复制到/backup下面相对的目录中
mkdir -p $Path/$Dir
[ -f /var/spool/cron/root ] && cp -rp /var/spool/cron/root $Path/$Dir/
[ -f /etc/rc.d/rc.local ] && cp -rp /etc/rc.d/rc.local $Path/$Dir/
[ -d /server/scripts ] && cp -rp /server/scripts $Path/$Dir/
[ -d /var/html/www ] && cp -rp /var/html/www $Path/$Dir/
[ -d /app/logs ] && cp -rp /app/logs $Path/$Dir/
[ -f /etc/sysconfig/iptables ] && cp -rp /etc/sysconfig/iptables $Path/$Dir/
#######进入/backup目录并打包相应的目录文件
cd $Path
tar -zcf $Path/${Dir}.tar.gz $Dir
#######打包完成后,删除源目录
rm -rf $Path/$Dir
#######创建md5sum验证信息,方便backup服务器检验数据是否完成
/usr/bin/md5sum $Path/${Dir}.tar.gz > $Path/md5sum_${IP}.txt
#######推送打包的文件及md5sum校验值到备份服务器
rsync -avz $Path/ [email protected]${backupServer}::backup --password-file=/etc/rsync.password
#######找出超过7天的备份并删除
find $Path/ -name "${IP}*" -type f -mtime +7 | xargs rm -rf
(6)nfs备份的定时任务
00 00 * * * /bin/sh /server/scripts/nfsbak.sh
四、备份服务器上检查备份结果是否正常,并将每天的备份结果发给管理员信箱;
因为nfs服务器备份过来的文件都有md5sum,因此文件备份是否正常,只需要md5sum的值一样
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
#####找出1天内的备份文件的md5sum校验和
md5=$(/bin/find /backup/ -type f -mtime -1 -name "md5sum_*.txt")
for i in $md5
do
md5sum -c $md5 >/dev/null 2>&1
md5jg=$(echo $?)
if [ ${md5jg} -eq 0 ];then
mail -s "$(date +%F)This is backup OK" [email protected]
else
mail -s "$(date +%F)This is backup Worry" [email protected]
done
五、备份服务器上每周六的数据都保留,其他备份仅保留180天备份
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
find /backup ! -name "*_6.tar.gz" ! -name "md5sum*_6.txt" -mtime +180 -type f | xargs rm -rf
六、backup服务器的定时任务
##################################定时任务脚本######################################################
vim /server/scripts/backup.sh
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
md5=$(/bin/find /backup/ -type f -mtime -1 -name "md5sum_*.txt")
for i in $md5
do
md5sum -c $md5 > /dev/null 2>&1
md5jg=$(echo $?)
if [ ${md5jg} -eq 0 ];then
mail -s "$(date +%F) This is backup Ok!!!" [email protected]
else
mail -s "$(date +%F) This is backup Worry!!!" [email protected]
done
find /backup ! -name "*_6.tar.gz" ! -name "md5sum*_6.txt" -mtime +180 -type f | xargs rm -rf
#####################################################################################################
##############################定时任务#########################################################
00 00 * * * /bin/sh /server/scripts/backup.sh
七、ansible一键部署rsync
1、主机网络参数:
主机 网卡 ip地址 用途
m01 eth1 172.16.1.61 管理主机
backup01 eth1 172.16.1.41 backup服务器
nfs01 eth1 172.16.1.31 nfs服务器
2、在m01管理主机上的准备工作:
(1)编写ssh分发脚本,让m01可以免交互式登录backup和nfs服务器
#!/bin/bash
#ssh connect
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
rm -rf /root/.ssh/id_dsa
#####创建免交互密钥
ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
yum install -y sshpass
#####免交互推送m01的密钥
for ip in 41 31
do
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no 172.16.1.$ip"
done
(2)安装ansible
服务端:
yum install -y ansible
客户端:(可选)
yum install -y libselinux-python
(3)编写rsync剧本:rsync.yaml
============================================rsync.yaml__start==============================================================
#command play-book
- hosts: rsync_server
tasks:
- name: step01:install rsync
yum: name=rsync state=installed
- name: step02:edit rsync conf file
copy: src=/etc/ansible/conf/rsync_conf/rsyncd.conf dest=/etc/
- name: step03:create rsync user
user: name=rsync state=present createhome=no shell=/sbin/nologin
- name: step04:create auth file
copy: src=/etc/ansible/conf/rsync_conf/rsync.password dest=/etc/ mode=600
- name: step05:create backup directory
file: dest=/backup state=directory owner=rsync group=rsync
- name: step06:start rsync server
shell: rsync --daemon creates=/var/run/rsyncd.pid
- hosts: rsync_client
tasks:
- name: step01:create auth file
copy: src=/etc/ansible/conf/rsync_conf/rsync_client.password dest=/etc/rsync.password mode=600
============================================rsync.yaml__end==============================================================
(4)测试剧本
[[email protected] rsync_conf]# ansible-playbook -C /server/scripts/rsync_install.yaml
PLAY [rsync_server] ***************************************
TASK [Gathering Facts] ************************************
ok: [172.16.1.41]
TASK [step01:install rsync] ***********************************
ok: [172.16.1.41]
TASK [step02:edit rsync conf file] ********************************
ok: [172.16.1.41]
TASK [step03:create rsync user] ***********************************
ok: [172.16.1.41]
TASK [step04:create auth file] ************************************
ok: [172.16.1.41]
TASK [step05:create backup directory] *********************************
ok: [172.16.1.41]
TASK [step06:start rsync server] **********************************
skipping: [172.16.1.41]
PLAY [rsync_client] ***************************************
TASK [Gathering Facts] ************************************
ok: [172.16.1.7]
ok: [172.16.1.31]
TASK [step01:create auth file] ************************************
changed: [172.16.1.7]
ok: [172.16.1.31]
PLAY RECAP ****************************************
172.16.1.31 : ok=2 changed=0 unreachable=0 failed=0
172.16.1.41 : ok=6 changed=0 unreachable=0 failed=0
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0
3、一键部署:
[[email protected] rsync_conf]# ansible-playbook /server/scripts/rsync_install.yaml
八、rsync开机自启动脚本(网上查找的)
制作服务启动脚本/etc/init.d/rsyncd
#!/bin/bash
#author:Mr.chen
# chkconfig:35 13 91
# description:This is Rsync service management shell script
# Source function library
. /etc/rc.d/init.d/functions
start(){
rsync --daemon
if [ $? -eq 0 -a `ps -ef|grep -v grep|grep rsync|wc -l` -gt 0 ];then
action "Starting Rsync:" /bin/true
sleep 1
else
action "Starting Rsync:" /bin/false
sleep 1
fi
}
stop(){
pkill rsync;sleep 1;pkill rsync
if [ `ps -ef|grep -v grep|grep "rsync --daemon"|wc -l` -lt 1 ];then
action "Stopping Rsync: " /bin/true
sleep 1
else
action "Stopping Rsync:" /bin/true
sleep 1
fi
}
case "$1" in
start)
start;
;;
stop)
stop;
;;
restart|reload)
stop;
start;
;;
*)
echo $"Usage: $0 {start|stop|restart|reload}"
;;
esac
6)启动脚本放到/etc/init.d目录下,增加x执行权限并添加开机启动
[[email protected] ~]# chmod +x /etc/init.d/rsyncd
[[email protected] ~]# /etc/init.d/rsyncd stop
已终止
[[email protected] ~]# /etc/init.d/rsyncd start
Starting Rsync: [确定]
[[email protected] ~]# chkconfig rsyncd on
[[email protected] ~]# chkconfig --list | grep rsync
rsyncd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
原文地址:https://www.cnblogs.com/yaokaka/p/11620766.html