一键部署nfs、rsync、sersync

项目代码:

链接:https://pan.baidu.com/s/13I0BBAYsdK-KmPekZ5VpdA

提取码:u2tw

--来自百度网盘超级会员V6的分享

[root@m01 /ansible/roles]# tree -F
.
├── fenfa.sh #分发秘钥脚本
├── group_vars/ #主机组变量
│ └── all/
│ └── main.yml
├── hosts #hosts文件
├── nfs-client/
│ ├── files/
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
├── nfs-server/
│ ├── files/
│ ├── handlers/
│ │ └── main.yml
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── exports.j2
├── rsync-client/
│ ├── files/
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ ├── back-conf.j2
│ └── rsync.j2
├── rsync-server/
│ ├── files/
│ ├── handlers/
│ │ └── main.yml
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── rsyncd.j2
├── sersync-client/
│ ├── files/
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
├── sersync-server/
│ ├── files/
│ │ └── sersync2.5.4_64bit_binary_stable_final.tar.gz
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── confxml.j2*
└── top.yml #启动文件

fenfa.sh文件内容

[root@m01 /ansible/roles]# cat fenfa.sh
#!/bin/bash
#author: wh
#version: v2
#desc: 一键创建秘钥对 分发秘钥对 #1.vars
pass=1 #服务器的密码
ips="172.16.1.7 172.16.1.31 172.16.1.41"
. /etc/init.d/functions #1.4 判断是否联网或是否可以使用yum
#1.5 加入判断sshpass命令是否存在,如果不存在则安装 #2.创建秘钥对
if [ -f ~/.ssh/id_rsa ] ;then
echo "已经创建过秘钥对"
else
echo "正在创建秘钥对...."
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' &>/dev/null
if [ $? -eq 0 ];then
action "密钥创建成功" /bin/true
else
action "密钥创建失败" /bin/false
fi
fi #3.通过循环发送公钥
for ip in $ips
do
sshpass -p${pass} ssh-copy-id -i ~/.ssh/id_rsa.pub -oStrictHostKeyChecking=no $ip &>/dev/null
if [ $? -eq 0 ];then
action "$ip 公钥分发 成功" /bin/true
else
action "$ip 公钥分发 失败" /bin/false
fi
done

hosts文件内容

[root@m01 /ansible/roles]# cat hosts
[web]
172.16.1.7 [nfs]
172.16.1.31 [backup]
172.16.1.41

启动文件top.yml文件内容

[root@m01 /ansible/roles]# cat top.yml
- hosts: nfs
roles:
- role: nfs-server
- role: rsync-client
- role: sersync-server - hosts: backup
roles:
- role: rsync-server
- role: rsync-client
- role: sersync-client - hosts: web
roles:
- role: rsync-client
- role: nfs-client

主机组变量文件内容

[root@m01 /ansible/roles]# cat group_vars/all/main.yml
#nfs的用户
nfs_user: nfsnobody #nfs的共享的挂载目录
nfs_dir: /data #nfs配置的共享目录
nfs_server_dir: "172.16.1.31:/data" #web挂载nfs的目录
web_nfs_dir: /upload #rsync用户
rsync_user: rsync #rsync认证用户
rsync_auth_user: rsync_backup #rsync服务端ip
rsync_server_ip: 172.16.1.41 #rsync备份配置文件的模板
rsync_module_name: backup #rsync的备份共享目录
rsync_dir: /backup #rsync密码文件
rsync_client_pass_dir: /etc/rsync.client #rsync的密码
rsync_auth_password: 1 #sersync的nfs实时同步模块
sersync_module_name: nfsbackup #sersync的nfs实时同步目录
sersync_dir: /nfsbackup

nfs客户端文件内容

[root@m01 /ansible/roles]# cat nfs-client/tasks/main.yml
- name: 安装nfs-utils
yum:
name: nfs-utils
state: present
- name: 挂载目录
mount:
src: "{{ nfs_server_dir }}"
path: "{{ web_nfs_dir }}"
fstype: nfs
state: mounted

