แสดงบทความที่มีป้ายกำกับ Mysql แสดงบทความทั้งหมด

วันเสาร์ที่ 31 ตุลาคม พ.ศ. 2558

[HOW TO] Script Backup Mysql Log (Shell Script)

[HOW TO] Script Backup Mysql Log (Shell Script)



#!/bin/bash
###################################################
#      ____             _               ____      #
#     / ___|_   _ _   _| |_ ___  _ __  / ___|     #
#    | |  _| | | | | | | __/ _ \| '_ \| |  _      # 
#    | |_| | |_| | |_| | || (_) | | | | |_| |     #
#     \____|\__,_|\__,_|\__\___/|_| |_|\____|     #
#                                                 #
###################################################
#            ScriptMySqlLogBackup.sh              #
###################################################

timestamp=$(date +"%Y%m%d")
MyHost="localhost"
MyUser="root"
MyPass=""
logDir="/var/log/mysql"
moveToDir=""

echo "[INFO] $(date +"%Y%m%d %T") : mysql log backup start";

mv $logDir/mysql.log $logDir/mysql.log.$timestamp
mv $logDir/mysql_error.log $logDir/mysql_error.log.$timestamp
mv $logDir/mysql-slow.log $logDir/mysql-slow.log.$timestamp

if [ -f "$logDir/mysql.log" ]
then
   echo "[WARN] $(date +"%Y%m%d %T") : mysql.log Move Failed";
else
   echo "[INFO] $(date +"%Y%m%d %T") : mysql.log Move Successed";
fi

if [ -f "$logDir/mysql_error.log" ]
then
   echo "[WARN] $(date +"%Y%m%d %T") : mysql_error.log Move Failed";
else
   echo "[INFO] $(date +"%Y%m%d %T") : mysql_error.log Move Successed";
fi

if [ -f "$logDir/mysql-slow.log" ]
then
   echo "[WARN] $(date +"%Y%m%d %T") : mysql-slow.log Move Failed";
else
   echo "[INFO] $(date +"%Y%m%d %T") : mysql-slow.log Move Successed";
fi

if [ -n "$MyPass" ]
then
   /usr/bin/mysqladmin --host=$MyHost --user=$MyUser --password=$MyPass flush-logs
else 
   /usr/bin/mysqladmin --host=$MyHost --user=$MyUser flush-logs
fi

echo "[INFO] $(date +"%Y%m%d %T") : flush-logs Successed";

zip $logDir/mysql.log.$timestamp.zip $logDir/mysql.log.$timestamp
zip $logDir/mysql_error.log.$timestamp.zip $logDir/mysql_error.log.$timestamp
zip $logDir/mysql-slow.log.$timestamp.zip $logDir/mysql-slow.log.$timestamp

if [ -f "$logDir/mysql.log.$timestamp.zip" ]
then
   echo "[INFO] $(date +"%Y%m%d %T") : mysql.log.$timestamp zip Successed";
   rm -rf $logDir/mysql.log.$timestamp
   echo "[INFO] $(date +"%Y%m%d %T") : mysql.log.$timestamp Deleted";
else 
   echo "[WARN] $(date +"%Y%m%d %T") : mysql.log.$timestamp zip Failed";
fi

if [ -f "$logDir/mysql_error.log.$timestamp.zip" ]
then
   echo "[INFO] $(date +"%Y%m%d %T") : mysql_error.log.$timestamp zip Successed";
   rm -rf $logDir/mysql_error.log.$timestamp
   echo "[INFO] $(date +"%Y%m%d %T") : mysql_error.log.$timestamp Deleted";
else 
   echo "[WARN] $(date +"%Y%m%d %T") : mysql_error.log.$timestamp zip Failed";
fi

if [ -f "$logDir/mysql-slow.log.$timestamp.zip" ]
then
   echo "[INFO] $(date +"%Y%m%d %T") : mysql-slow.log.$timestamp zip Successed";
   rm -rf $logDir/mysql-slow.log.$timestamp
   echo "[INFO] $(date +"%Y%m%d %T") : mysql-slow.log.$timestamp Deleted";
else 
   echo "[WARN] $(date +"%Y%m%d %T") : mysql-slow.log.$timestamp zip Failed";
fi


if [ -n "$moveToDir" ]
then
   if [ -f "$logDir/mysql.log.$timestamp.zip" ]
   then
       mv $logDir/mysql.log.$timestamp.zip $moveToDir/mysql.log.$timestamp.zip
       echo "[INFO] $(date +"%Y%m%d %T") : move mysql.log.$timestamp.zip to $moveToDir";
   else 
      echo "[WARN] $(date +"%Y%m%d %T") : mysql.log.$timestamp.zip not found or move Failed";
   fi

   if [ -f "$logDir/mysql_error.log.$timestamp.zip" ]
   then
      mv $logDir/mysql_error.log.$timestamp.zip $moveToDir/mysql_error.log.$timestamp.zip
      echo "[INFO] $(date +"%Y%m%d %T") : move mysql_error.log.$timestamp.zip to $moveToDir";
   else 
      echo "[WARN] $(date +"%Y%m%d %T") : mysql_error.log.$timestamp.zip not found or move Failed";
   fi

   if [ -f "$logDir/mysql_error.log.$timestamp.zip" ]
   then
       mv $logDir/mysql_slow.log.$timestamp.zip $moveToDir/mysql_slow.log.$timestamp.zip
       echo "[INFO] $(date +"%Y%m%d %T") : move mysql_slow.log.$timestamp.zip to $moveToDir";
   else 
      echo "[WARN] $(date +"%Y%m%d %T") : mysql_slow.log.$timestamp.zip not found or move Failed";
   fi
else 
   echo "[WARN] $(date +"%Y%m%d %T") : Move Failed";
fi

echo "[INFO] $(date +"%Y%m%d %T") : mysql log backup finish";
#============================
#edit ScriptMySqlLogBackup.sh
#============================
MyHost="localhost"
MyUser="root"
MyPass=""
logDir="/var/log/mysql"

#============================
#edit crontab
#============================
crontab -e

0 23 * * * /path/to/ScriptMySqlLogBackup/ScriptMySqlLogBackup.sh > /path/to/ScriptMySqlLogBackup/ScriptMySqlLogBackup.log