翼度科技»论坛 云主机 LINUX 查看内容

一键部署nfs、rsync、sersync

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
一键部署nfs、rsync、sersync

项目代码:

链接:https://pan.baidu.com/s/13I0BBAYsdK-KmPekZ5VpdA
提取码:u2tw
--来自百度网盘超级会员V6的分享
目录结构
  1. [root@m01 /ansible/roles]# tree -F
  2. .
  3. ├── fenfa.sh              #分发秘钥脚本
  4. ├── group_vars/                      #主机组变量
  5. │ └── all/
  6. │     └── main.yml
  7. ├── hosts                                  #hosts文件
  8. ├── nfs-client/
  9. │ ├── files/
  10. │ ├── handlers/
  11. │ ├── tasks/
  12. │ │ └── main.yml
  13. │ └── templates/
  14. ├── nfs-server/
  15. │ ├── files/
  16. │ ├── handlers/
  17. │ │ └── main.yml
  18. │ ├── tasks/
  19. │ │ └── main.yml
  20. │ └── templates/
  21. │     └── exports.j2
  22. ├── rsync-client/
  23. │ ├── files/
  24. │ ├── handlers/
  25. │ ├── tasks/
  26. │ │ └── main.yml
  27. │ └── templates/
  28. │     ├── back-conf.j2
  29. │     └── rsync.j2
  30. ├── rsync-server/
  31. │ ├── files/
  32. │ ├── handlers/
  33. │ │ └── main.yml
  34. │ ├── tasks/
  35. │ │ └── main.yml
  36. │ └── templates/
  37. │     └── rsyncd.j2
  38. ├── sersync-client/
  39. │ ├── files/
  40. │ ├── handlers/
  41. │ ├── tasks/
  42. │ │ └── main.yml
  43. │ └── templates/
  44. ├── sersync-server/
  45. │ ├── files/
  46. │ │ └── sersync2.5.4_64bit_binary_stable_final.tar.gz
  47. │ ├── handlers/
  48. │ ├── tasks/
  49. │ │ └── main.yml
  50. │ └── templates/
  51. │     └── confxml.j2*
  52. └── top.yml                                #启动文件
复制代码
fenfa.sh文件内容
  1. [root@m01 /ansible/roles]# cat fenfa.sh
  2. #!/bin/bash
  3. #author: wh
  4. #version: v2
  5. #desc: 一键创建秘钥对 分发秘钥对
  6. #1.vars
  7. pass=1             #服务器的密码
  8. ips="172.16.1.7 172.16.1.31 172.16.1.41"
  9. . /etc/init.d/functions
  10. #1.4 判断是否联网或是否可以使用yum
  11. #1.5 加入判断sshpass命令是否存在,如果不存在则安装
  12. #2.创建秘钥对
  13. if [ -f ~/.ssh/id_rsa ] ;then
  14.    echo "已经创建过秘钥对"
  15. else
  16.    echo "正在创建秘钥对...."
  17.    ssh-keygen -t rsa  -f  ~/.ssh/id_rsa   -P ''  &>/dev/null
  18.    if [ $? -eq 0 ];then
  19.        action "密钥创建成功" /bin/true
  20.    else
  21.        action "密钥创建失败" /bin/false
  22.    fi
  23. fi
  24. #3.通过循环发送公钥
  25. for ip  in  $ips
  26. do
  27.    sshpass -p${pass} ssh-copy-id -i ~/.ssh/id_rsa.pub -oStrictHostKeyChecking=no  $ip &>/dev/null
  28.    if [ $? -eq 0 ];then
  29.        action "$ip 公钥分发 成功" /bin/true
  30.    else
  31.        action "$ip 公钥分发 失败" /bin/false
  32.    fi
  33. done
复制代码
hosts文件内容
  1. [root@m01 /ansible/roles]# cat hosts
  2. [web]
  3. 172.16.1.7
  4. [nfs]
  5. 172.16.1.31
  6. [backup]
  7. 172.16.1.41
