备份项目:
项目背景
徐sir:网站数据需要备份,你去做下!
rsync服务端部署并调试
#服务端的存储备份数据的目录
[root@backup01 /]# ll
total 20
drwxr-xr-x 4 rsync rsync 43 May 1 16:51 backup
#配置服务端配置文件
[root@backup01 /]# cat /etc/rsyncd.conf
#rsyncd.conf start
#author:Allen
fake super =yes
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
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
######################################
[data]
comment = allen
path = /data
[backup]
comment = gather backups
path=/backup/
[root@backup01 ~]# cat /etc/rsync.password
rsync_backup:123456
systemctl restart rsyncd
rsync客户端节点配置脚本
[root@nfs01 ~]# cat /server/scripts/bak_conf.sh
#!/bin/bash
#auther:allen
#desc:客户端执行备份的脚本
#1.变量
ip=`hostname -I |awk '{print $2}'`
backup_dir=/backup/${ip}
time=`date +%F_%w`
backup_file=conf-${time}.tar.gz
backup_user=rsync_backup
backup_server=172.16.1.41
backup_target_dir=backup
password_file=/etc/rsync.client.password
#2.备份命令
mkdir -p ${backup_dir}
tar zcf ${backup_dir}/${backup_file} /etc/ /var/spool/cron/
#3.推送命令
rsync -a ${backup_dir} ${backup_user}@${backup_server}::${backup_target_dir} --password-file=${password_file}
#4.删除旧的备份
find ${backup_dir} -type f -name "*.tar.gz" -mtime +7 | xargs rm -f
#配置定时任务
[root@nfs01 ~]# crontab -l
#1. sync time
*/2 * * * * /sbin/ntpdate ntp1.aliyun.com &>/dev/null
#2.backup sys conf 每天午夜自动备份
00 00 * * * sh /server/scripts/bak_conf.sh &>/dev/null
在备份服务器上检查并发送结果邮件
[root@backup01 ~]# cat /server/scripts/check_backup.sh
#!/bin/bash
#author:Allen
#desc:检查备份文件,清理旧备份文件
#1:清理旧的备份
find /backup -type f -name "*.tar.gz" -mtime +180 |xargs rm -f
#2:统计备份结果
find /backup/ -type f -name "*.tar.gz" | xargs \
ls -lhd | \
awk -F '[ /]+' 'BEGIN{print "客户端","压缩包","大小"}{print $(NF-1),$NF,$5}' | \
column -t >/server/scripts/back_result.txt
#3:发送邮件 修改自己的邮箱
mail -s "每日备份结果" xxxxxxx@qq.com </server/scripts/back_result.txt
#定时任务
[root@backup01 ~]# crontab -l
#1. sync time
*/2 * * * * /sbin/ntpdate ntp1.aliyun.com &>/dev/null
#2.定时运行检查备份任务
00 00 * * * sh /server/scripts/check_backup.sh &>/dev/null
添加md5校验压缩包
客户端:
[root@nfs01 /var/spool/cron]# cat /server/scripts/bak_conf.sh
#!/bin/bash
#auther:allen
#desc:备份服务客户端的配置文件
#1.变量
ip=`hostname -I |awk '{print $2}'`
backup_dir=/backup/${ip}
time=`date +%F_%w`
backup_file=conf-${time}.tar.gz
backup_user=rsync_backup
backup_server=172.16.1.41
backup_target_dir=backup
password_file=/etc/rsync.client.password
#2.备份命令
mkdir -p ${backup_dir}
tar zcf ${backup_dir}/${backup_file} /etc/ /var/spool/cron/
#3.md5 加密 防止压缩包备份有问题
md5sum ${backup_dir}/${backup_file} >${backup_dir}/bakfinger.md5
#4.推送命令
rsync -a ${backup_dir} ${backup_user}@${backup_server}::${backup_target_dir} --password-file=${password_file}
#5.删除旧的备份
find ${backup_dir} -type f -name "*.tar.gz" -mtime +7 | xargs rm -f
服务端
#!/bin/bash
#author:Allen
#desc:备份服务服务端的配置文件 检查备份文件,清理旧备份文件
#变量
result=/server/scripts/back_result.txt
#1:清理旧的备份
find /backup -type f -name "*.tar.gz" -mtime +180 |xargs rm -f
#2:统计备份结果
find /backup/ -type f -name "*.tar.gz" | xargs \
ls -lhdt | \
awk -F '[ /]+' 'BEGIN{print "客户端","压缩包","大小"}{print $(NF-1),$NF,$5}' | \
column -t >${result}
du -sh /backup/ >>${result}
#3:md5校验 以防压缩包有问题
echo '#md5校验信息:有FAILED 请重视,无信息请忽略' >>${result}
md5sum --quiet -c /backup/*/bakfinger.md5 >>${result}
#4:发送邮件
mail -s "每日备份结果" allenxu95@qq.com <${result}
每日发送的邮件信息: