通常一个企业会用一台机子做备份机,其他机器的数据都备份到这台机器上,机器少还好,如果一次要增加的机器多了,那要增加配置就很麻烦。写了一个小脚本,能够增加和删除该服务器的配置。

原理是根据每台服务器的IP来设置一段配置,所以服务器上要备份到备份机上来也得做对应的配置。使用前要先看清配置,免得出了问题啊。

#!/bin/bash
# ./rsyncd.sh (add|del)IP
# By Chen Zhidong
# http://sillydong.com

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

CONFPATH="/xxxxxxx"
PASSPATH="/xxxxxxx"

if [ $(id -u) != "0" ];then
    echo "Error: You must be root to run this script!"
    exit 1
fi

if [ $# -lt 2 ]; then
    echo "Syntax: ./rsyncd.sh (add|del) IP"
    exit
fi

case $1 in
add)
    read -p "Sure to add \"$2\" into $CONFPATH?(Y/n)" choice1
    case $choice1 in
        n|N)
            echo "Didn't add any thing!"
        ;;
        *)
            mkdir /backup/vhosts/tars/${2}
            mkdir /backup/vhosts/rsyncfiles/${2}
            mkdir /backup/mysql/rsyncfiles/${2}

            #for db
            echo "[${2}_db_lio]" >> $CONFPATH
            echo "path = /backup/mysql/rsyncfiles/${2}" >> $CONFPATH
            echo "uid = root" >> $CONFPATH
            echo "gid = root" >> $CONFPATH
            echo "read only = false" >> $CONFPATH
            echo "ignore errors" >> $CONFPATH
            echo "secrets file = $PASSPATH" >> $CONFPATH
            echo -e "hosts allow = ${2}\n" >> $CONFPATH

            #for www
            echo "[${2}_www_lio]" >> $CONFPATH
            echo "path = /backup/vhosts/rsyncfiles/${2}" >> $CONFPATH
            echo "uid = root" >> $CONFPATH
            echo "gid = root" >> $CONFPATH
            echo "read only = false" >> $CONFPATH
            echo "ignore errors" >> $CONFPATH
            echo "secrets file = $PASSPATH" >> $CONFPATH
            echo -e "hosts allow = ${2}\n" >> $CONFPATH

            tail -n 18 $CONFPATH
        ;;
    esac
;;
del)
    read -p "Sure to delete \"$2\" from $CONFPATH?(y/N)" choice2
    case $choice2 in
        y|Y)
            CLEAN=0
            until [ "$CLEAN"  = "1" ]
            do
                line=`grep "${2}_" $CONFPATH -n | awk -F: '{print $1}'`
                if [ -n "$line" ];then
                    del=`echo $line | sed 's/ .*//'`
                    for k in 1 2 3 4 5 6 7 8 9
                    do
                        sed -i "$del d" $CONFPATH
                        let "k++"
                    done
                else
                    CLEAN=1
                fi
            done
            echo "Delete configure data for ${2} finished!"
        ;;
        *)
            echo "Didn't do any change!"
        ;;
    esac
;;
*)
    echo "You may use this script like this: ./rsyncd.sh (add|del) IP"
;;
esac

#End