autoDeploy/dbUpgrade.sh
zhangkun9038@dingtalk.com 03899e7e0f mkdir -p
2022-10-24 17:17:17 +00:00

102 lines
2.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#dbUpgrade.sh dbUpdate.sql "192.168.96.36" "root" "xtph9638642" "3306" "/tmp/autoDeploy" "ztupload/stage/sql"
sqlFile=$1
dbHost=$2
dbUser=$3
dbPassword=$4
dbPort=$5
tempPath=$6
ossPath=$7
projectName="sql"
projectPath=${tempPath}/${projectName}
function init(){
cd $tempPath
if [ ! -d $projectName ]; then
mkdir $projectName -p
fi
}
function compareMd5(){
cd $tempPath
oriSum=$(md5sum $sqlFile)
oriSum="${oriSum% *}"
desSum=""
echo "origin file $sqlFile md5sum: $oriSum"
cd $projectPath
if [ ! -f "$sqlFile" ];then
cp $tempPath/$sqlFile .
return 1
else
desSum=$(md5sum $sqlFile)
desSum="${desSum% *}"
echo "current path: $(pwd)"
echo "destination file ${sqlFile} md5sum: ${desSum}"
fi
if [ ${oriSum} = ${desSum} ];then
return 0
else
return 1
fi
}
function replaceAndExec(){
cd $projectPath
rm $sqlFile
rm sqlOut.log
cp $tempPath/$sqlFile .
/usr/bin/mysql -u${dbUser} -p${dbPassword} -P${dbPort} -e "$(cat ${sqlFile})" | tee sqlOut.log
~/ossutil cp -f sqlOut.log oss://${ossPath}/${dbHost}/sqlOut.log
}
function uploadLog()
{
ctype=$1
identify=$2
count=$3
echo "current user is ${USER}"
journalctl -n $count -xe -${ctype} $identify > ${tempPath}/${identify}.log
~/ossutil cp -f ${tempPath}/${identify}.log oss://${ossPath}/${dbHost}/${identify}.log
if [ $? -eq 0 ]; then
echo "ok: log ${identify} uploaded to oss!"
return 0
else
echo "error: upload log ${identify} to oss failed!"
return 1
fi
}
function main() {
echo " "
sleep 5
echo "dbUpgrade start"
init
compareMd5
result=$?
if [ ${result} = 0 ];then
echo "local temp file ${sqlFile} is same with ${projectPath}no need to replace"
echo "ok: dbUpgrade done"
echo " "
exit 0
else
echo "local temp file $sqlFile is different from ${projectPath}, will replace and execusing..."
fi
replaceAndExec
result=$?
if [ $result = 0 ];then
echo "ok: replace and execuse done"
else
echo "error: replace and execuse faild"
exit 0
fi
sleep 3
uploadLog t autoDeploy 100
sleep 1
}
main