测试NFS+rsync+sersync+SMTP+shell脚本+cron

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试NFS+rsync+sersync+SMTP+shell脚本+cron相关的知识,希望对你有一定的参考价值。

考核1

实现拓扑图:

技术分享图片

实验需要的机器:

名称

ip地址

主机名

web服务器

192.168.1.10

web01

rsync备份服务器

192.168.1.20

rsync

nfs存储服务器

192.168.1.30

nfs

1nfs创建一个目录名为/data/web的目录作为web服务器的后端存储,然后共享使用虚拟用户webuser,因为公司访问量不是很大,所以可以直接用sync来同步写入到磁盘

NFS服务器上:

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

/etc/init.d/rpcbind start && /etc/init.d/nfs start

mkdir -p /data/web

[[email protected] ~]# cat /etc/exports  #修改nfs配置文件

/data/web 192.168.1.0/24(rw,sync,root_squash,all_squash,anonuid=666,anongid=666)

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

chown -R webuser:webuser /data/web/

[[email protected] ~]# exportfs -arv

exporting 192.168.1.0/24:/data/web

 

#先进行本地挂载测试,如果成功在到远端挂载

[[email protected] ~]# showmount -e 192.168.1.30

Export list for 192.168.1.30:

/data/web 192.168.1.0/24

[[email protected] ~]# mount -t nfs 192.168.1.30:/data/web /mnt/

[[email protected] ~]# df -Th

Filesystem           Type   Size  Used Avail Use% Mounted on

/dev/mapper/vg_moban-lv_root

                     ext4    18G 1000M   16G   6% /

tmpfs                tmpfs  490M     0  490M   0% /dev/shm

/dev/sda1            ext4   477M   52M  400M  12% /boot

192.168.1.30:/data/web   nfs     18G 1000M   16G   6% /mnt

[[email protected] ~]# umount /mnt

web服务器挂载nfs用作网站的后端存储:

[[email protected] ~]# mkdir /webdata

[[email protected] ~]# mount -t nfs 192.168.1.30:/data/web /webdata/

[[email protected] ~]# df -Th

Filesystem           Type   Size  Used Avail Use% Mounted on

/dev/mapper/vg_moban-lv_root

                     ext4    18G  998M   16G   6% /

tmpfs                tmpfs  490M     0  490M   0% /dev/shm

/dev/sda1            ext4   477M   52M  400M  12% /boot

192.168.1.30:/data/web  nfs     18G 1000M   16G   6% /webdata

[[email protected] ~]# echo "mount -t nfs 192.168.1.30:/data/web /webdata/" >>/etc/rc.local  #设置成开机自动挂载

NFS共享挂载已经完成,然后下一步实施搭建rsync服务器

yum -y install rsync.x86_64

[[email protected] ~]# vim /etc/rsyncd.conf

uid = rsync

gid = rsync

use chroot = no

max connections = 20

list = false

timeout = 300

read only = false

pid file = /var/run/rsyncd.pid

lock file= /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

ignore errors

host allow = 192.168.1.0/24

[nfsbackup]

comment = This is an nfs real-time backup module

path = /backup/nfs

auth users = nfs_rsync

secrets file = /etc/rsync.password

[[email protected] ~]# mkdir -p /backup/nfs

[[email protected] ~]# useradd rsync -s /sbin/nologin -M

[[email protected] ~]# chown rsync:rsync /backup/nfs/

[[email protected] ~]# echo "nfs_rsync:nfspassword" >> /etc/rsync.password

[[email protected] ~]# chmod 600 /etc/rsync.password

[[email protected] nfs]# /usr/bin/rsync --daemon

[[email protected] nfs]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local

 

NFS充当客户端:

[[email protected] ~]# echo "nfspassword" >> /etc/rsync.password #生成密码文件  

[[email protected] ~]# chmod 600 /etc/rsync.password

[[email protected] ~]# yum -y install rsync.x86_64

[[email protected] ~]# rsync -avz /data/web/ [email protected]::nfsbackup/ --password-file=/etc/rsync.password

测试推送成功,