nfs服务端文件内容

[root@m01 /ansible/roles]# cat nfs-server/tasks/main.yml
- name: 安装rpcbind,nfs-utils
yum:
name: "{{ item }}"
state: present
loop:
- rpcbind
- nfs-utils
- name: 创建共享目录,修改属主属组
file:
path: "{{ nfs_dir }}"
state: directory
owner: "{{ nfs_user }}"
group: "{{ nfs_user }}"
- name: 修改配置文件
template:
src: exports.j2
dest: /etc/exports
backup: yes
notify:
- 重载nfs
- name: 启动rpcbind,nfs
systemd:
name: "{{ item }}"
enabled: yes
state: started
loop:
- rpcbind
- nfs [root@m01 /ansible/roles]# cat nfs-server/handlers/main.yml
- name: 重载nfs
systemd:
name: nfs
state: reloaded [root@m01 /ansible/roles]# cat nfs-server/templates/exports.j2
{{ nfs_dir }} 172.16.1.0/24(rw)

rsync客户端文件内容

[root@m01 /ansible/roles]# cat rsync-client/tasks/main.yml
- name: 安装rsync
yum:
name: rsync
state: present
- name: 创建脚本目录
file:
path: /server/scripts
state: directory
- name: 分发备份脚本
template:
src: back-conf.j2
dest: /server/scripts/back-conf.sh
- name: 分发密码文件
template:
src: rsync.j2
dest: "{{ rsync_client_pass_dir }}"
mode: 600
- name: 创建定时任务
cron:
name: backup conf
minute: "*/2"
job: sh /server/scripts/back-conf.sh &>/dev/null
state: present [root@m01 /ansible/roles]# cat rsync-client/templates/back-conf.j2
#!/bin/bash
#author: wh
#desc: 备份配置文件+定时任务+推送到rsync服务端 source /etc/profile
source ~/.bash_profile
#定义变量
ip=`ifconfig|awk 'NR==2{print $2}'`
date=`date +%F`
backup_dir=/backup/${ip}
backup_filename=conf-${date}.tar.gz
#rsync用户
rsync_authUser={{ rsync_auth_user }}
#rsync密码文件
rsync_passwdFile={{ rsync_client_pass_dir }}
#服务端ip
rsync_serviceIP={{ rsync_server_ip }}
#备份服务器备份模块
rsync_module={{ rsync_module_name }} #创建备份目录
mkdir -p ${backup_dir} #备份
tar zcf ${backup_dir}/${backup_filename} /etc/ /var/spool/cron #生成md5sum校验文件
md5sum ${backup_dir}/${backup_filename} > ${backup_dir}/conf.md5 #推送到rsync服务端
rsync -az ${backup_dir} ${rsync_authUser}@${rsync_serviceIP}::${rsync_module} --password-file=${rsync_passwdFile} #删除7天之前的备份
rm -f `find ${backup_dir} -type f -name "*.tar.gz" -mtime +7` [root@m01 /ansible/roles]# cat rsync-client/templates/rsync.j2
{{ rsync_auth_password }}

rsync服务端文件内容

[root@m01 /ansible/roles]# cat rsync-server/tasks/main.yml
- name: 安装rsync
yum:
name: rsync
state: present
- name: 配置rsync
template:
src: rsyncd.j2
dest: /etc/rsyncd.conf
backup: yes
notify:
- 重启rsync
- name: 创建用户
user:
name: "{{ rsync_user }}"
create_home: no
shell: /sbin/nologin
- name: 创建共享目录,修改属组属主 /backup
file:
path: "{{ rsync_dir }}"
state: directory
owner: "{{ rsync_user }}"
group: "{{ rsync_user }}"
- name: 创建密码文件并写入密码修改权限
lineinfile:
path: /etc/rsync.password
line: "{{ rsync_auth_user }}:{{ rsync_auth_password }}"
create: yes
mode: 600
- name: 启动rsync
systemd:
name: rsyncd
enabled: yes
state: started [root@m01 /ansible/roles]# cat rsync-server/handlers/main.yml
- name: 重启rsync
systemd:
name: rsyncd
state: restarted [root@m01 /ansible/roles]# cat rsync-server/templates/rsyncd.j2
fake super =yes
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
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 = 10.0.0.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#######################################
[{{ rsync_module_name }}]
comment = "备份文件夹"
path = {{ rsync_dir }}
[{{ sersync_module_name }}]
comment = "nfs备份文件夹"
path = {{ sersync_dir }}