复制代码
启动文件top.yml文件内容
  1. [root@m01 /ansible/roles]# cat top.yml
  2. - hosts: nfs                                                                               
  3.   roles:
  4.     - role: nfs-server
  5.     - role: rsync-client
  6.     - role: sersync-server
  7. - hosts: backup
  8.   roles:
  9.     - role: rsync-server
  10.     - role: rsync-client
  11.     - role: sersync-client
  12. - hosts: web
  13.   roles:
  14.     - role: rsync-client
  15.     - role: nfs-client
复制代码
主机组变量文件内容
  1. [root@m01 /ansible/roles]# cat group_vars/all/main.yml
  2. #nfs的用户
  3. nfs_user: nfsnobody
  4. #nfs的共享的挂载目录
  5. nfs_dir: /data
  6. #nfs配置的共享目录
  7. nfs_server_dir: "172.16.1.31:/data"
  8. #web挂载nfs的目录
  9. web_nfs_dir: /upload
  10. #rsync用户
  11. rsync_user: rsync
  12. #rsync认证用户
  13. rsync_auth_user: rsync_backup
  14. #rsync服务端ip
  15. rsync_server_ip: 172.16.1.41
  16. #rsync备份配置文件的模板
  17. rsync_module_name: backup
  18. #rsync的备份共享目录
  19. rsync_dir: /backup
  20. #rsync密码文件
  21. rsync_client_pass_dir: /etc/rsync.client
  22. #rsync的密码
  23. rsync_auth_password: 1
  24. #sersync的nfs实时同步模块
  25. sersync_module_name: nfsbackup
  26. #sersync的nfs实时同步目录
  27. sersync_dir: /nfsbackup
复制代码
nfs客户端文件内容
  1. [root@m01 /ansible/roles]# cat nfs-client/tasks/main.yml
  2. - name: 安装nfs-utils
  3.   yum:
  4.     name: nfs-utils
  5.     state: present
  6. - name: 挂载目录
  7.   mount:
  8.     src: "{{ nfs_server_dir }}"
  9.     path: "{{ web_nfs_dir }}"
  10.     fstype: nfs
  11.     state: mounted
复制代码
nfs服务端文件内容
  1. [root@m01 /ansible/roles]# cat nfs-server/tasks/main.yml
  2. - name: 安装rpcbind,nfs-utils
  3.   yum:
  4.     name: "{{ item }}"
  5.     state: present
  6.   loop:
  7.     - rpcbind
  8.     - nfs-utils
  9. - name: 创建共享目录,修改属主属组
  10.   file:
  11.     path: "{{ nfs_dir }}"
  12.     state: directory
  13.     owner: "{{ nfs_user }}"
  14.     group: "{{ nfs_user }}"
  15. - name: 修改配置文件
  16.   template:
  17.     src: exports.j2
  18.     dest: /etc/exports
  19.     backup: yes
  20.   notify:
  21.     - 重载nfs
  22. - name: 启动rpcbind,nfs
  23.   systemd:
  24.     name: "{{ item }}"
  25.     enabled: yes
  26.     state: started
  27.   loop:
  28.     - rpcbind
  29.     - nfs
  30.    
  31. [root@m01 /ansible/roles]# cat nfs-server/handlers/main.yml
  32. - name: 重载nfs
  33.   systemd:
  34.     name: nfs
  35.     state: reloaded
  36.    
  37. [root@m01 /ansible/roles]# cat nfs-server/templates/exports.j2
  38. {{ nfs_dir }} 172.16.1.0/24(rw)
