Rsync- remote synchronization (lower) - Business Case

Known 3 server host names are web01, backup, nfs host information in the table below:

角色           外网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
  • == Client Client Requirements: *
    1. The client prepared in advance stored in the backup directory, the directory rules are as follows: /backup/nfs_172.16.1.31_2019-09-06
    2. client locally packaged backup (system configuration files, application configuration, etc.) to copy /backup/nfs_172.16.1.31_2019-09-06
    3. Finally, the backed up client data pushed to the backup server
    4. the client server's local data retention last 7 days, to avoid waste of disk space
    5. client daily 1:00 timed execution of the script

  • == Server server requirements: *
    1. server deployment rsync, for receiving client push over backup data
    2. The server needs to check clients every day to push over the data is complete
    3. The server needs to be checked every day result is notified to the administrator
    4. the backup server retains data only six months, remove all remaining
    5 services daily check result of the timing

Server (Server) deployment Rsync

#1.安装
[root@backup ~]# yum install rsync -y
#2.配置
[root@backup ~]# 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用户
[root@backup ~]# groupadd rsync
[root@backup ~]# useradd rsync -M -s /sbin/nologin -g rsync
[root@backup ~]# id rsync
uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)

3.2创建虚拟用户和密码
[root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd

3.3创建backup目录
[root@backup ~]# mkdir /backup
[root@backup ~]# chown -R rsync.rsync /backup/

#4.启动服务
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd

#5.检查
[root@backup ~]# 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-side operations:
ideas:
1. What you need to back up?
/ Etc / fstab / etc / hosts / etc / passwd / var / spool / cron /
2. You have to back up to where?
/Backup/nfs_172.16.1.31_2018-09 -02
IP: ifconfig eth1 | awk 'NR == 2 Print {$ 2}'
Host: hostname
DATE: DATE +% F
3. where you want to push?
172.16.1.41

== == Note: All servers must backup directory are / backup

快递:  散货-------->打包--->标记------->装车-------------->运输------------->仓库
       验货-------->评价
       
系统:  备份的文件-->打包--->校验值----->备份至本地目录---->网络-------->推送至备份服务器
       管理员校验-->通知--->邮件
#创建存放脚本的位置:     
[root@nfs ~]# mkdir -p /server/scripts/
#根据要求遍写脚本:
[root@nfs ~]# 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    
------------------------------------------------------------------------------------------------------
##批量的模拟数据
[root@nfs ~]# for i in {1..30};do date -s "201909$i"; sh /scripts/clinet_push_data_server.sh ;

== Server-side operations: ==

------------------ configure your mail server function --------------------

1.安装
[root@backup ~]# yum install mailx -y

2.配置
[root@backup ~]# 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.测试一下是否能发送成功
[root@backup ~]# mail -s "Rsync Backup" [email protected] < /etc/hosts

4.编写脚本
[root@backup ~]# mkdir -p /server/scripts
[root@backup ~]# 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

#定时任务:
客户端
[root@nfs ~]# crontab -l
#客户端每天凌晨1点定时执行该脚本
*/2 * * * * sh /server/scripts/push_data_server.sh &> /dev/null

服务端
[root@backup ~]# crontab -l
#定时校验备份的结果
*/2 * * * *   sh /server/scripts/check_data_notify.sh &> /dev/null


如何增加多台及上千台客户端服务器备份数据?
[root@web01 ~]# rsync -avz [email protected]:/server /
[root@web02 ~]# rsync -avz [email protected]:/server /
[root@web03 ~]# rsync -avz [email protected]:/server /
[root@web04 ~]# rsync -avz [email protected]:/server /
[root@web05 ~]# rsync -avz [email protected]:/server /

......................................................

Guess you like

Origin www.cnblogs.com/yinwu/p/11481974.html