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最典型和實用的熱備指令碼實戰指令碼
- mysql備份指令碼MySql指令碼
- 能用js實現的最終用js實現,Shell指令碼也不例外JS指令碼
- MySQL的一些功能實用的Linux shell指令碼分享MySqlLinux指令碼
- 幾例實用的Shell指令碼指令碼
- 案例:通過shell指令碼實現mysql資料備份與清理指令碼MySql
- 兩套mysql備份指令碼MySql指令碼
- SpringBoot整合RabbitMQ之典型應用場景實戰二Spring BootMQ
- SpringBoot整合RabbitMQ之典型應用場景實戰一Spring BootMQ
- 實戰-MySQL定時增量備份(2)MySql
- MySQL運維實戰之備份和恢復(8.1)xtrabackup全量備份MySql運維
- 分享兩個實用的shell指令碼指令碼
- 9個實用shell指令碼指令碼
- AE實用指令碼之——typemonkey指令碼
- MySQL資料庫備份的shell指令碼MySql資料庫指令碼
- MySQL中binlog備份指令碼分享MySql指令碼
- MySQL的冷備份和熱備份概念理解(轉)MySql
- 基於Groovy的規則指令碼引擎實戰指令碼
- 實戰-MySQL定時全量備份(1)MySql
- 超級實用的 iptables 防火牆指令碼防火牆指令碼
- 一站看完全網熱文,目前最實用的熱點工具!
- Liunx備份mysql資料庫的shell指令碼MySql資料庫指令碼
- Steam 推出全新實時和周度榜單,展示最熱銷和最熱玩遊戲遊戲
- MySQL:Windows下分庫備份指令碼MySqlWindows指令碼
- Mysql定時備份資料指令碼MySql指令碼
- 【MySQL】Xtrabackup備份及恢復指令碼MySql指令碼
- Appium iOS 測試指令碼開發實戰APPiOS指令碼
- 實戰-將MySQL備份上傳到私有云(3)MySql
- linuxshell程式設計實戰-編寫簡單的指令碼實用工具Linux程式設計指令碼
- 行業實踐:RocketMQ 業務整合典型行業應用和實踐行業MQ
- 五個實用的SQL Server PowerShell指令碼OMSQLServer指令碼
- MySQL實時計算QPS,TPS的SHELL指令碼兩例MySql指令碼
- ETL指令碼的實現指令碼
- Redis 實戰 —— 14. Redis 的 Lua 指令碼程式設計Redis指令碼程式設計
- MySQL備份指令碼,應該這麼寫MySql指令碼
- linux mysql定時備份指令碼記錄LinuxMySql指令碼
- mysql的冷備份與熱備份MySql
- 實用git指令Git