复制代码
rsync客户端文件内容
  1. [root@m01 /ansible/roles]# cat rsync-client/tasks/main.yml
  2. - name: 安装rsync
  3.   yum:
  4.     name: rsync
  5.     state: present
  6. - name: 创建脚本目录
  7.   file:
  8.     path: /server/scripts
  9.     state: directory
  10. - name: 分发备份脚本
  11.   template:
  12.     src:  back-conf.j2
  13.     dest: /server/scripts/back-conf.sh
  14. - name: 分发密码文件
  15.   template:
  16.     src: rsync.j2
  17.     dest: "{{ rsync_client_pass_dir }}"
  18.     mode: 600
  19. - name: 创建定时任务
  20.   cron:
  21.     name: backup conf
  22.     minute: "*/2"
  23.     job: sh /server/scripts/back-conf.sh &>/dev/null
  24.     state: present
  25.    
  26. [root@m01 /ansible/roles]# cat rsync-client/templates/back-conf.j2
  27. #!/bin/bash
  28. #author: wh
  29. #desc:   备份配置文件+定时任务+推送到rsync服务端
  30. source /etc/profile
  31. source ~/.bash_profile
  32. #定义变量
  33. ip=`ifconfig|awk 'NR==2{print $2}'`
  34. date=`date +%F`
  35. backup_dir=/backup/${ip}
  36. backup_filename=conf-${date}.tar.gz
  37. #rsync用户
  38. rsync_authUser={{ rsync_auth_user }}
  39. #rsync密码文件
  40. rsync_passwdFile={{ rsync_client_pass_dir }}
  41. #服务端ip
  42. rsync_serviceIP={{ rsync_server_ip }}
  43. #备份服务器备份模块
  44. rsync_module={{ rsync_module_name }}
  45. #创建备份目录
  46. mkdir -p ${backup_dir}
  47. #备份
  48. tar zcf ${backup_dir}/${backup_filename} /etc/ /var/spool/cron
  49. #生成md5sum校验文件
  50. md5sum ${backup_dir}/${backup_filename} > ${backup_dir}/conf.md5
  51. #推送到rsync服务端
  52. rsync -az ${backup_dir} ${rsync_authUser}@${rsync_serviceIP}::${rsync_module} --password-file=${rsync_passwdFile}
  53. #删除7天之前的备份
  54. rm -f `find ${backup_dir} -type f -name "*.tar.gz" -mtime +7`
  55. [root@m01 /ansible/roles]# cat rsync-client/templates/rsync.j2
  56. {{ rsync_auth_password }}
复制代码
rsync服务端文件内容
  1. [root@m01 /ansible/roles]# cat rsync-server/tasks/main.yml
  2. - name: 安装rsync
  3.   yum:
  4.     name: rsync
  5.     state: present
  6. - name: 配置rsync
  7.   template:
  8.     src: rsyncd.j2
  9.     dest: /etc/rsyncd.conf
  10.     backup: yes
  11.   notify:
  12.     - 重启rsync
  13. - name: 创建用户
  14.   user:
  15.     name: "{{ rsync_user }}"
  16.     create_home: no
  17.     shell: /sbin/nologin  
  18. - name: 创建共享目录,修改属组属主 /backup
  19.   file:
  20.     path: "{{ rsync_dir }}"
  21.     state: directory
  22.     owner: "{{ rsync_user }}"
  23.     group: "{{ rsync_user }}"
  24. - name: 创建密码文件并写入密码修改权限
  25.   lineinfile:
  26.     path: /etc/rsync.password
  27.     line: "{{ rsync_auth_user }}:{{ rsync_auth_password }}"
  28.     create: yes
  29.     mode: 600
  30. - name: 启动rsync
  31.   systemd:
  32.     name: rsyncd
  33.     enabled: yes
  34.     state: started
  35. [root@m01 /ansible/roles]# cat rsync-server/handlers/main.yml
  36. - name: 重启rsync   
  37.   systemd:
  38.     name: rsyncd
  39.     state: restarted
  40. [root@m01 /ansible/roles]# cat rsync-server/templates/rsyncd.j2
  41. fake super =yes
  42. uid = rsync
  43. gid = rsync
  44. use chroot = no
  45. max connections = 2000
  46. timeout = 600
  47. pid file = /var/run/rsyncd.pid
  48. lock file = /var/run/rsync.lock
  49. log file = /var/log/rsyncd.log
  50. ignore errors
  51. read only = false
  52. list = false
  53. #hosts allow = 10.0.0.0/24
  54. #hosts deny = 0.0.0.0/32
  55. auth users = rsync_backup
  56. secrets file = /etc/rsync.password
  57. #######################################
  58. [{{ rsync_module_name }}]
  59. comment = "备份文件夹"
  60. path = {{ rsync_dir }}
  61. [{{ sersync_module_name }}]
  62. comment = "nfs备份文件夹"
  63. path = {{ sersync_dir }}
