综合案例:利用ansible常用七种模块实现备份系统的部署及告警(十一)
1.综合案例-rsync,nfs,httpd,sersync
1.1.基础环境部署
1)关闭firewalld、selinux
每台服务器都要操作,也可以使用ansible的shell模块批量操作
[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# systemctl disable firewalld
[root@ansible ~]# sed -ri '/SELINUX=/c SELINUX=disabled' /etc/sysconfig/selinux
[root@ansible ~]# sed -ri '/SELINUX=/c SELINUX=disabled' /etc/selinux/config
2)ssh密钥生成,及公钥推送
1.生成公钥文件
[root@ansible ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
2.将公钥推送至远程服务器
[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.81.220
[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.81.230
[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.81.230
3)镜像配置
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo ;curl -o /etc/yum.repos.d/CentOS-Base.repo
4)安装软件包rsync、nfs-utils
[root@ansible ~]# ansible all -m yum -a "name=nfs-utils,rpcbind,rsync state=installed"
5)创建用户、组
组
[root@ansible ~]# ansible all -m group -a "name=linuxowner gid=9999"
用户
[root@ansible ~]# ansible all -m user -a "name=linuxowner uid=9999 group=9999 create_home=no shell=/sbin/nologin"
6)创建目录、并修改所属和权限
rsync备份
[root@ansible ~]# ansible all -m file -a "path=/backup owner=6666 group=6666 state=directory"
"
数据目录
[root@ansible ~]# ansible all -m file -a "path=/data owner=6666 group=6666 state=directory"
脚本目录
[root@ansible ~]# [root@ansible ~]# ansible all -m file -a "path=/script owner=6666 group=6666 state=directory"
7)创建rsync客户端密码文件
[root@ansible ~]# ansible all -m copy -a "content=123456 dest=/etc/rsync_pass mode=600"
8)推送脚本
[root@ansible ~]# mkdir /server/script/ -p
准备rsync安装脚本
准备rsync备份脚本
root@ansible ~]# tree /server/script
/server/script
├── rsyncd_backup.sh
├── rsyncd_shjc_install.sh
└── rsyncd_sjjy2.sh
推送至客户端
1.备份脚本推送至每个客户端
[root@ansible ~]# ansible all -m copy -a "src=/server/script/rsyncd_backup.sh dest=/script"
2.对rsync执行安装脚本
[root@ansible ~]# ansible backup -m script -a "/server/script/rsyncd_shjc_install.sh"
4.推送数据校验脚本至rsync
[root@ansible ~]# ansible backup -m copy -a "src=/server/script/rsyncd_sjjy2.sh dest=/script"
9)计划任务
web服务器
[root@ansible ~]# ansible web -m cron -a "name=rsync_backup hour=1 minute=0 job='/bin/sh /script/rsyncd_backup.sh'"
nfs服务器
[root@ansible ~]# ansible nfs -m cron -a "name=rsync_backup hour=1 minute=0 job='/bin/sh /script/rsyncd_backup.sh'"
backup服务器
[root@ansible ~]# ansible backup -m cron -a "name=rsync_check hour=5 minute=0 job='/bin/sh /script/rsyncd_sjjy2.sh'"
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3ANTYqeS-1593935468984)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20200610234617538.png)]
1.2.安装rsynnc
1)安装rsync
[root@ansible ~]# ansible backup -m yum -a "name=rsync state=installed"
2)配置rsync服务端
[root@ansible ~]# mkdir -p /server/conf
[root@ansible conf]# ansible backup -m copy -a "src=/server/conf/rsyncd.conf dest=/etc/"
[root@ansible conf]# ansible backup -m copy -a "content=rsync_backup:123456 dest=/etc/rsyncd.conf"
[root@ansible conf]# ansible backup -m file -a "path=/etc/rsync_passwd mode=666"
3)启动rsync
[root@ansible conf]# ansible backup -m service -a "name=rsyncd state=restarted"
13.3.安装NFS
1)安装配置nfs
[root@ansible conf]# ansible nfs -m yum -a "name=nfs-utils,rpcbind state=installed"
[root@ansible conf]# echo '/data2 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)' > /server/conf/exports
[root@ansible conf]# ansible nfs -m copy -a "src=/server/conf/exports dest=/etc/"
2)启动nfs
[root@ansible conf]# ansible nfs -m service -a "name=nfs state=started"
3)挂载
[root@ansible conf]# ansible all -m mount -a "src=192.168.81.230/data2 path=/data2 fstype=nfs state=mounted"
目录 返回
首页