部署全网备份
全网备份方案
作者:牛耕田
日期:2017-10-14
一、 方案目的
实现全网自动备份并向指定的邮箱发送备份结果。
二、 方案要求
1、 标准化要求将所有的服务器备份配置在/backup/下
2、 标准化要求将执行脚本放置在/server/scripts/下
3、 每天备份系统配置文件、网站程序目录、访问日志并rsync推送到备份服务器,同时发送备份结果到指定的邮箱
4、 客户端清除7天以前的备份文件
5、 备份服务器上保留全网备份半年及半年前每个周一的备份文件
6、 备份服务器将客户端推送过来的备份文件进行检验,并将检验结果及/backup/目录下面文件结构发送到指定邮箱
三、 环境说明
|
服务端 |
客户端 |
操作系统 |
CentOS release 6.9 (Final) |
CentOS release 6.9 (Final) |
操作系统内核 |
2.6.32-696.el6.x86_64 |
2.6.32-696.el6.x86_64 |
四、 部署邮件服务
1) 安装mutt:yum install mutt –y
2) 配置邮件服务:echo 'set from=niugengtianyj@163.com smtp=smtp.163.com smtp-auth-user=niugengtianyj smtp-auth-password=********** smtp-auth=login' >>/etc/mail.rc
3) 开启邮件服务:/etc/init.d/postfix start 并echo '/etc/init.d/postfix' >>/etc/rc.local
五、 部署rsync服务端
1) 安装rsync:yum install rsync
2) 创建rsync用户:useradd –s /sbin/nolog -M rsync
3) 创建备份目录及修改属主:chown –R /backup
4) 配置服务端口令文件根据模块的设置:touch /etc/rsync.password && chmod 600 /etc/rsync.password 格式是 认证用户名:密码
5) 配置客户端口令文件: touch /etc/rsync.password && chmod 600 /etc/rsync.password 格式是 密码
6) 配置/etc/rsyncd.conf
#rsync_config
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
secrets file = /etc/rsync.password
[web01]
comment = "backup dir for web01"
path = /backup
auth users = web01_backup
[nfs01]
comment = "backup dir for nfs01"
path = /backup
auth users = nfs01_backup
[lb01]
comment = "backup dir for lb01"
path = /backup
auth users = lb01_backup
7) 创建口令文件/etc/rsync.password && chmod 600 /etc/rsync.password
8) 启动rsync及设置开机启动:rsync --daemon && echo ‘rsync –daemon’>/etc/rc.local
六、 分别设置本地备份脚本并写入开机启动
服务端:vim /server/scripts/Server_backup.sh
#!/bin/bash
#作者:牛耕田
#日期:2017-10-14
#说明:备份重要配置及文件夹备份好后发邮件
cd /
mkdir -p backup/`echo $(hostname -I;hostname)|awk '{print $2}'`
tar -zcf backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/`date +%F_%w -d "-1day"`.tar.gz etc/rc.local etc/profile var/spool/cron/root server/scripts/ var/html/www/ app/logs/ var/log/
#md5sum
touch /backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
md5sum /backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/`date +%F_%w -d "-1day"`.tar.gz >/backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
#delete 7day ago
#find /backup/`echo $(hostname -I;hostname)|awk '{print $2}'` -type f -mtime +7 |xargs rm -f
#display /backup/
tree /backup >>/backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
#send mail
mail -s "`hostname`本地备份结果" niugengtianyj@163.com </backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
客户端
#!/bin/bash
#作者:牛耕田
#日期:2017-10-14
#说明:备份重要配置及文件夹备份好后发邮件,并将文件推送到backup
cd /
mkdir -p backup/`echo $(hostname -I;hostname)|awk '{print $2}'`
tar -zcf backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/`date +%F_%w -d "-1day"`.tar.gz etc/rc.local etc/profile var/spool/cron/root server/scripts/ var/html/www/ app/logs/ var/log/
#md5sum
touch /backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
md5sum /backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/`date +%F_%w -d "-1day"`.tar.gz >/backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
#delete 7day ago
find /backup/`echo $(hostname -I;hostname)|awk '{print $2}'` -type f -mtime +7 |xargs rm –f
#display /backup/
tree /backup >>/backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
#send mail
mail -s "`hostname`本地备份结果" niugengtianyj@163.com </backup/`echo $(hostname -I;hostname)|awk '{print $2}'`/md5file.txt
#push to backupserver
echo $(hostname -I;hostname)|awk '{print "rsync -avzP /backup/ "$3"_backup@172.16.1.41::"$3,"--password-file=/etc/rsync.password"}'|bash
七、 服务端设置清理脚本并写入开机启动
vim /server/scripts/clear180daygao.sh
#!/bin/bash
#作者:牛耕田
#日期:2017-10-14
#说明:1、检验备份文件
# 2、删除大于180天的文件,但是保留每个周一的文件
# 2、将检验结果及/backup/下文件情况发送邮件
#检验md5
find /backup/ -type f -name "*.txt"|awk '{print "md5sum -c "$1}'|bash >/backup/`date -d "-1day" +%F`check.txt
#delete 180day ago
find /backup -type f -mtime +180 |egrep -v "*_1.tar.*"|xargs rm
#查看/backup/下的文件情况
tree /backup/ >>/backup/`date -d "-1day" +%F`check.txt
#send mail
mail -s "检验结果及/backup文件结构" niugengtianyj@163.com </backup/`date -d "-1day" +%F`check.txt
#delete 检验文件
rm -f /backup/`date -d "-1day" +%F`check.txt