Linux Shell脚本之利用mysqldump备份MySQL数据库(详细注解)
利用mysqldump命令备份MySQL数据库的脚本(不带注释版,适合生产环境使用)
设计该脚本的一些设计、编写考虑:
利用mysqldump命令备份MySQL数据库的脚本(不带注释版,适合生产环境使用)
#!/bin/bash
MYSQLDBUSERNAME=root
MYSQLDBPASSWORD=password
MYSQBASEDIR=/usr/local/mysql
MYSQL=$MYSQBASEDIR/bin/mysql
MYSQLDUMP=$MYSQBASEDIR/bin/mysqldump
BACKDIR=/var/backup/db
DATEFORMATTYPE1=$(date +%Y-%m-%d)
DATEFORMATTYPE2=$(date +%Y%m%d%H%M%S)
[ -d $MYSQBASEDIR ] && MYSQDATADIR=$MYSQBASEDIR/data || MYSQDATADIR=/var/lib/mysql
[ -x $MYSQL ] || MYSQL=mysql
[ -x $MYSQLDUMP ] || MYSQLDUMP=mysqldump
[ -d ${BACKDIR} ] || mkdir -p ${BACKDIR}
[ -d ${BACKDIR}/${DATEFORMATTYPE1} ] || mkdir ${BACKDIR}/${DATEFORMATTYPE1}
DBLIST=`ls -p $MYSQDATADIR | grep / |tr -d /`
for DBNAME in $DBLIST
do ${MYSQLDUMP} --user=${MYSQLDBUSERNAME} --password=${MYSQLDBPASSWORD} --routines --events --triggers --single-transaction --flush-logs --databases ${DBNAME} | gzip > ${BACKDIR}/${DATEFORMATTYPE1}/${DBNAME}-backup-${DATEFORMATTYPE2}.sql.gz
[ $? -eq 0 ] && echo "${DBNAME} has been backuped successful" || echo "${DBNAME} has been backuped failed"
/bin/sleep 5
done
利用mysqldump命令备份MySQL数据库的脚本(带注释版,适合学习和测试使用)
#!/bin/bash
# MYSQLDBUSERNAME是MySQL数据库的用户名,可自定义
MYSQLDBUSERNAME=root
# MYSQLDBPASSWORD是MySQL数据库的密码,可自定义
MYSQLDBPASSWORD=password
# MYSQBASEDIR是MySQL数据库的安装目录,--prefix=$MYSQBASEDIR,可自定义
MYSQBASEDIR=/usr/local/mysql
# MYSQL是mysql命令的绝对路径,可自定义
MYSQL=$MYSQBASEDIR/bin/mysql
# MYSQLDUMP是mysqldump命令的绝对路径,可自定义
MYSQLDUMP=$MYSQBASEDIR/bin/mysqldump
# BACKDIR是数据库备份的存放地址,可以自定义修改成远程地址
BACKDIR=/var/backup/db
# 获取当前时间,格式为:年-月-日,用于生成以这种时间格式的目录名称
DATEFORMATTYPE1=$(date +%Y-%m-%d)
# 获取当前时间,格式为:年月日时分秒,用于生成以这种时间格式的文件名称
DATEFORMATTYPE2=$(date +%Y%m%d%H%M%S)
# 如果存在MYSQBASEDIR目录,则将MYSQDATADIR设置为$MYSQBASEDIR/data,具体是什么路径,就把data改成什么路径,否则将MYSQBASEDIR设定为/var/lib/mysql,可自定义
[ -d $MYSQBASEDIR ] && MYSQDATADIR=$MYSQBASEDIR/data || MYSQDATADIR=/var/lib/mysql
# 如果mysql命令存在并可执行,则继续,否则将MYSQL设定为mysql,默认路径下的mysql
[ -x $MYSQL ] || MYSQL=mysql
# 如果mysqldump命令存在并可执行,则继续,否则将MYSQLDUMP设定为mysqldump,默认路径下的mysqldump
[ -x $MYSQLDUMP ] || MYSQLDUMP=mysqldump
# 如果不存在备份目录则创建这个目录
[ -d ${BACKDIR} ] || mkdir -p ${BACKDIR}
[ -d ${BACKDIR}/${DATEFORMATTYPE1} ] || mkdir ${BACKDIR}/${DATEFORMATTYPE1}
# 获取MySQL中有哪些数据库,,根据mysqldatadir下的目录名字来确认,此处可以自定义,TODO
DBLIST=`ls -p $MYSQDATADIR | grep / |tr -d /`
# 从数据库列表中循环取出数据库名称,执行备份操作
for DBNAME in $DBLIST
# mysqldump skip one table
# -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
# mysqldump --ignore-table=mysql.event
#
# --routines,备份存储过程和函数
# --events,跳过mysql.event表
# --triggers,备份触发器
# --single-transaction,针对InnoDB,在单次事务中通过转储所有数据库表创建一个一致性的快照,此选项会导致自动锁表,因此不需要--lock-all-tables
# --flush-logs,在dump转储前刷新日志
# --ignore-table,忽略某个表,--ignore-table=database.table
# --master-data=2 ,如果启用MySQL复制功能,则可以添加这个选项
# 将dump出的sql语句用gzip压缩到一个以时间命名的文件
do ${MYSQLDUMP} --user=${MYSQLDBUSERNAME} --password=${MYSQLDBPASSWORD} --routines --events --triggers --single-transaction --flush-logs --ignore-table=mysql.event --databases ${DBNAME} | gzip > ${BACKDIR}/${DATEFORMATTYPE1}/${DBNAME}-backup-${DATEFORMATTYPE2}.sql.gz
# 检查执行结果,如果错误代码为0则输出成功,否则输出失败
[ $? -eq 0 ] && echo "${DBNAME} has been backuped successful" || echo "${DBNAME} has been backuped failed"
# 等待5s,可自定义
/bin/sleep 5
done
执行效果:
[root@htvm ~]# ./backupmysqlbydate.sh
mysql has been backuped successful
test has been backuped successful
[root@htvm ~]# ls /var/backup/db/2015-07-27/
mysql-backup-20150727195515.sql.gz test-backup-20150727195515.sql.gz
[root@htvm ~]#
标签:mysqldump,备份MySQL数据库,MySQL数据库备份,mysql备份,mysql备份脚本
--end--
使用mysqldump进行MariaDB 的备份
使用mysqldump导出数据库
基于mysqldump快速搭建从库
恢复mysqldump创建的备份集
使用mysqldump命令行工具创建逻辑备份
本文永久更新链接地址:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.
