备份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

(0)

相关推荐

  • 备份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    

  • 实现自动清除日期目录shell脚本实例代码

    实现自动清除日期目录shell脚本实例代码 很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过find结合rm或者delete显得有些力不从心.本文提供一个简单的小脚本,可以嵌入到其他脚本,也可直接调用,如下文供大家参考. 1.脚本内容 [root@SZDB ~]# more purge_datedir.sh #!/bin/bash # Author: Leshami # Blog : http://blog.csdn.net/l

  • 非常实用的23个Shell脚本实例

    shell脚本是帮助程序员和系统管理员完成费时费力的枯燥工作的利器,是与计算机交互并管理文件和系统操作的有效方式.区区几行代码,就可以让计算机接近按照你的意图行事. 为大家整理了23个实例,通过23个实战经典脚本实例,展示了shell脚本编程的实用技术和常见工具用法.大家只需根据自己的需求,将文中这些常见任务和可移植自动化脚本推广应用到其他类似问题上,能解决那些三天两头碰上的麻烦事. #!/bin/bash ##################################### #检测两台服

  • 输出执行操作和打印日志的shell脚本实例

    cat /mnt/log_function.sh #!/bin/bash #log function ####log_correct函数打印正确的输出到日志文件 function log_correct () { DATE=`date "+%Y-%m-%d %H:%M:%S"` ####显示打印日志的时间 USER=$(whoami) ####那个用户在操作 echo "${DATE} ${USER} execute $0 [INFO] $@" >>/v

  • linux下mysql如何自动备份shell脚本

    Linux 服务器上的程序每天都在更新 MySQL 数据库,于是就想起写一个 shell 脚本,结合 crontab,定时备份数据库.其实非常简单,主要就是使用 MySQL 自带的 mysqldump 命令. #!/bin/bash # Shell script to backup MySql database # To backup Nysql databases file to /backup dir and later pick up by your # script. You can s

  • 超详细的5个Shell脚本实例分享(值得收藏)

    概述 今天主要分享5个shell脚本实例,大家可以借鉴下里面的思路,看下有没另外一种实现方式. 1.定时清空文件内容,定时记录文件大小 #!/bin/bash ################################################################ #每小时执行一次脚本(任务计划),当时间为0点或12点时,将目标目录下的所有文件内 #容清空,但不删除文件,其他时间则只统计各个文件的打小,一个文件一行,输出到以时#间和日期命名的文件中,需要考虑目标目录下二级

  • linux中mysql备份shell脚本代码

    第一步:在你的linux服务器中定义备份目录: 复制代码 代码如下: mkdir /var/lib/mysqlbackup cd /var/lib/mysqlbackup 第二步:下面是最重要的一步了,就是写定时备份脚本. 复制代码 代码如下: vi dbbackup.sh 代码文件如下 复制代码 代码如下: #!/bin/sh# mysql data backup script## use mysqldump --help,get more detail.#BakDir=/root/back/

  • php管理nginx虚拟主机shell脚本实例

    本文实例讲述了php管理nginx虚拟主机shell脚本,分享给大家供大家参考.具体分析如下: 使用php作为shell脚本是一件很方便的事情.理所当然,我们可以使用php脚本来管理 nginx虚拟主机,下面是笔者的 脚本 文件供各位参考: 复制代码 代码如下: #!/usr/bin/php -q <?php   start: fwrite(STDOUT,"===========Vhost Script===========\n"); fwrite(STDOUT,"=

  • Linux下文件剪切的shell脚本实现代码

    需求描述 编写shell脚本实现Linux下不同目录(路径)之间的文件的剪切(移动)操作. 其中,文件移动之前所在的目录称为源目录,文件移动之后所在的目录称为目的目录.要求当源目录不存在.源目录下无文件及剪切文件成功时,均要在屏幕上输出相关的日志信息:并且,在程序执行之前,只有源目录是存在的,目的目录需要由程序创建. shell脚本 umask 0000 if [ -d $1 ] then fcnt=`ls -l $1 | wc -l` if [ $fcnt -ne 1 ] then mkdir

  • linux下实现ftp自动备份shell脚本

    利用here文档 复制代码 代码如下: #!/bin/sh ftp -ivn 210.29.28.124 <<EOF user yun yun2011 lcd /home/veyun cd /home/veyun/yhb get v bye EOF 解释: -i :关闭多文件传输过程中的交互提示,所以不会再有让用户输入用户名和密码的提示 -n:阻止了初始连接时的自动登录 -v:将ftp命令设置为verbose模式,从而可以在会话时看到其中的命令 在本例中使用用户名yun和密码yun2011登录

随机推荐