复制代码
sersync客户端文件内容
  1. [root@m01 /ansible/roles]# cat sersync-server/tasks/main.yml
  2. - name: 创建bin目录,conf目录
  3.   file:
  4.     path: "{{ item }}"
  5.     state: directory
  6.   loop:
  7.     - /app/tools/sersync/bin/
  8.     - /app/tools/sersync/conf/
  9. - name: 解压sersync2.5.4_64bit_binary_stable_final.tar.gz
  10.   unarchive:
  11.     src:  sersync2.5.4_64bit_binary_stable_final.tar.gz
  12.     dest: /root/
  13. - name: 移动目录
  14.   shell: "mv /root/GNU-Linux-x86/sersync2 /app/tools/sersync/bin"           
  15. - name: 拷贝配置文件
  16.   template:
  17.     src: confxml.j2
  18.     dest: /app/tools/sersync/conf/confxml.xml        
  19.     backup: yes
  20. - name: 创建快捷方式
  21.   file:
  22.     path: /bin/sersync2
  23.     src: /app/tools/sersync/bin/sersync2
  24.     state: link
  25. - name: 启动sersync
  26.   shell: "sersync2 -rdo /app/tools/sersync/conf/confxml.xml"
  27.   
  28. [root@m01 /ansible/roles]# cat sersync-server/templates/confxml.j2
  29. <?xml version="1.0" encoding="ISO-8859-1"?>
  30. <head version="2.5">
  31.     <host hostip="localhost" port="8008"></host>
  32.     <debug start="false"/>
  33.     <fileSystem xfs="false"/>
  34.     <filter start="false">
  35.         <exclude expression="(.*)\.svn"></exclude>
  36.         <exclude expression="(.*)\.gz"></exclude>
  37.         <exclude expression="^info/*"></exclude>
  38.         <exclude expression="^static/*"></exclude>
  39.     </filter>
  40.     <inotify>
  41.         <delete start="true"/>
  42.         <createFolder start="true"/>
  43.         <createFile start="false"/>
  44.         <closeWrite start="true"/>
  45.         <moveFrom start="true"/>
  46.         <moveTo start="true"/>
  47.         <attrib start="false"/>
  48.         <modify start="false"/>
  49.     </inotify>
  50.     <sersync>
  51.         <localpath watch="{{ nfs_dir }}">
  52.             <remote ip="{{ rsync_server_ip }}" name="{{ sersync_module_name }}"/>
  53.             
  54.             
  55.         </localpath>
  56.         <rsync>
  57.             <commonParams params="-az"/>
  58.             <auth start="true" users="{{ rsync_auth_user }}" passwordfile="{{ rsync_client_pass_dir }}"/>
  59.             <userDefinedPort start="false" port="874"/>
  60.             <timeout start="false" time="100"/>
  61.             <ssh start="false"/>
  62.         </rsync>
  63.         <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
  64.         <crontab start="false" schedule="600">
  65.             <crontabfilter start="false">
  66.                 <exclude expression="*.php"></exclude>
  67.                 <exclude expression="info/*"></exclude>
  68.             </crontabfilter>
  69.         </crontab>
  70.         <plugin start="false" name="command"/>
  71.     </sersync>
  72.     <plugin name="command">
  73.         <param prefix="/bin/sh" suffix="" ignoreError="true"/>       
  74.         <filter start="false">
  75.             <include expression="(.*)\.php"/>
  76.             <include expression="(.*)\.sh"/>
  77.         </filter>
  78.     </plugin>
  79.     <plugin name="socket">
  80.         <localpath watch="/opt/tongbu">
  81.             <deshost ip="192.168.138.20" port="8009"/>
  82.         </localpath>
  83.     </plugin>
  84.     <plugin name="refreshCDN">
  85.         <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
  86.             <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
  87.             <sendurl base="http://pic.xoyo.com/cms"/>
  88.             <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
  89.         </localpath>
  90.     </plugin>
  91. </head>
  92. #此文件在百度网盘的链接接里,或者从官网下载也行
  93. [root@m01 /ansible/roles]# ll sersync-server/files
  94. total 712
  95. -rw-r--r-- 1 root root 727290 Feb  7 15:27 sersync2.5.4_64bit_binary_stable_final.tar.gz
复制代码
来源:https://www.cnblogs.com/world-of-yuan/p/17103264.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具