Rsync-远程同步(下)-企业案例
已知3台服务器主机名分别为web01、backup 、nfs主机信息见下表: 角色 外网IP(NAT) 内网IP(LAN) 主机名 WEB eth0:10.0.0.7 eth1:172.16.1.7 web01 NFS eth0:10.0.0.31 eth1:172.16.1.31 nfs Rsync eth0:10.0.0.41 eth1:172.16.1.41 backup 客户端: web nfs 服务端: backup
服务端(Server)部署Rsync#1.安装 [[email?protected] ~]# yum install rsync -y #2.配置 [[email?protected] ~]# vim /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 #3.根据配置创建一些环境 3.1创建rsync用户 [[email?protected] ~]# groupadd rsync [[email?protected] ~]# useradd rsync -M -s /sbin/nologin -g rsync [[email?protected] ~]# id rsync uid=1000(rsync) gid=1000(rsync) groups=1000(rsync) 3.2创建虚拟用户和密码 [[email?protected] ~]# echo "rsync_backup:123456" > /etc/rsync.passwd [[email?protected] ~]# chmod 600 /etc/rsync.passwd 3.3创建backup目录 [[email?protected] ~]# mkdir /backup [[email?protected] ~]# chown -R rsync.rsync /backup/ #4.启动服务 [[email?protected] ~]# systemctl start rsyncd [[email?protected] ~]# systemctl enable rsyncd #5.检查 [[email?protected] ~]# netstat -lntup |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1594/rsync tcp6 0 0 :::873 :::* LISTEN 1594/rsync Client端操作: 快递: 散货-------->打包--->标记------->装车-------------->运输------------->仓库 验货-------->评价 系统: 备份的文件-->打包--->校验值----->备份至本地目录---->网络-------->推送至备份服务器 管理员校验-->通知--->邮件 #创建存放脚本的位置: [[email?protected] ~]# mkdir -p /server/scripts/ #根据要求遍写脚本: [[email?protected] ~]# vim /server/scripts/push_data_server.sh #!/usr/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin Path=/backup Host=$(hostname) Addr=$(ifconfig eth1 |awk 'NR==2 {print $2}') Date=$(date +%F) Dest=${Host}_${Addr}_${Date} #1.创建存放备份的目录 [ -d $Path/$Dest ] || mkdir -p $Path/$Dest #2.打包本地文件至备份目录 cd / && [ -f $Path/$Dest/sys.tar.gz ] || tar czf $Path/$Dest/sys.tar.gz etc/fstab etc/hosts etc/passwd && [ -f $Path/$Dest/other.tar.gz ] || tar czf $Path/$Dest/other.tar.gz var/spool/cron/ && #3.增加标记 md5sum $Path/$Dest/*.tar.gz > $Path/$Dest/flag_${Date} #4.将备份的数据推送至远程服务器 export RSYNC_PASSWORD=123456 rsync -avz $Path/ [email?protected]::backup #5.客户端服务器本地保留最近7天的数据 find $Path/ -type d -mtime +7 | xargs rm -rf ------------------------------------------------------------------------------------------------------ ##批量的模拟数据 [[email?protected] ~]# for i in {1..30};do date -s "201909$i"; sh /scripts/clinet_push_data_server.sh ; ==Server端操作:== ------------------服务端配置邮件功能-------------------- 1.安装 [[email?protected] ~]# yum install mailx -y 2.配置 [[email?protected] ~]# vim /etc/mail.rc //最后一行下边添加如下内容: set [email?protected] //发件人QQ set smtp=smtps://smtp.qq.com:465 set [email?protected] //发件人QQ set smtp-auth-password=xxxxxxxxxxx //QQ授权码 set smtp-auth=login set ssl-verify=ignore set nss-config-dir=/etc/pki/nssdb/ 3.测试一下是否能发送成功 [[email?protected] ~]# mail -s "Rsync Backup" [email?protected] < /etc/hosts 4.编写脚本 [[email?protected] ~]# mkdir -p /server/scripts [[email?protected] ~]# vim /server/scripts/check_data_notify.sh #!/usr/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin Path=/backup Date=$(date +%F) #1.校验客户端推送过来的数据 md5sum -c ${Path}/*_${Date}/flag_${Date} > ${Path}/result_${Date} #2.将校验的结果通知给管理员 mail -s "Rsync Backup ${Date}" [email?protected] < ${Path}/result_${Date} #3.服务端保留最近180天的备份数据 find $Path/ -type d -mtime +180 | xargs rm -rf #定时任务: 客户端 [[email?protected] ~]# crontab -l #客户端每天凌晨1点定时执行该脚本 */2 * * * * sh /server/scripts/push_data_server.sh &> /dev/null 服务端 [[email?protected] ~]# crontab -l #定时校验备份的结果 */2 * * * * sh /server/scripts/check_data_notify.sh &> /dev/null 如何增加多台及上千台客户端服务器备份数据? [[email?protected] ~]# rsync -avz [email?protected]:/server / [[email?protected] ~]# rsync -avz [email?protected]:/server / [[email?protected] ~]# rsync -avz [email?protected]:/server / [[email?protected] ~]# rsync -avz [email?protected]:/server / [[email?protected] ~]# rsync -avz [email?protected]:/server / ...................................................... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |