服务端备份环境及代码
环境 配置/etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes :假的超级管理员
use chroot = no :用户更改根目录
max connections = 200:最大的连接数
timeout = 600 :超时时间
ignore errors:忽略错误
read only = false
list = false auth
users = rsync_backup :授权
secrets file = /etc/rsync.passwd :密码
log file = /var/log/rsyncd.log
####################################
#模块
[backup]
comment = welcome to oldboyedu
backup! path = /backup :路径
根据配置文件创建相应环境
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#1.创建组
groupadd rsync
#2.创建用户
useradd rsync -M -s /sbin/nologin -g rsync
#3.为虚拟用户设置密码
echo "rsync_backup:123456" >/etc/rsync.passwd
chmod 600 /etc/rsync.passwd
#4.创建模块目录
mkdir /backup chown -R rsync.rsync /backup/
#5.启动服务 s
ystemctl start rsyncd systemctl enable rsyncd
客户端备份代码
#! /usr/bin/bash #设置全局变量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Src=/backup
Host=$(hostname)
Addr=$(ifconfig eth1|awk 'NR==2 {print $2}')
Date=$(date +%F)
Dest=${Host}*${Addr}*${Date}
#1.创建对应的目录
#mkdir /backup
#mkdir /backup/$(hostname)*$(ifconfig eth1|awk 'NR==2 {print $2}')*$(date +%F)
#mkdir -p ${Src}/${Host}*${Addr}*${Date}
[ -d ${Src}/${Dest} ] || mkdir -p ${Src}/${Dest}
#2.装车,打包
#将/etc/passwd /etc/shadow打包至${Src}/${Dest}/下叫*.tar.gz
[ -f ${Src}/${Dest}/sys.tar.gz ] || tar czf ${Src}/${Dest}/sys.tar.gz /etc/passwd /etc/shadow
[ -f $++{Src}/${Dest}/cron.tar.gz ] || tar czf ${Src}/${Dest}/cron.tar.gz /var/spool/cron
#3.检测(将校验的值重定向至flag_每日时间)实现增量
[ -f ${Src}/${Dest}/flag*${Date}] || md5sum ${Src}/${Dest}/*.tar.gz > ${Src}/${Dest}/flag*${Date}
#4.输送
export RSYNC_PASSWORD=123456
rsync -avz ${Src}/ rsync_backup@172.16.1.41::backup
#5.保留最近7天的内容
find ${Src}/ -type d -mtime +7 --delete
服务端备份代码
#! /usr/bin/bash #设置全局变量
PATH= /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin Src=/backup Date=$(date +%F)
#1.校验数据
md5sum -c ${Src}/*_${Date}/flag_${Date} >${Src}/result_${Date}
#2.发送邮件
mail -s "backup" [2450738951@qq.com](mailto:2450738951@qq.com) < ${Src}/result_${Date}
#配置邮件
[root@backup /]# yum install mailx -y
[root@backup /]# vim /etc/mail.rc
set from=发件人@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=发件人@qq.com
set smtp-auth-password=授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/