跳转至

32. 脚本:rsync管理

4.6 rsync管理脚本

vim my_rsync

#!/bin/bash
#1.内置美化输出日志代码
lsb_functions="lib/lsb/init-functions"
if test -f $lsb_functions;then
    . $lsb_functions
else
    init_functions="/etc/init.d/functions"
    if test -f $init_functions;then
        . $init_functions
    fi
    log_success_msg(){
        echo "SUCCESS! $@"
    }
    log_failure_msg(){
        echo " ERROR! $@"
    }
fi

#2.提示信息
function usage(){
    echo "Usage: $0 {start|stop|restart}"
    exit 1
}

#3.start功能
function start(){
    /usr/bin/rsync --daemon
    sleep 1
    if [ `netstat -tunlp |grep rsync |wc -l` -ge "1" ];then
        log_success_msg "rsyncd is started"
    else
        log_failure_mse "rsyncd is not started"
    fi
}

#4.stop功能
function stop(){
    killall rsync &>/dev/null
    sleep 1
    if [ `netstat -tunlp |grep rsync |wc -l` -eq 0 ];then
        log_success_msg "rsyncd is stopped"
    else
        log_failure_msg "rsyncd is not stopped"
    fi
}

#5.restart功能
function restart(){
    stop
    start
}

#6.脚本程序入口
function main(){
    if [ "$#" -ne 1 ];then
        usage
    fi

    if [ "$1" = "start" ];then
        start
    elif [ "$1" = "stop" ];then
        stop
    elif [ "$1" = "restart" ];then
        restart
    else
        usage
    fi
}

#7.调用函数
main $*
chmod +x /etc/init.d/my_rsync

最后更新: 2022-02-19 13:59:07