MySQL備份恢復基礎mysql
一,mysqldump命令sql
mysqldump這個命令通常能夠知足備份的大部分須要,由於這個工具支持全庫備份,單庫備份,單表備份,甚至邏輯備份,只備份表結構等等功能。
shell
主要命令格式:數據庫
shell> mysqldump [options] > dump.sql
主要的一些參數:
bash
-u ,--user 用戶名服務器
-p , --password 用戶密碼socket
-S ,--socket 套接字ide
-h , --host 指定鏈接的服務器函數
-P,--port 指定服務器的對應端口工具
--default-character-set 設置備份集字符集
-A ,--all-databases 備份全部數據庫
-B ,--databases 導出某個或者某些數據庫,數據庫名以空格分隔
--tables 導出指定的表,格式爲「數據庫名 表名」,這個參數會覆蓋-B參數
-l ,--lock-tables 鎖定表,默認開啓
--single-transaction ,對於支持事物的數據庫引擎來講,保證了一致性,一般和-l一塊兒工做,寫做 --single-transaction -l, 其做用是取代其餘引擎的 -x 參數。
-d, --nodata, 只導出對象的結構,不導出數據。
-t , --no-create-info , 只導出對象數據而不包含對象的結構
-F,--flush-logs ,通常在全庫備份的時候須要先刷新日誌,以方便恢復。
--flush-privileges , 備份前刷新一下權限。
-R , --routines , 導出存儲過程,函數等MYSQL程序。
-E, --events ,輸出event
--master-data=1/2 --1,記錄備份後將繼續回覆的binlog 編號和位置。 2,編號和位置前有註釋
通常以上參數適應了大多數的應用場景
對於myisam,比較經常使用的全庫備份語句:
mysqldump -uroot -p -A -B -F -R --flush-privileges --master-data=2 -x --events | gzip > /data/backup/all.sql.gz
對於innodb,比較經常使用的全庫備份語句:
mysqldump -uroot -p -A -B -F -R --flush-privileges --master-data=2 --single-transaction --events | gzip > /data/backup/all.sql.gz
二,備份腳本:
1.全庫備份:
vi /data/mysqldata/scripts/mysql_full_backup.sh #!/bin/sh source /data/mysqldata/scripts/mysql_env.ini DATA_PATH=/data/mysqldata/backup/mysql_full DATA_FILE=${DATA_PATH}/dbfullbak_`date +%F`.sql.gz LOG_FILE=${DATA_PATH}/dbfullbak_`date +%F`.log MYSQL_PATH=/opt/mysql/bin MYSQL_DUMP="${MYSQL_PATH}/mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${MYSQL_PORT}/mysql.sock -A -R -x --default-character-set=utf8" echo > ${LOG_FILE} echo -e "=== Jobs started at `date +%F''%T''%w` ===\n" >> ${LOG_FILE} echo -e "*** Executed command:${MYSQL_DUMP} | gzip > ${DATA_FILE}" >> ${LOG_FILE} ${MYSQL_DUMP} | gzip > $DATA_FILE echo -e "*** Executed finished at at `date +%F''%T''%w` ===" >> ${LOG_FILE} echo -e "*** Backup file size: `du -sh ${DATA_FILE}` ===\n" >> ${LOG_FILE} echo -e "--- Find expired backup and delete those files ---" >> ${LOG_FILE} for tfile in $(/usr/bin/find ${DATA_PATH}/ -mtime +6) do if [ -d $tfile ] ; then rmdir $tfile elif [ -f $tfile ] ; then rm -f $tfile fi echo -e "--- Delete file: $tfile ---" >> ${LOG_FILE} done echo -e "\n=== Jobs ended at `date +%F''%T''%w` ===\n" >> ${LOG_FILE}
2. 分庫備份:
vi mysql_full_backup_by_dbs.sh #!/bin/sh source /data/mysqldata/scripts/mysql_env.ini DATA_PATH=/data/mysqldata/backup/mysql_full_by_dbs if [ ! -d ${DATA_PATH}/${MYSQL_PORT} ] ; then mkdir -p ${DATA_PATH}/${MYSQL_PORT} fi LOG_FILE=${DATA_PATH}/${MYSQL_PORT}/dbfullbak_`date +%F`.log MYSQL_PATH=/opt/mysql/bin MYSQL_CMD="${MYSQL_PATH}/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${MYSQL_PORT}/mysql.sock" MYSQL_DUMP="${MYSQL_PATH}/mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${MYSQL_PORT}/mysql.sock --single-transaction -l " echo > ${LOG_FILE} echo -e "=== Jobs started at `date +%F''%T''%w` ===\n" >> ${LOG_FILE} for dbs in `${MYSQL_CMD} -e "show databases" | sed '1d' | egrep -v "information_schema|mysql|performance_schema"` do echo -e "*** Database: ${dbs} Backup start at `date +%F''%T''%w` ***" >> ${LOG_FILE} ${MYSQL_DUMP} -B ${dbs} | gzip > ${DATA_PATH}/${MYSQL_PORT}/${dbs}_`date +%F''%T''%w`.sql.gz echo -e "*** Executed finished at at `date +%F''%T''%w` ===" >> ${LOG_FILE} echo -e "*** Backup file size: `du -sh ${DATA_FILE}` ===\n" >> ${LOG_FILE} done echo -e "--- Find expired backup and delete those files ---" >> ${LOG_FILE} for tfile in $(/usr/bin/find ${DATA_PATH}/${MYSQL_PORT} -mtime +6) do if [ -d $tfile ] ; then rmdir $tfile elif [ -f $tfile ] ; then rm -f $tfile fi echo -e "--- Delete file: $tfile ---" >> ${LOG_FILE} done echo -e "\n=== Jobs ended at `date +%F''%T''%w` ===\n" >> ${LOG_FILE}
3. 分表備份
vi mysql_full_backup_by_tbs.sh #!/bin/sh source /data/mysqldata/scripts/mysql_env.ini DATA_PATH=/data/mysqldata/backup/mysql_full_by_dbs if [ ! -d ${DATA_PATH}/${MYSQL_PORT} ] ; then mkdir -p ${DATA_PATH}/${MYSQL_PORT} fi LOG_FILE=${DATA_PATH}/${MYSQL_PORT}/dbfullbak_`date +%F`.log MYSQL_PATH=/opt/mysql/bin MYSQL_CMD="${MYSQL_PATH}/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${MYSQL_PORT}/mysql.sock" MYSQL_DUMP="${MYSQL_PATH}/mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${MYSQL_PORT}/mysql.sock --single-transaction -l " echo > ${LOG_FILE} echo -e "=== Jobs started at `date +%F''%T''%w` ===\n" >> ${LOG_FILE} for dbs in `${MYSQL_CMD} -e "show databases" | sed '1d' | egrep -v "information_schema|mysql|performance_schema"` do echo -e "*** Database: ${dbs} Backup start at `date +%F''%T''%w` ***" >> ${LOG_FILE} ##${MYSQL_DUMP} -B ${dbs} | gzip > ${DATA_PATH}/${MYSQL_PORT}/${dbs}_`date +%F''%T''%w`.sql.gz if [ ! -d ${DATA_PATH}/${MYSQL_PORT}/${dbs} ] ; then mkdir -p ${DATA_PATH}/${MYSQL_PORT}/${dbs} fi for tbls in `${MYSQL_CMD} -D ${dbs} -e "show tables" | sed '1d'` do echo -e "*** Tables: ${dbs}.${tbls} start dump at `date +%F''%T''%w` ***" >> ${LOG_FILE} echo -e " Execute Command: ${MYSQL_DUMP} --tables ${dbs} ${tbls} | gzip > ${DATA_PATH}/${MYSQL_PORT}/${dbs}/${dbs}.${tbls}.sql.gz" >> ${LOG_FILE} ${MYSQL_DUMP} --tables ${dbs} ${tbls} | gzip > ${DATA_PATH}/${MYSQL_PORT}/${dbs}/${dbs}.${tbls}.sql.gz echo -e " ### End ${dbs}.${tbls} Dump! Stop_time:`date +%F''%T''%w` ### " >> ${LOG_FILE} done echo -e "*** Executed finished at `date +%F''%T''%w` ===" >> ${LOG_FILE} echo -e "*** Backup file size: `du -sh ${DATA_FILE}` ===\n" >> ${LOG_FILE} done echo -e "--- Find expired backup and delete those files ---" >> ${LOG_FILE} for tfile in $(/usr/bin/find ${DATA_PATH}/${MYSQL_PORT}/${dbs} -mtime +6) do if [ -d $tfile ] ; then rmdir $tfile elif [ -f $tfile ] ; then rm -f $tfile fi echo -e "--- Delete file: $tfile ---" >> ${LOG_FILE} done echo -e "\n=== Jobs ended at `date +%F''%T''%w` ===\n" >> ${LOG_FILE} eco -e "--- Find expired logs and delete those files ---" >> ${LOG_FILE} for tfile in $(/usr/bin/find ${DATA_PATH}/${MYSQL_PORT}/${dbs} -mtime +6) do if [ -f $tfile ] ; then rm -f $tfile fi echo -e "--- Delete file: $tfile ---" >> ${LOG_FILE} done echo -e "\n=== Jobs ended at `date +%F''%T''%w` ===\n" >> ${LOG_FILE}