使用windos模拟搭建web集群(二)
一.通过rsync搭建备份服务器
这三个目录我们需要做实时热备,他们分别是 系统的脚本目录 系统的配置文件目录 系统的定时任务目录
[root@mage-monitor-01 ~]# cat /server/scripts/backuplist /server/scripts /etc /var/spool/cron
1.使用ansible 的 file模块 在所有主机上模拟创建 一个写脚本的 目录
[root@mage-monitor-01 scripts]# for i in `cat backuplist`;do ansible all -m file -a "path=/server/scripts state=directory"; done;
2.服务端安装部署rsync
[root@store-rsync-01 ~]# yum install -y rsync
设置rsync的开机自启动
[root@store-rsync-01 ~]# chmod +x /etc/rc.d/rc.local
[root@store-rsync-01 ~]# echo "rsync --daemon" >>/etc/rc.d/rc.local
配置
uid = root gid = root use chroot = no max connections = 200 timeout = 300 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 = 192.168.5.0/24 auth users = benjamin secrets file = /etc/rsync.password [backup] comment = "backup dir " path = /backup [nfsbackup] comment = "nfsbackup dir" path = /nfsbackup [scripts] comment = "scripts dir" path = /server/scripts [etc] comment = "etc dir" path = /etc [crontab] comment = "crontab dir" path = /var/spool/cron
创建 rsync用户
[root@store-rsync-01 ~]# useradd -s /sbin/nologin -M rsync
创建数据备份储存目录,目录修改属主
[root@backup ~]# mkdir /nfsbackup/ [root@backup ~]# chown -R rsync.rsync /nfsbackup/
创建认证用户密码文件并进行授权600
[root@store-rsync-01 ~]# echo "benjamin:123" >>/etc/rsync.password [root@store-rsync-01 ~]# chmod 600 /etc/rsync.password
启动rsync服务
rsync --daemon
[root@store-rsync-01 ~]# ps -ef |grep rsync root 1087 1 0 07:57 ? 00:00:00 rsync --daemon root 1091 1049 0 07:57 pts/0 00:00:00 grep --color=auto rsync
3.客户端测试
创建安全认证文件,并进行修改权限600
echo "123" >>/etc/rsync.password chmod 600 /etc/rsync.password
[root@mage-monitor-01 scripts]# rsync -avz `pwd` benjamin@192.168.5.131::nfsbackup --password-file=/etc/rsync.password sending incremental file list rsync: chgrp "scripts" (in nfsbackup) failed: Operation not permitted (1) scripts/ scripts/a.sh rsync: chgrp "scripts/.a.sh.SdKZyg" (in nfsbackup) failed: Operation not permitted (1) sent 122 bytes received 216 bytes 676.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
4.使用ansible copy模块 将/etc/rsync.password 和 同步脚本发送到所有主机
备份脚本
[root@mage-monitor-01 scripts]# cat backup_to_rsync.sh #!/bin/bash #source function lib . /etc/init.d/functions rsync_host=rsync.woniu.com #Defined varibles IP=$(ifconfig eth1|awk -F '[ :]+' 'NR==2 {print $3}') Path="/backup/$IP" TIME=`/bin/date +%F` BackupFile=/server/scripts/backuplist # Judge the existence of varibles [ ! -d $Path ] && mkdir -p $Path [ ! -f $BackupFile ] && { echo "Please give me $BackupFile" exit 1 } # Defined result function function Msg(){ if [ $? -eq 0 ];then action "$*" /bin/true else action "$*" /bin/false fi } # Backup config files tar zcfh $Path/conf_${TIME}.tar.gz `cat $BackupFile` &>/dev/null Msg 'Backup config files' # Make a flag for backup find $Path -type f -name "${TIME}.tar.gz"|xargs md5sum >$Path/flag_$TIME 2>/dev/null Msg 'Make a flag for backup' # Send backup to backup server rsync -avz $Path benjamin@${rsync_host}::backup --password-file=/etc/rsync.password Msg 'Send backup to backup server' # Delete backup a week ago find ${Path-/tmp} -type f -name "*.tar.gz" -mtime +7|xargs rm -f &>/dev/null Msg 'Delete backup a week ago'
[root@mage-monitor-01 ~]# ansible all -m copy -a "src=/server/scripts/backuplist dest=/server/scripts/"
[root@mage-monitor-01 scripts]# ansible all -m copy -a "src=/etc/rsync.password dest=/etc"
[root@mage-monitor-01 scripts]# ansible all -m copy -a "src=/server/scripts/backup_to_rsync.sh dest=/server/scripts/"
5.将脚本加入定时任务使用每天凌晨推送 ansible的 shell 模块
centos7默认 的定时任务文件没有执行权限需要添加一下
[root@mage-monitor-01 scripts]# ansible all -m shell -a "chmod +x /etc/rc.d/rc.local"
定时脚本
[root@mage-monitor-01 scripts]# cat set_backup_script.sh #!/bin/bash function crond_backup(){ [ `crontab -l|grep "backup data"|wc -l` -eq 0 ]&&{ echo -e "#backup data\n 00 00 * * * /bin/sh /server/scripts/backup_to_rsync.sh >/dev/null 2>&1" >> \ /var/spool/cron/root crontab -l sleep 2 }||{ echo "backup cron is exist,no config." } } crond_backup
将脚本 发送到所有主机 已存在的不做覆盖
[root@mage-monitor-01 ~]# ansible all -m copy -a "src=/server/scripts/set_backup_script.sh dest=/server/scripts/"
使用ansible的 script模块 执行脚本 removes参数判断脚本不存在就不执行,反之就执行
[root@mage-monitor-01 ~]# ansible all -m script -a "removes=/server/scripts/set_backup_script.sh /server/scripts/set_backup_script.sh "
将备份结果发邮件提醒就不弄了,比较简单。
下一节搞小米监控