备份shell脚本实例代码

 更新时间:2013年02月11日 15:23:10   作者:  
备份shell脚本一例,有需要的朋友可以参考下
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

相关文章

  • Shell根据web日志计算平均连接时间功能

    Shell根据web日志计算平均连接时间功能

    这篇文章主要介绍了Shell根据web日志计算平均连接时间功能,本文给出了原代码和自己修改后的代码,需要的朋友可以参考下
    2014-12-12
  • shell脚本中set -e选项作用范围小结

    shell脚本中set -e选项作用范围小结

    本文主要介绍了shell脚本中set -e选项作用范围小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • linux find命令将查找到的文件批量删除方法

    linux find命令将查找到的文件批量删除方法

    这篇文章主要介绍了linux find命令将查找到的文件批量删除,文中给大家补充介绍了Linux中find三种删除方式,常用于crontab定时任务和shell脚本,需要的朋友可以参考下
    2022-12-12
  • shell在指定目录下批量执行sql脚本的实例

    shell在指定目录下批量执行sql脚本的实例

    今天小编就为大家分享一篇shell在指定目录下批量执行sql脚本的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • Shell编程之免交互的实现示例

    Shell编程之免交互的实现示例

    对于Linux操作系统中,有许多操作都会触及到交互,本文主要介绍了Shell编程之免交互,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-06-06
  • Linux下快速比较两个目录的不同(多种方法)

    Linux下快速比较两个目录的不同(多种方法)

    这篇文章主要介绍了Linux下快速比较两个目录的不同,本文给大家带来了多种方法,非常不错,具有一定的参考借鉴价值,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-05-05
  • Linux自定义防误删脚本的思路与测试

    Linux自定义防误删脚本的思路与测试

    相信很多朋友都遇到过在linux下误删除文件的时候,此刻的心中仿佛有无数的羊驼在奔腾,下面这篇文章主要给大家介绍了关于Linux自定义防误删脚本的思路与测试的相关资料,需要的朋友可以参考下
    2021-09-09
  • 如何解决 shell 脚本重复执行的问题

    如何解决 shell 脚本重复执行的问题

    假如执行备份脚本消耗的时间远大于设置的备份间隔的话,系统会出现多个同时在执行脚本的Bash实例,会占用大量的系统资源,进而影响正常业务程序的运行,那如何解决上述shell脚本重复执行的问题呢,本文将要介绍的 flock 命令可以解决这个问题
    2021-05-05
  • shell读取配置文件的方式sed命令详解

    shell读取配置文件的方式sed命令详解

    在编写启动脚本时,涉及到读取配置文件,特地记录下shell脚本读取启动文件的方式,这篇文章主要介绍了shell读取配置文件-sed命令,需要的朋友可以参考下
    2023-04-04
  • Linux 终端中命令输出保存到文件中的方法

    Linux 终端中命令输出保存到文件中的方法

    这篇文章主要介绍了如何将 Linux 终端中命令的输出保存到文件中实例操作,操作步骤非常详细,有需要的小伙伴可以按步骤来研究下吧
    2020-12-12

最新评论