您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“mysql如何實(shí)現(xiàn)按表備份腳本”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“mysql如何實(shí)現(xiàn)按表備份腳本”這篇文章吧。
有兩個(gè)版本,分別是linux和windows環(huán)境下的。我不會(huì)python,其實(shí)用python會(huì)簡(jiǎn)單得多,python可以跨平臺(tái)的。
linux平臺(tái)的版本是來自于君三思的<涂抹mysql>:
==========================================================LINUX Shell腳本===============================================================
#more /data/mysqldata/scripts/mysql_full_backup_by_table.sh
#!/bin/sh
#created by junsansi 20130505
source /data/mysqldata/scripts/mysql_env_ini
DATA_PATH=/data/mysqldata/backup/mysql_full_bytables
DATA_FILE=${DATA_PATH}/dbfullbak_by_tables_`date +%F`.sql.gz
LOG_FILE=${DATA_PATH}/dbfullbak_`date +%F`.log
MYSQL_PATH=/usr/local/mysql/bin
MYSQL_CMD="${MYSQL_PATH}/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock"
MYSQL_DUMP="${MYSQL_PATH}/mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_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 'ld'|egrep -v "information_schema|mysql|performance_schema"`
do
mkdir -p ${DATA_PATH}/${dbs}
echo -e "**** Database:${dbs} Backup Start_Time:`date +%F' '%T' '%w` ****\n" >> ${LOG_FILE}
for tbls in `${MYSQL_CMD} -D ${dbs} -e "show tables"|sed 'ld'`
do
echo -e" ####Begin${dbs}.${tbls}Dump!Start_Time:`date +%F' '%T`" >> ${LOG_FILE}
echo -e" Execute Command:${MYSQL_DUMP} --tables ${dbs} ${tbls} |gzip > ${DATA_PATH}/${dbs}/${dbs}_${tbls}.sql.gz" >> ${LOG_FILE}
${MYSQL_DUMP} --tables ${dbs} ${tbls} |gzip > ${DATA_PATH}/${dbs}/${dbs}_${tbls}.sql.gz
echo -e" ####End${dbs}.${tbls} Dump!Stop_Time:`date +%F' '%T`\n" >> ${LOG_FILE}
echo >> ${LOG_FILE}
done
echo -e "**** Database:${dbs} Backup Stop_Time:`date +%F' '%T' '%w` ****\n" >> ${LOG_FILE}
echo -e "**** Backup file size:`du -sh ${DATA_PATH}/${dbs}` ****\n" >> ${LOG_FILE}
done
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
============================================================Windows Bat腳本=======================================================================
prompt $P$G$T$G
if %time:~0,2% lss 10 (
set ymd=%date:~0,4%-%date:~5,2%-%date:~8,2%_0%time:~1,1%
)else (
set ymd=%date:~0,4%-%date:~5,2%-%date:~8,2%_%time:~0,2%
)
set date=%date:~0,4%-%date:~5,2%-%date:~8,2%
set v_backup_file_name=xedk_%ymd%_bak.sql
set v_backup_error_name=xedk_%ymd%_error.txt
cd D:\DBbak\INPUT
D:
md %date%
rem pause
cd D:\mysql-5.6.19-winx64\bin
mysql -uxedk -P 3308 -p"admin" -N <D:\backupSc\backTabSc.sql> mysqldumpsc_%date%.bat
rem pause
cd D:\DBbak\INPUT
move D:\Quarkp2p\mysql-5.6.19-winx64\bin\mysqldumpsc_%date%.bat D:\DBbak\INPUT\%date%
rem pause
cd D:\mysql-5.6.19-winx64\bin
call \DBbak\INPUT\%date%\mysqldumpsc_%date%.bat
rem pause
cd D:\INPUT
move D:\DBbak\INPUT\*.sql D:\DBbak\INPUT\%date%\
rem pause
-----------------------------------------------------------
D:\backupSc\backTabSc.sql中的內(nèi)容是:
SELECT CONCAT("mysqldump -uroot -h292.168.30.4 -p\"P\#92\^z\!\" -P 3308 --tables ", "xedk ",table_name, " > D:","\\","DBbak","\\","INPUT","\\","Table_",table_name,".sql") FROM information_schema.tables where table_schema='xedk';
=============================================================================================================================================
總結(jié):其實(shí)兩個(gè)版本的思路都是一致的,就是讀出所有的tables的名字,然后用mysqldump按每一個(gè)table的名字進(jìn)行導(dǎo)出。
其他:
1、windows下面,mysql在執(zhí)行sql語句時(shí)加上into outfile 'mysqldumpsc.txt';可以將sql語句的執(zhí)行結(jié)果輸出到指定的文件“mysqldumpsc.txt”中,但是這個(gè)文件的位置默認(rèn)是在mysql的datadir下面。如果指定文件的保留位置,要注意mysql對(duì)其目錄是否有讀寫權(quán)限
2、windows下面,如果用以下語句:mysql -uxedk -P 3308 -p"admin" -N mysqldumpsc2.txt 將執(zhí)行結(jié)果輸出到mysqldumpsc2.txt中,這個(gè)文件的默認(rèn)位置是在mysql的執(zhí)行路徑下。
3、用mysql -uxedk -P 3308 -p"admin" -e "show databases;" 這種方式直接在命令界面執(zhí)行sql語句時(shí),sql語句里如果有函數(shù),是無法被識(shí)別的。
以上是“mysql如何實(shí)現(xiàn)按表備份腳本”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。