欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

备份shell脚本实例代码

程序员文章站 2023-11-15 15:25:22
1、backup_run.sh复制代码 代码如下:    #!/bin/sh    # backup_run&n...
1、backup_run.sh
复制代码 代码如下:

    #!/bin/sh
    # backup_run
    # script to run the backups
    # loads in a setting file for the user to change
    source=/home/bob/backup.defaults
    check_source()
    {
        # check_source
        # can we load the file
        # backup.defaults is the source file containing config/functions
        # make sure your path includes this directory you are runing from
        if [ -r $source ];then
          . $source # load $source
        else
            echo "`basename $0`: cannot locate default file"
            exit 1
        fi
    }
    header()
    {
        # header
        user=`whoami`
        mydate=`date +%a" "%e" of "%b-%y`
        clear
        cat << hh
        user: $user $mydate
                    network system backup
                    =====================
    hh
    }
    change_settings()
    {
        # change_settings
        # let the user see the default settings..
        header
        echo "valid entries are..."
        echo "tape device: rmt0,rmt1,rmt3"
        echo "mail admin:yes,no"
        echo "backup type: full,normal,sybase "
        while :
        do
            echo -n -c "\n\n tape device to be used for this backup [$_device] :"
            read t_device
            : ${t_device:=$_device}
            case $t_device in
                rmt0|rmt1|rmt3) break

                *) echo "the device are either...rmt0,rmt1,rmt3"

            esac
        done
        # if the user hits return on any of the fields, the default value will be used
        while :
        do
            echo -n "mail admin when done [$_inform] : "
            read t_inform
            : ${t_inform:=$_inform}
            case $t_inform in
                yes|yes) break

                no|no) break

                *) echo "the choices are yes,no"

            esac
        done
        while :
        do
            echo -n "backup type [$_type] :"
            read t_type
            : ${t_type:=$_type}
            case $t_type in
                full|full) break

                normal|normal) break

                sybase|sybase) break

                *) echo "the choices are either ... full,normal,sybase"
            esac
        done
        # re-assign the temp varialbes back to original variables that
        # were loaded in
        _device=$t_device;_inform=$t_inform;_type=$t_type
    }
    show_settings()
    {
        # display current settings
        cat << hh
                    default settings are...
                tape device to be used : $_device
                mail admin when done : $_inform
                type of backup : $_type
                log file of backup : $_logfile
    hh
    }
    get_code()
    {
        # users get 3 attempts at entering the correct code
        # _code is loaded in from the source file
        clear
        header
        _counter=0
        echo "you must enter the correct code to be able to change default settings"
        while :
        do
            _counter=`expr $_counter + 1`
            echo -n "enter the code to change the settings: "
            read t_code
            # echo $_counter
            if [ "$t_code" = "$_code" ];then
                return 0
            else
                if [ "$_counter" -gt 3 ];then
                    echo "sorry incorrect code entered, you cannot change the settings..."
                    return 1
                fi
            fi
        done
    }
    check_drive()
    {
        # make sure we can rewind the tape
        mt -f /dev/$_device rewind > /dev/null 2>&1
        if [ $? -ne 0 ];then
            return 1
        else
            return 0
        fi
    }
    #====================== main =======================
    # can we source the file
    check_source
    header
    # display the loaded in variables
    show_settings
    # ask user if he/she wants to change any settings
    if continue_prompt "do you wish to change some of the system defaults" "y";
    then
        echo $?   
        # yes then enter code name
        if get_code;then
            # change some settings
            change_settings
        fi
    fi
    #------------------ got settings... now do backup
    if check_drive;then
        echo "tape ok..."
    else
        echo "cannot rewind the tape..is it in the tape drive???"
        echo "check it out"
        exit 1
    fi
    # file system paths to backup
    case $_type in
        full|full)
            backup_path="sybase syb/suppor etc var bin apps usr/local"

        normal|normal)
            backup_path="etc var bin apps usr/local"

        sybase|sybase)
            backup_path="sybase syb/suppor"

    esac
    # now for backup
    cd /
    echo "now starting backup......"
    find $backup_path -print | cpio -ovb -o /dev/$_device >> $_logfile 2>&1
    # if the above cpio does not work on your system try cpio below, instead
    # find $backup_path -print | cpio -ovb > /dev/$_device >> $_logfile 2>&1
    # to get more information on the tape change -ovb to -ovcc66536
    if [ "$_inform" = "yes" ];then
        echo "backup finished check the log file" | mail admin
    fi

2、backup.defaults
复制代码 代码如下:

    #!/bin/sh
    # backup.defaults
    # configuration default file for network backups
    # edit this file at your own
    # name backup.defaults
    # --------------------------------------------
    # not necessary for the environments to be in quotes.. but
    _code="comet"
    _logfile="/appdva/backup/log.`date +%y%m%d`"
    _device="rmt0"
    _inform="yes"
    _type="full"
    #---------------------------------------------
    continue_prompt()
    {
        # continue_prompt
        # to call: continue_prompt "string to display" default_answer
        _str=$1
        _default=$2
        # check we have the right params
        if [ $# -lt 1 ];then
            echo "continue_prompt: i need a string to display"
            return 1
        fi
        while :
        do
            echo -n "$_str [y..n] [$_default]:"
            read _ans
            if [ "$_ans" = "" ];then
                : ${_ans:=$_default}
                case $_ans in
                    y) return 0

                    n) return 1

                esac
            fi
            # user has selected something
            case $_ans in
                y|y|yes|yes)
                    return 0

                n|n|no|no)
                    return 1

                *) echo "answer either y or n, default is $_default"

            esac
            echo $_ans
        done
    }

3、运行:
复制代码 代码如下:

$./backup_run.sh backup.defaults