crotab+shell实现DB2自动备份表结构和数据(Linux)

2021/11/10 2:09:45

本文主要是介绍crotab+shell实现DB2自动备份表结构和数据(Linux),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#!/bin/bash -x
#设置环境变量为了crontab识别db2look,db2move命令
. /etc/profile
. ~/.profile
. /home/db2inst1/sqllib/db2profile

ymd=$(date +%Y-%m-%d)
instanceName="db2inst1";

#/mnt/backup/把window的共享路径绑定到Llinux路径,通过SMP
#mount -t cifs 
basePath="/mnt/backup/${ymd}_auto_backup/${instanceName}";
logPath="${basePath}/log/"
logName="${logPath}/backup_${instanceName}_${ymd}.log"

#执行成功日志打印路径
function logInfo ()
{
    if [ ! -d ${logPath}  ]
    then
        mkdir -p ${logPath}
    fi

    DATE_N=$(date "+%Y-%m-%d %H:%M:%S")
    USER_N=$(whoami)
    echo "${DATE_N} ${USER_N} execute $0 [INFO] $@" >>$logName 

}
 #执行失败日志打印路径
function log_error ()
{
    DATE_N=$(date "+%Y-%m-%d %H:%M:%S")
    USER_N=$(whoami)

    echo -e "\033[41;37m ${DATE_N} ${USER_N} execute $0 [ERROR] $@ \033[0m"  >>$logName

}

function fnLog ()  {
    if [  $? -eq 0  ]
    then
        logInfo "$@ sucessed."
        echo -e "\033[32m $@ sucessed. \033[0m"
    else
        log_error "$@ failed."
        echo -e "\033[41;37m $@ failed. \033[0m"
    fi
}

function daochuSchema() {


    dbName=$1;
    schemaPath="${basePath}/${instanceName}_${dbName}_Schema_Bak/"

    db2 connect to ${dbName};
    fnLog "db2 connect to ${dbName};";

    if [ ! -d "$schemaPath" ]; 
    then 
        mkdir -p ${schemaPath};
        fnLog "mkdir -p ${schemaPath};";

    fi 
    cd ${schemaPath};

    db2look -d ${dbName} -e -a -l -o "${dbName}_${ymd}".sql;
    fnLog "db2look -d ${dbName} -e -a -l -o "${dbName}_${ymd}".sql";


}


function daochuData() {


    dbName=$1;

    dataPath="${basePath}/${instanceName}_${dbName}_Data_Bak"


    #db2 force applications all;
    db2 connect to ${dbName};
    fnLog "db2 connect to ${dbName};";

    if [ ! -d $dataPath ] 
    then 
        mkdir -p ${dataPath};
        fnLog "mkdir -p ${dataPath};";

    fi 
    cd ${dataPath};

    db2move ${dbName} export -aw
    fnLog "db2move ${dbName} export -aw";
}

daochuSchema "SchemaName";
logInfo 'daochuSchema "db2inst1" "SchemaName";'
daochuSchema "SchemaName1";
logInfo 'daochuSchema "db2inst1" "SchemaName1";'

daochuData "SchemaName";
logInfo 'daochuData "db2inst1" "SchemaName";'
daochuData "SchemaName1";
logInfo 'daochuData "db2inst1" "SchemaName1";'
exit;


#backup.sh修改权限777,配置crontab
#* */1 * * db2inst1 "/root/shell/backup.sh"

  相关链接:

https://blog.csdn.net/cshongye/article/details/105848597

https://www.cnblogs.com/smitty/p/7356730.html

环境变量配置:

https://blog.csdn.net/u012240363/article/details/88301033

DB2新建实例:

https://blog.csdn.net/super712/article/details/17118525

https://blog.csdn.net/weixin_41282397/article/details/81113014

 

https://blog.csdn.net/fuwencaho/article/details/30881427

 



这篇关于crotab+shell实现DB2自动备份表结构和数据(Linux)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程