autoDeploy/dbUpgrade.sh
2025-02-25 21:52:14 +08:00

112 lines
2.5 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}
filename_no_extension="${sqlfile%.sql}"
dbName="${filename_no_extension:0:3}"
protal=oss
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 .
# 获取当前日期并格式化为YYYY-MM-DD的形式
current_date=$(date +'%Y-%m-%d')
# 获取当前时间并格式化为HH:MM:SS的形式
current_time=$(date +'%H:%M:%S')
fileName="abc_${current_date}-${current_time}.sql"
/usr/bin/mysqldump -u${dbUser} -p${dbPassword} -P${dbPort} $dbname > $fileName
/usr/bin/mysql -u${dbUser} -p${dbPassword} -P${dbPort} -e "$(cat ${sqlFile})" | tee sqlOut.log
ossutil cp -f sqlOut.log ${protol}://${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 ${protol}://${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