sersync客户端文件内容

[root@m01 /ansible/roles]# cat sersync-server/tasks/main.yml
- name: 创建bin目录,conf目录
file:
path: "{{ item }}"
state: directory
loop:
- /app/tools/sersync/bin/
- /app/tools/sersync/conf/
- name: 解压sersync2.5.4_64bit_binary_stable_final.tar.gz
unarchive:
src: sersync2.5.4_64bit_binary_stable_final.tar.gz
dest: /root/
- name: 移动目录
shell: "mv /root/GNU-Linux-x86/sersync2 /app/tools/sersync/bin"
- name: 拷贝配置文件
template:
src: confxml.j2
dest: /app/tools/sersync/conf/confxml.xml
backup: yes
- name: 创建快捷方式
file:
path: /bin/sersync2
src: /app/tools/sersync/bin/sersync2
state: link
- name: 启动sersync
shell: "sersync2 -rdo /app/tools/sersync/conf/confxml.xml" [root@m01 /ansible/roles]# cat sersync-server/templates/confxml.j2
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify> <sersync>
<localpath watch="{{ nfs_dir }}">
<remote ip="{{ rsync_server_ip }}" name="{{ sersync_module_name }}"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="{{ rsync_auth_user }}" passwordfile="{{ rsync_client_pass_dir }}"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync> <plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin> <plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head> #此文件在百度网盘的链接接里,或者从官网下载也行
[root@m01 /ansible/roles]# ll sersync-server/files
total 712
-rw-r--r-- 1 root root 727290 Feb 7 15:27 sersync2.5.4_64bit_binary_stable_final.tar.gz

一键部署nfs、rsync、sersync的更多相关文章

  1. day10、nfs+rsync全网备份及实时同步

    题目要求 注意:博主使用的系统为: [root@web01 ~]# uname -a Linux web01 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29 ...

  2. Kubernetes一键部署利器:kubeadm

    要真正发挥容器技术的实力,你就不能仅仅局限于对 Linux 容器本身的钻研和使用. 这些知识更适合作为你的技术储备,以便在需要的时候可以帮你更快的定位问题,并解决问题. 而更深入的学习容器技术的关键在 ...

  3. 文件触发式实时同步 Rsync+Sersync Rsync+Inotify-tools

    一.概述 1.Rsync+Sersync 是什么? 1)Sersync使用c++编写基于inotify开发的触发机制: 2)Sersync可以监控所监听的目录发生的变化(包括新建.修改.删除),具体到 ...

  4. Rsync+sersync实现实时同步

    介绍: sersync主要用于服务器同步,web镜像等功能.基于boost1.43.0,inotify api,rsync command.开发.目前使用的比较多的同步解决方案是inotify-too ...

  5. rsync+sersync多线程实时同步

    一.sersync优点 1)使用c++编写,对linux系统文件产生的临时文件和重复文件操作会进行过滤,在结合rsync同步的时候,会减少运行时消耗的本地及网络资源,因此速度更快. 2)相比较inot ...

  6. inotify+rsync sersync+rsync实时同步服务

    中小型网站搭建-数据实时的复制-inotify/sersync inotify是一种强大的,细粒度的.异步的文件系统事件监控机制(软件),linux内核从2.6.13起,加入inotify支持,通过i ...

  7. rsync & sersync 实时同步

    1.根据之前一篇关于rsync的随笔部署好rsync服务后,可以开始inotify的部署 2.sersync的部署 ①.部署服务(安装和配置过程) #Master 部署Sersync服务 mkdir ...

  8. centos7服务搭建常用服务配置之二:Rsync+sersync实现数据实时同步

    目录 1.RSYNC数据备份 1.1 rsync服务简介 1.2 rsync特点和优势 1.3 rysnc运行模式简介 1.4 数据同步方式 2 Rsync实验测试 2.1 实验环境说明 2.2 服务 ...

  9. rsync+sersync实现文件同步

    一.目的 A服务器:11.11.11.11 源服务器 B服务器:22.22.22.22 目标服务器,既同步备份的目标 将A服务器的文件同步到B服务器上 二.rsync环境部署 1.关闭selinux, ...

  10. Mysql+Mycat+NFS+Rsync+LVS+DNS+IPtables综合实验

    1.环境准备 服务器 IP地址 作用 系统版本 Mysql-master eth0:10.0.0.58 主数据库 Rocky8.6 Mysql-slave1 eth0:10.0.0.68 备数据库 R ...