然后在NFS上的数据要实时的备份到rsync服务器上(sersync

[[email protected] ~]# mkdir /applocal/

[[email protected]~]# mv sersync2.5.4_64bit_binary_stable_final.tar.gz  /applocal/

[[email protected] ~]# cd /applocal/

[[email protected] applocal]# tar -xvzf sersync2.5.4_64bit_binary_stable_final.tar.gz

sersync /

sersync /bin/sersync2

sersync/conf/confxml.xml

[[email protected] sersync]# tree /applocal/sersync

/applocal/sersync

├── bin

   └── sersync2

└── conf

    └── confxml.xml

 

2 directories, 2 files

[[email protected] conf]# cp confxml.xml{,.bak}

[[email protected] conf]# vim confxml.xml

    <sersync>

    <localpath watch="/data/web/">

        <remote ip="192.168.1.20" name="nfsbackup"/>

        <!--<remote ip="192.168.8.39" name="tongbu"/>-->

        <!--<remote ip="192.168.8.40" name="tongbu"/>-->

    </localpath>

    <rsync>

        <commonParams params="-az"/>

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

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

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

        <ssh start="false"/>

    </rsync>

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

60mins execute once-->

 

[@nfs /]# /applocal/sersync/bin/sersync2 -dro /applocal/sersync/conf/confxml.xml

[[email protected] conf]# cd /data/web/

[[email protected] web]# touch 1

[[email protected] web]# echo " /applocal/sersync/bin/sersync2 -dro /applocal/sersync/conf/confxml.xml" >>/etc/rc.local

 

#然后实行全网备份脚本:

备份每台服务器里面的:/etc/rc.local    /etc/hosts    /scripts

备份数据在本地保存7天,在rsync上保存180天,星期六的数据不删除

如果备份成功发送邮件到[email protected]

1)先搭建邮件服务,用SMTP

[[email protected] etc]# yum -y install mailx.x86_64

[[email protected] etc]# vim /etc/mail.rc

##邮件服务,利用qq.com来发送邮件

set [email protected]

set smtp=smtps://smtp.qq.com:465

set [email protected]

set smtp-auth-password=dcnhslrkexdbbgbh

set smtp-auth=login

set ssl-verify=ignore

set nss-config-dir=/etc/pki/nssdb

[[email protected] etc]# echo 123 | mail -v -s "test Email" [email protected]

 

然后开始写备份脚本:

rsync配置文件里面增加:

[networkbackup]

comment = This is a full network backup module

path = /backup

auth users = rsync_backup

secrets file = /etc/rsyncn.password

[[email protected] etc]# chown rsync:rsync /backup/

[[email protected] etc]# echo "rsync_backup:rsync_backup" >>/etc/rsyncn.password

[[email protected] etc]# chmod 600 /etc/rsyncn.password

 

[[email protected] backup]# vim /scripts/nfs_backup.sh

#!/bin/bash

#创建一个备份使用的脚本,要求如下:

#1,使用日期作为文件名的一部分

#2,打包备份/etc/rc.local and /etc/exports and /scripts/ and /etc/crontab

#3,备份数据在本地只保留7

#4,备份数据都放在/backup/目录下

#5,推送到备份服务器上要是用自己的ip地址作为目录

 

#每天00点打包文件,然后推送,这时候文件名日期应该是前一天的日期,截取前一天的日期

Time=$(/bin/date +"%Y-%m-%d" -d "-1 days")

Ip=$(/sbin/ifconfig eth3|awk -F "[ :]+" 'NR==2 {print $4}' )

[ ! -d /backup/$Ip ] && mkdir -p /backup/$Ip

Path=/backup/$Ip

tar -czf $Path/$Time\.tar.gz /etc/rc.local /etc/exports /scripts/ /etc/crontab \

      && md5sum $Path/$Time\.tar.gz >$Path/file.md5

rsync -az /backup/ [email protected]::networkbackup --password-file=/etc/rsyncn.password

#然后删除七天以前的文件

find $Path -mtime +7 -type f |xargs -i rm -rf {}

 

[[email protected] ~]# crontab -l -u root

00 * * * * /bin/sh /scripts/nfs_backup.sh

 

#!/bin/bash

#先找出180天以前的文件,进行删除,因为/backup目录下有一个nfs目录是nfs的备份文件所以它里面的内容不>

能动,要给他排除掉

for i in  `find /backup/ -type d ! -name nfs`

do

    #进行文件完整性校验

    [ -f $i/file.md5 ] && \

    if [ $(md5sum $i/file.md5 | awk -F "[: ]+" '{print $2}') = 'OK' ]

        then

        echo "文件备份成功" |mail -s "文件备份" [email protected]

    fi

    #删除80天以前的内容

    find $i -type f -mtime +180 | xargs -i rm -rf {}

done

[[email protected] 192.168.1.30]# crontab -l -u root

00 * * * * /bin/sh /scripts/backup_del_md5.sh


以上是关于测试NFS+rsync+sersync+SMTP+shell脚本+cron的主要内容,如果未能解决你的问题,请参考以下文章

rsync+nfs+sersync实战案例

rsync+sersync+nfs高可用

sersync实现实时同步

sersync实现实时同步

使用ansible自动化部署nfs+rsync+sersync+web01自动化挂载

rsync+sersync实现数据时时备份