MySQL备份恢复基础-创新互联
MySQL备份恢复基础
彭州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!一,mysqldump命令
mysqldump这个命令一般可以满足备份的大部分需要,因为这个工具支持全库备份,单库备份,单表备份,甚至逻辑备份,只备份表结构等等功能。
主要命令格式:
shell> mysqldump [options] > dump.sql主要的一些参数:
-u ,--user 用户名
-p , --password 用户密码
-S ,--socket 套接字
-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}另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享名称:MySQL备份恢复基础-创新互联
分享链接:http://hbruida.cn/article/ccpohh.html