随机推荐

  1. k8s 中的 ingress 使用细节

    k8s中的ingress 什么是ingress Ingress 如何使用 ingress 使用细节 参考 k8s中的ingress 什么是ingress k8s 中使用 Service 为相同业务的 ...

  2. Appscan的安装破解以及使用

    本文简单介绍Appscan的安装和使用. 一.下载安装 可自行百度下载相关安装包(因较高版本的破解资料比较难找,建议下载9.0版本). 双击.exe安装文件进行安装,在弹出的安装指引中各选项默认安装即 ...

  3. Xtrabackup使用帮助

    目录 1.安装工具 2.下载后上传到需要备份的服务器 全备 1.安装完成后我们进行数据库备份执行以下命令 2.查看备份的数据 3.进入数据库,删除一个测试库 4.删除school库 5.备份数据目录 ...

  4. 2022春每日一题:Day 17

    今天打CF去了,但是很菜,只做了三题.赛后一分钟做出了第四题,wa了,改了一下下,过了 第一题就是对应的小写字母在大写字母前出现. 第二题直接dfs. 第三题dp,f[i][j]表示以第i个数开始加了 ...

  5. CAN总线数据链路层(一)

    1.通信机制 发送报文. 1.首先检测Bus状态,空闲 则发送 报文且回读         2.线与机制,若有两个节点同时发报文          报文结构:          通过ID进行仲裁(规则 ...

  6. 绵阳2020CCPC补题

    绵阳2020CCPC D,K,J,L,G D. Defuse the Bombs 知识点:二分答案 复杂度:\(O(nlogn+log^2n)\) vp时我猜了一个结论,验了几个样例就写了,喜提WA3 ...

  7. 3. qtdesinger的使用方法

    专栏地址 ʅ(‾◡◝)ʃ windows 上使用 qtdesigner 找到那个路径直接打开就行了 linux 上使用 qtdesigner 打开 qtcreator 软件 1. 启动软件,新建项目 ...

  8. c++详细学习——引用

    1 引用(reference) 引用是一个变量的别名,故引用在申明的时候必须给初始值,从此他们就建立了"不能离婚的婚姻关系",改变引用就会改变被引用的原变量 1 int main( ...

  9. 【大数据工具选型】ETL&同步&调度工具比较-Kettle、Streamset,DataX、Sqoop、Canel,DolphinSchedule、Azkaban、Oozie、Airflow、Xxl Job

    〇.概述 1.常用资料 dolphinscheduler用户手册:https://dolphinscheduler.apache.org/zh-cn/docs/latest/user_doc/syst ...

  10. 06.python闭包

    python闭包 什么样的函数是 闭包函数 ? 满足以下条件: 闭:外层函数嵌套了一个内层函数. 包:内层函数调用外层函数命名空间内的名字. 举例如下: def out_func(): # 外层函数 ...