#! /bin/bash
host="XXXXXX"
port="XXXXXX"
userName="XXXXXX"
password="XXXXXX"
dbName="XXXXXX"
currPath="$PWD"
cmd="SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM information_schema.TABLES WHERE TABLE_SCHEMA='${dbName}'"
rm -f ${currPath}/${dbName}"_truncate.sql"
mysql -h${host} -u${userName} -p${password} ${dbName} -P${port} -N -e "${cmd}">${currPath}/${dbName}"_truncate.sql"
if [ ! -d "${currPath}/logs" ]; then
mkdir ${currPath}/logs
fi
logPath=${currPath}"/logs/"${dbName}"_truncate_log"$(date +%Y-%m-%d-%H-%M-%S)
mysql -h${host} -u${userName} -p${password} ${dbName} -P${port} -e "source ${currPath}/${dbName}"_truncate.sql"" -vvv|tee -a ${logPath}
echo "Clear data of ${dbName} successful"
rm -f ${currPath}/${dbName}"_truncate.sql"