作者: 薄荷冰           Linux基础  |  网站架构  |  数据库  |  监控报警  |  shell系列  |  常用工具  |  常见问题


 

导航

 
#!/bin/bash
############################################################
# $Name:         rsync_backup.lib
# $Version:      v1.0
# $Functions:
#                1 function_server_rsyncd_backup
#                2 function_client_rsync_backup
#                3 function_install_sersync
#                4 source /tmp/scripts/tools_scripts/create_backup_cron_script.lib
#                    4.1 function_create_server_rsync_bak_script "${cron_scripts}" "${backup_dir}" "${localpath_watch}"
#                    4.2 function_create_client_rsync_bak_script "${cron_scripts}" "${backup_dir}" "${localpath_watch}" \
#                                                                "${passwordfile}" "${module_backup}" 'store-rsync-01'
# $Author:       Jeson Li
# $organization: www.lzbbg.cn
# $Create Date:  2017-03-10
# $Mail:         libobin@tom.com
# $Description:  Linux data backup
############################################################
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
Tik="\e[5m";Red="\e[31m";Grn="\e[32m";Ylw="\e[33m";Blu="\e[34m";Pup="\e[35m";Rst="\e[0m"
# Globle variable
    IP="`grep "$(hostname)" /etc/hosts|awk 'NR==1{print $1}'|head -1`"
    cron_scripts='/server/cron_scripts'
    auth_users="rsync_backup"
    passwordfile="/etc/rsyncd.passwd"
# Defining rsync variable
    rsync_user="nobody"
    backup_dir="/backup/"
    hosts_allow="${IP%.*}.0/24" 
    module_backup="backup"
    auth_passwd="jeson123456"
    mkdir -p $cron_scripts
# Use in function_install_sersync
    DIR_versions="/apps/versions"
    DIR_links="/apps/links"
    sersync_packet='sersync2.5.4_64bit_binary_stable_final.tar.gz'
    download_URL='http://download.lzbbg.cn:81/CentOS7/packets_tar/'
    localpath_watch='/nfs_data/'
    remote_ip='store-rsync-01'
    module_sersync='nfs_data'
    rsync_params='-rz'
    auth_start='true'    
source /tmp/scripts/tools_scripts/create_backup_cron_script.lib

function_server_rsyncd_backup(){
    id ${rsync_user} &>/dev/null || useradd ${rsync_user} -u 873 -M -s /sbin/nologin
    echo "${auth_users}:${auth_passwd}" >${passwordfile}
    chmod 600 ${passwordfile}
    mkdir -p ${backup_dir} ${localpath_watch}
    chown -R ${rsync_user}.${rsync_user} ${backup_dir} ${localpath_watch}
    \cp /etc/rsyncd.conf{,.$(date +%F)}
    cat > /etc/rsyncd.conf <<-EOF
        uid = ${rsync_user}
        gid = ${rsync_user}
        use chroot = no
        max connections = 2000
        pid file = /var/run/rsyncd.pid
        log file = /var/log/rsyncd.log
        lock file = /var/lock/rsyncd.lock
        read only = false
        list = false
        auth users = ${auth_users}
        secrets file = ${passwordfile}
        hosts allow = ${hosts_allow}
        ignore errors
        timeout = 600
        [${module_backup}]
          path = ${backup_dir}
          comment = Daily data ${module_backup}
        [${module_sersync}]
          path = ${localpath_watch}
          comment = Daily data ${module_sersync}
    EOF
    ### rsyncd boot from the startup
    systemctl start  rsyncd
    systemctl enable rsyncd
    ### Create server cron script
    function_create_server_rsync_bak_script "${cron_scripts}" "${backup_dir}" "${localpath_watch}"
 }

function_client_rsync_backup(){
    echo "${auth_passwd}" >${passwordfile}
    chmod 600 ${passwordfile}
    ### Create server cron script
    function_create_client_rsync_bak_script "${cron_scripts}" "${backup_dir}" "${localpath_watch}" "${passwordfile}" "${module_backup}" 'store-rsync-01'
}

function_install_sersync(){
    wget -O /tmp/${sersync_packet} ${download_URL}${sersync_packet}
    tar    xzf /tmp/${sersync_packet} -C ${DIR_versions}
    ln -s ${DIR_versions}/GNU-Linux-x86 ${DIR_links}/sersync
    \cp ${DIR_links}/sersync/confxml.xml{,.$(date +%F).$(whoami)}
    cat >${DIR_links}/sersync/confxml.xml<<-EOF
        <?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="true"/>
            <closeWrite start="true"/>
            <moveFrom start="true"/>
            <moveTo start="true"/>
            <attrib start="false"/>
            <modify start="true"/>
            </inotify>

            <sersync>
            <localpath watch="${localpath_watch}">
                <remote ip="${remote_ip}" name="${module_sersync}"/>
                <!--<remote ip="192.168.8.39" name="tongbu"/>-->
                <!--<remote ip="192.168.8.40" name="tongbu"/>-->
            </localpath>
            <rsync>
                <commonParams params="${rsync_params}"/>
                <auth start="${auth_start}" users="${auth_users}" passwordfile="${passwordfile}"/>
                <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>
        </head>
    EOF
    [ `grep "^DIR_links=" /etc/profile` ] || echo "DIR_links=$DIR_links" >>/etc/profile
    echo -e "# Sersync Env created $(date +%F) from $(whoami)\n"'export PATH=$DIR_links/sersync:$PATH' >>/etc/profile
    chmod +x /etc/rc.d/rc.local
    echo -e "### Sersync boot from the startup for $(date +%F) from $(whoami)###\n$DIR_links/sersync/sersync2 -rdo ${DIR_links}/sersync/confxml.xml" >>/etc/rc.d/rc.local
    echo '50000000' > /proc/sys/fs/inotify/max_user_watches
    echo '327679' > /proc/sys/fs/inotify/max_queued_events
    $DIR_links/sersync/sersync2 -rdo ${DIR_links}/sersync/confxml.xml
 }

 

posted on 2018-12-27 20:15  薄荷冰  阅读(149)  评论(0)    收藏  举报