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

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

 

标签:-%,db2inst1,shell,crotab,Linux,fnLog,logInfo,backup,dbName
来源: https://www.cnblogs.com/fanwenchao/p/15531660.html