一、应用场景
备份公司的重要的文件或者日志文件,系统配置文件等
二、脚本思路
1、本地打包备份
2、同步服务推送至备份服务器
3、同步成功发送邮件提醒管理员
4、编写计划任务
5、本地备份文件保留时间,服务器备份文件保留时间
三、项目实施
1、服端配置rsync服务
步骤一:安装rsync服务
[root@node01 ~]# yum install rsync.x86_64 -y
步骤二:修改配置文件
uid= rsync
gid= rsync
usechroot = no
maxconnections = 200
timeout= 300
pidfile = /var/run/rsyncd.pid
lockfile = /var/run/rsync.lock
logfile = /var/log/rsyncd.log
[data]
comment= It's my test data!
path= /data/
ignoreerrors
readonly = false
list= false
hostsallow = 192.168.11.0/24
hostsdeny = 0.0.0.0/32
authusers = rsync_backup
secretsfile = /etc/rsyncd.passwd
步骤三:准备用户授权
[root@node01 ~]# mkdir /data
[root@node01 ~]# useradd rsync -s /sbin/nologin -M
[root@node01 ~]# chown -R rsync.rsync /data/
步骤四:配置用于同步的密码文件
[root@node01 ~]# echo "rsync_backup:123456" >> /etc/rsyncd.passwd
[root@node01 ~]# cat /etc/rsyncd.passwd
rsync_backup:123456
[root@node01 ~]# chmod 600 /etc/rsyncd.passwd
[root@node01 ~]# ll /etc/rsyncd.passwd
-rw------- 1 root root 20 Apr 8 18:30 /etc/rsyncd.passwd
步骤五:启动服务并设置为开机自启动
[root@node01 ~]# systemctl enable --now rsyncd
2、客户端配置rsync服务
步骤一:安装rsync服务
[root@node01 ~]# yum install rsync -y
步骤二:写入密码文件
[root@node02 ~]# echo "123456" >> /etc/rsyncd.passwd
[root@node02 ~]# cat /etc/rsyncd.passwd
123456
[root@node02 ~]# chmod 600 /etc/rsyncd.passwd
3、编写脚本
示例代码:
[root@node01 ~]# cat web_bak.sh
#!/bin/bash
#define var
# 取IP地址,这里用的VMware做的,网卡会和服务器网卡不一样
IP=`ifconfig ens32 | awk -F '[ :]+' 'NR==2 {print $3}'`
# 接收当前系统时间
Today=$(date +%F)
# 定义备份路径
Path=/backup/$IP
# 配置文件 定时任务服务 开机自启动 防火墙iptables
COFING_data="var/spool/cron/root etc/rc.local etc/sysconfig/iptablesServer/Scripts/"
# web服务器数据
WEB_data=var/www/html
# 日志
WEB_log=var/log/httpd
# 备份后的名称
BAK_CONFIG_NAME=config.$Today.tar.gz
BAK_WEB_DATA_NAME=web_data.$Today.tar.gz
BAK_WEB_LOG_NAME=web_log.$Today.tar.gz
# 备份服务器地址
BAK_Server=192.168.11.111
Flag_file=flag.$Today.txt
#backup
[ -d $Path ] || mkdir -p $Path
cd / && \
tar czf $Path/${BAK_CONFIG_NAME} ${COFING_data} && \
tar czf $Path/${BAK_WEB_DATA_NAME} ${WEB_data} && \
tar czf /$Path/${BAK_WEB_LOG_NAME} ${WEB_log}
#check backup && make Flag_file
if [ $? == 0 ]
then
find $Path -type f -name "*$Today.tar.gz" | xargs md5sum >> $Path/${Flag_file}
else
echo "backup Failure" >> $Path/${Flag_file}
exit 1
fi
#to backup server
rsync -az /backup/ rsync_backup@${BAK_Server}::backup --passwordfile=/etc/rsyncd.passwd
#del local backup file,Keep a week data
find $Path -type f -name "*.tar.gz" -mtime +7 | xargs rm -f
4、服务器端检查邮件告警脚本在服务端完成
[root@node01 ~]# cat check_web.sh
#!/bin/bash
#define var
Today=$(date +%F)
Flag_file=/backup/192.168.11.111/flag.$Today.txt
Mail_file=/opt/mail_bak_${Today}.txt
#check flag
if [ ! -f ${Flag_file} ]
then
echo "backup is error,pls view back server" > ${Mail_file}
else
find /backup -type f -name "flag.$(date +%F)*" | xargs md5sum -c | grep FAILED > ${Mail_file}
fi
#check Mail_file
if [ -s ${Mail_file} ]
then
mail -s "$(date +%F-%T) back" [email protected] <${Mail_file}
else
echo "backup is success!"
fi
#del remote bak file,30 days keep data
find /backup -type f -name "*.tar.gz" -mtime +30 | xargs rm -f
标签:rsync,root,备份,全网,CentOS7,etc,file,rsyncd,backup
From: https://www.cnblogs.com/sre-chan/p/17298854.html