zoukankan      html  css  js  c++  java
  • 服务器配置文件同步

    服务器配置文件同步

    rsync 文件同步脚本。
    =========================================================================================================
    参考:https://blog.whsir.com/post-1097.html    

    客户端安装:yum -y install rsync
    ==============================================================================
    # Debian
    $ sudo apt-get install rsync

    # Red Hat
    $ sudo yum install rsync

    # Arch Linux
    $ sudo pacman -S rsync
    ==============================================================================
    配置文件:vim /etc/rsyncd.conf
      1 uid = root
      2 gid = root
      3 use chroot = no
      4 hosts allow=*
      5 max connections = 3
      6 pid file = /var/run/rsyncd.pid
      7 lock file = /var/run/rsync.lock
      8 [tongbu]
      9 path = /whsir
     10 comment = whsir
     11 read only = false
     -----------------这个目录要先创建好------------------------
    配置文件示例:
    ----------------------start----------------------------------------------------
    # /etc/rsyncd: configuration file for rsync daemon mode
    # See rsyncd.conf man page for more options.
    # configuration example:

    uid = root
    gid = root
    address = 172.18.156.198              #监听IP          
    port = 873                          #监听端口

    use chroot = yes                   #是否囚牢,锁定家目录,rsync被黑之后,黑客无法再rsync运行的家目录之外创建文件,选项设置为yes  

    #最大连接数
    max connections = 200
    pid file = /var/run/rsyncd.pid
    #支持max connections参数的锁文件
    lock file = /var/run/rsyncd.lock
    #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
    log file = /var/log/rsyncd.log
    read only = false  #设置rsync服务端文件为读写权限
    list = false #不显示rsync服务端资源列表
    #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    #hosts allow = 192.168.139.0/24
    hosts allow = 172.18.156.198,172.18.156.195,172.18.156.197,172.18.156.196
    #hosts deny = 0.0.0.0/32 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    auth users = rsync,rsyncbak #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开

    #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
    secrets file = /etc/rsync.password       

    # exclude = lost+found/
    # transfer logging = yes
    # timeout = 900
    # ignore nonreadable = yes
    # dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

    # [ftp]
    #        path = /home/ftp
    #        comment = ftp export area

    [backup]   #自定义模块名称  
    path = /backup  #rsync服务端数据目录路径
    comment = used for web-data root    #模块描述
    read only = false                   #设置服务端文件读写权限   
    list = yes                          #是否允许查看模块信息
    auth users = rsyncbak              #备份的用户,和系统用户无关
    secrets file = /etc/rsync.password    #存放用户的密码文件(chmod 600 /etc/rsync.passwd),格式是  用户名:密码   
    ----------------------------------------------------
    uid = root                         #运行进程的身份                     
    gid = root                         #运行进程的组                
    address =192.168.0.50              #监听IP          
    port =873                          #监听端口              
    hosts allow =192.168.0.0/24        #允许同步客户端的IP地址,可以是网段,或者用*表示所有 192.168.1.0/24或192.168.1.0/255.255.255.0  
    use chroot = yes                   #是否囚牢,锁定家目录,rsync被黑之后,黑客无法再rsync运行的家目录之外创建文件,选项设置为yes            
    max connections =10                 #最大连接数           
    pid file =/var/run/rsyncd.pid       #进程PID,自动生成  
    lock file =/var/run/rsync.lock      #指max connectios参数的锁文件
    log file =/var/log/rsyncd.log       #日志文件位置

     
    [wwwroot]                           #同步模块名称           
    path =/opt/cosmo/test/              #同步文件路径     
    comment = used for web-data root    #模块描述
    read only = false                   #设置服务端文件读写权限   
    list = yes                          #是否允许查看模块信息
    auth users = rsyncuser              #备份的用户,和系统用户无关
    secrets file =/etc/rsync.passwd     #存放用户的密码文件,格式是  用户名:密码
    ----------------------end------------------------------------------------------
    启动同步服务: /usr/bin/rsync --daemon
    关闭命令:
    启动服务
    rsync --daemon
    # 关闭服务
    pkill rsync


    #启动rsync服务
    systemctl start rsyncd.service
    systemctl enable rsyncd.service

    #检查是否已经成功启动
    netstat -lnp|grep 873
    ---------------------------------------------------------

    服务端:
    cd /usr/local/
    wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
    tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
    mv GNU-Linux-x86 sersync
    cp confxml.xml confxml.xml.yl
    chmod +x sersync2
    vim confxml.xml
    --------------------------start------------------------------------
    <?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="/opt/tongbu">
            <remote ip="172.18.156.198" name="tongbu"/>

            <remote ip="172.18.156.197" name="www"/>
            <remote ip="172.18.156.195" name="www"/>
           
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
            <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>
    ----------------------------end-------------------------------------
    以下为参照:
    ----------------------start-----------------------------------------
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <head version="2.5">
    <host hostip="localhost" port="8008"></host> #保留字段,默认即可
    <debug start="false"/> #是否开启调试模式,默认false即可
    <fileSystem xfs="true"/> #是否开启支持xfs文件系统,Centos7默认都是xfs的了,所以建议true开启
    <filter start="false"> #是否开启过滤模式,根据需求开启,例:过滤以.php结尾的文件(.*).php
    <exclude expression="(.*).svn"></exclude> #过滤以.svn结尾的文件
    <exclude expression="(.*).gz"></exclude> #过滤以.gz结尾的文件
    <exclude expression="^info/*"></exclude> #过滤监控目录下的info路径
    <exclude expression="^static/*"></exclude> #过滤监控目录下的static路径
    <exclude expression="wwwroot/blogwhsir/*"></exclude> #过滤wwwroot/blogwhsir/目录下所有文件
    </filter>
    <inotify> #inotify监控事件
    <delete start="true"/> #如果不开启此项,在删除监控目录下的文件时,目标服务器的文件则不会同时删除,根据需求开启
    <createFolder start="true"/> #不开启不能监控子目录,建议true
    <createFile start="false"/> #关闭提高通讯性能,默认就好
    <closeWrite start="true"/>
    <moveFrom start="true"/>
    <moveTo start="true"/>
    <attrib start="false"/>
    <modify start="false"/>
    </inotify>
     
    <sersync>
    <localpath watch="/opt/tongbu"> #指定要监控的本地目录
    <remote ip="127.0.0.1" name="tongbu1"/> #指定要同步的目标服务器的IP地址,及目标服务器rsync的[模块]
    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync> #配置rsync
    <commonParams params="-artuz"/> #rsync的参数
    <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> #是否开启rsync的认证模式,需要配置users及passwordfile,根据情况开启(如果开启,注意密码文件权限一定要是600)
    <userDefinedPort start="false" port="874"/><!-- port=874 --> #远程目标服务器的端口不是默认端口时使用
    <timeout start="false" time="100"/><!-- timeout=100 --> #是否开启rsync的超时时间
    <ssh start="false"/>
    </rsync>
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #目标服务器传输失败时会重新传输,再次失败会写入rsync_fail_log中,每隔一段时间(timeToExecute)执行脚本再次传输
    <crontab start="false" schedule="600"><!--600mins--> #对监控目录与目标服务器每隔一段时间进行一次整体同步,默认600分钟,根据个人情况是否开启
    <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="https://blog.whsir.com"/>
    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
    </localpath>
    </plugin>
    </head>
    ---------------------end------------------------------------------------------------
    启动:   /usr/local/sersync/sersync2 -n 16 -d -o /usr/local/sersync/confxml.xml
    如果没有报错,就可以去测试文件是否能同眇了  ,这里开了16线程

    启动时,如下没产生报错就可以了
    [root@iZwz9dv2hn8jtmeie8ipc0Z sersync]# /usr/local/sersync/sersync2 -n 16 -d -o /usr/local/sersync/confxml.xml
    set the system param
    execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    parse the command param
    option: -n      thread num is:  16
    option: -d      run as a daemon
    option: -o      config xml name:  /usr/local/sersync/confxml.xml
    parse xml config file
    host ip : localhost     host port: 8008
    daemon start,sersync run behind the console
    config xml parse success
    please set /etc/rsyncd.conf max connections=0 Manually
    sersync working thread 18  = 1(primary thread) + 1(fail retry thread) + 16(daemon sub threads)
    Max threads numbers is: 50 = 18(Thread pool nums) + 32(Sub threads)
    please according your cpu ,use -n param to adjust the cpu rate
    run the sersync:
    watch path is: /home/www/project
    [root@iZwz9dv2hn8jtmeie8ipc0Z sersync]#


    查看文件同步进程
    [root@iZwz9dv2hn8jtmeie8ipc0Z ~]# ps -ef|grep rsync
    root      5249  6073  0 Dec06 ?        00:09:50 /bin/sh /tmp/rsync_fail_log.sh
    root      6073     1  0 Dec06 ?        00:00:04 /usr/local/sersync/sersync2 -n 16 -d -o /usr/local/sersync/confxml.xml
    root     10087  5249  0 09:34 ?        00:00:00 rsync -artuz -R ./lapu/Application/Runtime/Logs/Api/20_12_05.log 172.18.156.197::www
    root     10090  9623  0 09:34 pts/1    00:00:00 grep --color=auto rsync
    [root@iZwz9dv2hn8jtmeie8ipc0Z ~]#




























    ========================== 下面不用看,没用的  =====================================================================================================================


     


    ----------------- rsync 命令 ---------------------------------
    $ rsync -r source1 source2 destination
    上面命令中,source1、source2都会被同步到destination目录。
    -a参数可以替代-r,除了可以递归同步以外,还可以同步元信息(比如修改时间、权限等)。由于 rsync 默认使用文件大小和修改时间决定文件是否需要更新,所以-a比-r更有用。下面的用法才是常见的写法。
    $ rsync -a source destination
    如果只想同步源目录source里面的内容到目标目录destination,则需要在源目录后面加上斜杠

    -n 参数
    如果不确定 rsync 执行后会产生什么结果,可以先用-n或--dry-run参数模拟执行的结果。


    $ rsync -anv source/ destination
    上面命令中,-n参数模拟命令执行的结果,并不真的执行命令。-v参数则是将结果输出到终端,这样就可以看到哪些内容会被同步。

    $ rsync -av --delete source/ destination
    上面命令中,--delete参数会使得destination成为source的一个镜像。
    =======================================================================================
    下面是一个脚本示例,备份用户的主目录
    #!/bin/bash

    # A script to perform incremental backups using rsync

    set -o errexit
    set -o nounset
    set -o pipefail

    readonly SOURCE_DIR="${HOME}"
    readonly BACKUP_DIR="/mnt/data/backups"
    readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
    readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
    readonly LATEST_LINK="${BACKUP_DIR}/latest"

    mkdir -p "${BACKUP_DIR}"

    rsync -av --delete
      "${SOURCE_DIR}/"
      --link-dest "${LATEST_LINK}"
      --exclude=".cache"
      "${BACKUP_PATH}"

    rm -rf "${LATEST_LINK}"
    ln -s "${BACKUP_PATH}" "${LATEST_LINK}"
    # 上面脚本中,每一次同步都会生成一个新目录${BACKUP_DIR}/${DATETIME},并将软链接${BACKUP_DIR}/latest指向这个目录。
    下一次备份时,就将${BACKUP_DIR}/latest作为基准目录,生成新的备份目录。最后,再将软链接${BACKUP_DIR}/latest指向新的备份目录。
    =======================================================================================
    -------------------------------------------------------------

    A、推文件:  rsync -av /etc/passwd  192.168.204.168:/tmp/passwd.txt
    B、拉文件:  rsync -av  192.168.204.168:/tmp/passwd.txt  /tmp/test.txt
    指定ssh端口: rsync -av -e "ssh -p 22" 192.168.204.168:/tmp/passwd.txt  /tmp/a.txt
    ==========================================================================
    备份数据库:mysqldump --opt -uroot -p elapse > elapse.2020.08.19.sql
               mysqldump --opt -uroot -p tokay > tokay.sql
    恢复库:mysql -u root -p tokay < tokay.sql

    导出某些数据表:
    mysqldump -uusername -ppassword db1 table1 table2 > tb1tb2.sql
    导入某些数据表
    mysql -uusername -ppassword db1 < tb1tb2.sql






  • 相关阅读:
    CentOS下安装Git
    MongoDB 从入门到精通专题教程
    mongodb分布式集群架构
    CentOS6.2 下安装中文语言包
    英语流利说 第18天
    英语流利说 第17天
    英语流利说 第16天
    英语流利说 第15天
    英语流利说 第14天
    英语流利说 第13天
  • 原文地址:https://www.cnblogs.com/ccw869476711/p/14370565.html
Copyright © 2011-2022 走看看