mysql如何实现按表备份脚本
这篇文章主要为大家展示了"mysql如何实现按表备份脚本",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"mysql如何实现按表备份脚本"这篇文章吧。
有两个版本,分别是linux和windows环境下的。我不会python,其实用python会简单得多,python可以跨平台的。
linux平台的版本是来自于君三思的<涂抹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
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中的内容是:
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';
=============================================================================================================================================
总结:其实两个版本的思路都是一致的,就是读出所有的tables的名字,然后用mysqldump按每一个table的名字进行导出。
其他:
1、windows下面,mysql在执行sql语句时加上into outfile 'mysqldumpsc.txt';可以将sql语句的执行结果输出到指定的文件"mysqldumpsc.txt"中,但是这个文件的位置默认是在mysql的datadir下面。如果指定文件的保留位置,要注意mysql对其目录是否有读写权限
2、windows下面,如果用以下语句:mysql -uxedk -P 3308 -p"admin" -N mysqldumpsc2.txt 将执行结果输出到mysqldumpsc2.txt中,这个文件的默认位置是在mysql的执行路径下。
3、用mysql -uxedk -P 3308 -p"admin" -e "show databases;" 这种方式直接在命令界面执行sql语句时,sql语句里如果有函数,是无法被识别的。
以上是"mysql如何实现按表备份脚本"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!