MySQL最典型和實用的熱備指令碼實戰
對於 D BA 或運維人員來講,備份恢復是最基本也是最必要的技能之一,一個通用並且穩定的熱備指令碼能夠極大的釋放工程師的時間和精力,本篇提供 MySQL 資料庫最常用的熱備指令碼,希望能夠幫助到大家。
1 、 MySQL 邏輯熱備指令碼( Linux 版)
指令碼程式碼:
#!/bin/bash . $HOME/.bash_profile BACKUPDIR=/home/backup LOGFILE=$BACKUPDIR/mysql_logicbak.log IPADDR=192.168.239.51 USER=root PASSWORD=Abcd321# PORT=3306 TIME1=`date +"%Y-%m-%d %H:%M:%S"` CONN="-h$IPADDR -u$USER -p$PASSWORD -P$PORT" stdopt=`mysql $CONN -e"select 1" 2> /dev/null|awk 'NR>1'` if [ "$stdopt" = "1" ];then TIME2=`date +"%Y%m%d%H%M%S"` BKPFILE=$BACKUPDIR/dbs_mysql_${TIME2}_bkp.sql mysqldump $CONN --databases ceshi --routines --events --single-transaction --master-data=2 --flush-logs > $BKPFILE 2> /dev/null sleep 2 stdopt2=`tail -1 $BKPFILE|awk '{print $2,$3}'` if [ "$stdopt2" = "Dump completed" ];then echo "$TIME1 [INFO] Logical backup is successed, Take it easy." >> $LOGFILE else echo "$TIME1 [ERROR] Logical backup is failed, Please check." >> $LOGFILE fi else echo "$TIME1 [ERROR] Connect is failed, Please check." >> $LOGFILE fi find $BACKUPDIR -name "full_mysql_*_bkp.sql" -ctime +7 -exec rm {} \;
正常輸出:
2022-11-23 07:04:22 [INFO] Logical backup is successed, Take it easy.
2022-11-24 02:45:01 [INFO] Logical backup is successed, Take it easy.
2022-11-25 02:45:01 [INFO] Logical backup is successed, Take it easy.
2022-11-26 02:45:01 [INFO] Logical backup is successed, Take it easy.
2022-11-27 02:45:01 [INFO] Logical backup is successed, Take it easy.
錯誤輸出:
2022-11-23 06:22:55 [ERROR] Connect is failed, Please check.
2022-11-23 06:23:16 [ERROR] Connect is failed, Please check.
2022-11-23 06:23:32 [ERROR] Connect is failed, Please check.
2022-11-23 06:23:45 [ERROR] Connect is failed, Please check.
2 、 MySQL 物理熱備指令碼( Linux 版)
指令碼程式碼:
#!/bin/bash . $HOME/.bash_profile BACKUPDIR=/backup LOGFILE=$BACKUPDIR/mysql_physicbak.log USER=root PASSWORD=Abcd321# SOCKET=/mysql/mysql57/product/data/mysql.sock CONN="-u$USER -p$PASSWORD -S$SOCKET" stdopt=`mysql $CONN -e"select 1" 2> /dev/null|awk 'NR>1'` if [ "$stdopt" = "1" ];then TIME2=`date +"%Y%m%d%H%M%S"` TMPFILE=$BACKUPDIR/xtrabackup_${TIME2}.LOG #test -d $BACKUPDIR/$TIME2 || mkdir -p $BACKUPDIR/$TIME2 #xtrabackup $CONN --backup --target-dir=$BACKUPDIR/$TIME2 2> $TMPFILE xtrabackup $CONN --backup --stream=tar 2> $TMPFILE | gzip > $BACKUPDIR/full_mysql_${TIME2}.tar.gz sleep 2 TIME1=`date +"%Y-%m-%d %H:%M:%S"` stdopt2=`tail -1 $TMPFILE|awk '{print $3,$4}'` if [ "$stdopt2" = "completed OK!" ];then echo "$TIME1 [INFO] physical backup is successed, Take it easy." >> $LOGFILE else echo "$TIME1 [ERROR] physical backup is failed, Please check." >> $LOGFILE fi else TIME1=`date +"%Y-%m-%d %H:%M:%S"` echo "$TIME1 [ERROR] Connect is failed, Please check." >> $LOGFILE fi find $BACKUPDIR -name "full_mysql_*.tar.gz" -ctime +7 -exec rm {} \;
正常輸出:
2022-11-23 06:57:31 [INFO] physical backup is successed, Take it easy.
2022-11-23 06:58:08 [INFO] physical backup is successed, Take it easy.
2022-11-23 06:58:49 [INFO] physical backup is successed, Take it easy.
錯誤輸出:
2022-11-23 07:00:13 [ERROR] Connect is failed, Please check.
2022-11-23 07:00:19 [ERROR] Connect is failed, Please check.
2022-11-23 07:00:21 [ERROR] Connect is failed, Please check.
2022-11-23 07:00:36 [ERROR] Connect is failed, Please check.
2022-11-23 07:00:37 [ERROR] Connect is failed, Please check.
3 、 MySQL 邏輯熱備指令碼( W indows 版)
指令碼程式碼:
@echo off SETLOCAL ENABLEDELAYEDEXPANSION set DISK=C: set MySQLPATH=C:\Program Files\MySQL\MySQL Server 8.0\bin set BACKUPDIR=C:\backup set LOGFILE=%BACKUPDIR%\mysql_logicbak.log set IPADDR=127.0.0.1 set USER=root set PASSWORD=Abcd321# set PORT=3308 set CONN=-h%IPADDR% -u%USER% -p%PASSWORD% -P%PORT% %DISK% cd %MySQLPATH% for /f %%i in ('mysql %CONN% --silent --skip-column-names -e"select 1" 2^>nul') do @set stdopt1=%%i timeout 1 >nul :: echo %stdopt1% set TIME1=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2% if "%stdopt1%"=="1" ( echo "%TIME1% [INFO] Connect is successed, Take it easy." >> %LOGFILE% ) else ( echo "%TIME1% [ERROR] Connect is failed, Please check." >> %LOGFILE% exit 1 ) set BKPFILE=%BACKUPDIR%\dbs_mysql_%TIME1%_bkp.sql mysqldump %CONN% --databases hzmc --routines --events --single-transaction --master-data=2 --flush-logs > %BKPFILE% 2>nul timeout 2 >nul :: for /f "delims=" %%i in (%BKPFILE%) do (set lastLine=%%~i) :: for /f "tokens=3" %%i in ('echo %lastLine%') do @set stdopt2=%%i find "Dump completed" %BKPFILE% && set stdopt2=%errorlevel% timeout 1 >nul :: echo %stdopt2% if "%stdopt2%"=="0" ( echo "%TIME1% [INFO] Logical backup is successed, Take it easy." >> %LOGFILE% ) else ( echo "%TIME1% [ERROR] Logical backup is failed, Please check." >> %LOGFILE% exit 1 ) forfiles /P %BACKUPDIR%\ /M dbs_mysql_*_bkp.sql /D -2 /C "cmd /c del @file" ENDLOCAL pause
正常輸出:
"20221130192026 [INFO] Connect is successed, Take it easy."
"20221130192026 [INFO] Logical backup is successed, Take it easy."
"20221130192055 [INFO] Connect is successed, Take it easy."
"20221130192055 [INFO] Logical backup is successed, Take it easy."
"20221130192112 [INFO] Connect is successed, Take it easy."
"20221130192112 [INFO] Logical backup is successed, Take it easy."
錯誤輸出:
"20221208111538 [ERROR] Connect is failed, Please check."
"20221208111545 [ERROR] Connect is failed, Please check."
"20221208111548 [ERROR] Connect is failed, Please check."
"20221208111550 [ERROR] Connect is failed, Please check."
"20221208111552 [ERROR] Connect is failed, Please check."
以上
3
種熱備場景,指令碼中均內建的備份成功關鍵字的判斷,只有真正成功完成備份,才會在日誌中顯示備份成功,因此在備份可靠性方面是有保障的。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30310891/viewspace-2927288/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PG最典型和實用的熱備指令碼實戰指令碼
- oracle的熱備指令碼Oracle指令碼
- oracle 熱備指令碼 .Oracle指令碼
- 生成熱備份指令碼指令碼
- 10個典型實用的PHP程式碼片段PHP
- 能用js實現的最終用js實現,Shell指令碼也不例外JS指令碼
- mysql備份指令碼MySql指令碼
- Oracle物理熱備份指令碼(ZT)Oracle指令碼
- MySQL 之XtraBackup全量增量熱備實踐MySql
- Oracle自動備份指令碼的實現Oracle指令碼
- 基於資料庫的熱備指令碼資料庫指令碼
- mysql innobackupex備份指令碼MySql指令碼
- 日常實用指令碼整理指令碼
- rman實用指令碼集指令碼
- 實用小指令碼——mysql/mongo 匯出到本地檔案指令碼MySqlGo
- MySQL增量備份的指令碼(innobackupex)MySql指令碼
- mysql的一個備份指令碼MySql指令碼
- 幾例實用的Shell指令碼指令碼
- 常用的幾個典型指令碼指令碼
- MySQL的一些功能實用的Linux shell指令碼分享MySqlLinux指令碼
- 實戰-MySQL定時增量備份(2)MySql
- mysql 一個比較好用的備份指令碼 可以進行熱備 本人測試好用MySql指令碼
- Steam 推出全新實時和周度榜單,展示最熱銷和最熱玩遊戲遊戲
- 案例:通過shell指令碼實現mysql資料備份與清理指令碼MySql
- linux下oracle熱備份指令碼LinuxOracle指令碼
- 基於表空間的熱備份指令碼指令碼
- 一站看完全網熱文,目前最實用的熱點工具!
- MySQL運維實戰之備份和恢復(8.1)xtrabackup全量備份MySql運維
- 實戰模擬監控MySQL服務shell指令碼小結薦MySql指令碼
- EJB實用原理和實戰示例
- 兩套mysql備份指令碼MySql指令碼
- mysql按表備份指令碼MySql指令碼
- Mysql異地備份指令碼MySql指令碼
- MySQL自動備份指令碼MySql指令碼
- 基於Groovy的規則指令碼引擎實戰指令碼
- 9個實用shell指令碼指令碼
- 分享兩個實用的shell指令碼指令碼
- 實用的linux下指令碼!(轉)Linux指令碼