Home Database Mysql Tutorial MYSQL自动备份策略的选择与优劣点分析

MYSQL自动备份策略的选择与优劣点分析

Jun 07, 2016 pm 04:17 PM
mysql analyze backup Strategy automatic choose

目前流行几种备份方式: 1、逻辑备份: 使用mysql自带的mysqldump工具进行备份。备份成sql文件形式。 优点:最大好处是能够与正在运行的mysql自动协同工作, 在运行期间可以确保备份是当时的点,它会自动将对应操作的表锁定,不允许其他用户修改(只能访问)。

目前流行几种备份方式:
1、逻辑备份:
使用mysql自带的mysqldump工具进行备份。备份成sql文件形式。
优点:最大好处是能够与正在运行的mysql自动协同工作,
在运行期间可以确保备份是当时的点,它会自动将对应操作的表锁定,不允许其他用户修改(只能访问)。可能会阻止修改操作。sql文件通用方便移植。
缺点:备份的速度比较慢。如果是数据量很多的时候。就很耗时间。如果数据库服务器处在提供给用户服务状态,在这段长时间操作过程中,意味着要锁定表(一般是读锁定,只能读不能写入数据)。那么服务就会影响的。
备注:所谓的与mysql服务器能够自动协同工作,实际上是指加参数来控制mysql服务器,比如锁定所有表只能进行读,不能进行写操作。
--lock-all-tables
2、物理备份:
直接拷贝mysql的数据目录。缺点,你不能去操作正在运行的mysql服务器(在拷贝的过程中有用户通过应用程序访问更新数据,这样就无法备份当时的数据)
可能无法移植到其他机器上去。
直接拷贝只适用于myisam类型的表。这种类型的表是与机器独立的。但实际情况是,你设计数据库的时候不可能全部使用myisam类型表。你也不可能:因为myisam类型表与机器独立,方便移植,于是就选择这种表,这并不是选择它的理由。
更多的情况是,,你会根据业务特点(比如你需要支持事务机制就必须使用innodb),查询速度和服务性能来选择表类型的。
必须保证表不被使用中。
如果服务器在你则正在拷贝一个表时改变它,拷贝就失去意义。
如果数据库表在文件系统备份过程中被修改,进入备份的表文件主语不一致的状态,而对以后的恢复表将失去意义。
保证你的拷贝完整性的最好方法是:关闭服务器,拷贝文件,然后重启服务器。
或者是,要锁定对应的表(对前端用户造成访问问题)。
解释直接拷贝文件,为什么不具备可移植性?
mysqldump 产生可移植到其他机器、甚至具有不同硬件结构的机器上的文本文件。直接拷贝文件不能够移植到其他机器上,除非要拷贝的表使用MyISAM 存储格式。ISAM 表只能在具有相同硬件结构的机器之间进行拷贝。例如,将文件从S PARC 的Solaris 机器拷贝到Intel 的Solaris 机器(或者相反)是行不通的。由MySQL3.23 引进的MyISAM 表存储格式可以解决这个问题,因为该格式与机器独立。因此,如果以下两个条件都满足的话,直接拷贝文件可以移植到具有不同硬件结构的机器上:即另一台机器 上也必须运行MySQL3.23 以上的版本,并且文件必须表示成MyISAM 表,而不是ISAM 表。
3、双机热备份。
mysql数据库没有增量备份的机制。当数据量太大的时候备份是一个很大的问题。还好mysql数据库提供了一种主从备份的机制(也就是双机热备)
优点:适合数据量大的时候。现在明白了。大的互联网公司对于mysql数据备份,都是采用热机备份。搭建多台数据库服务器,进行主从复制。
主从复制经常遇到的问题就是,如何保证数据不堵塞,不延迟。这个问题还是可以容忍的,有一些方案可以改善。毕竟有得有失的。这已经是很省心省力的方式了。

我目前应该使用什么样的备份策略的权衡:
物理备份,恢复快,当然最好是存储在一个机器上。我现在是用物理备份还是逻辑备份为好呢?
考虑到以后会迁移平台。为了保证通用性。恢复速度1分钟左右的差距我是可以容忍的。所以我为了跨平台,我更加愿意使用逻辑备份。存储sql文件形式。
双热机备份方式,目前硬件没有多个。技术人员有限,需要人力去维护,比较麻烦。所以排除在外。
方案:
1、总体策略:写个定时执行任务。定时在晚上或凌晨自动备份(考虑数据库服务器在运行中不能停机)
代码中做成备份成功后,把以前的删掉。避免很多数据占据磁盘。
2、考虑到初期数据量这么小。使用mysqldump进行备份吧。设置在凌晨几点(4-6点这个时候基本上没什么人访问)的时候自动备份。
3、使用逻辑备份方式:恢复速度1分钟左右的差距我是可以容忍的。所以我为了跨平台,我更加愿意使用逻辑备份。存储sql文件形式。
4、每天都进行备份。
由于是在凌晨的时候mysqldump去锁定,访问数据库服务器。对服务器几乎没什么影响。所以每天都可以备份。每天都一个sql文件。那么将会很多文件。
所以,每次备份成功后。删除以前的文件。保留最近一个星期的备份sql文件。

备份工具的路径:/usr/bin/mysqldump
备份数据保存路径:/data/backdata/
5、备份脚本的编写
思路:
5.1 在shell脚本中调用mysqldump生成备份文件(这个工具可以生成sql文件到磁盘上去)
5.2 为了方便以后查找。每次备份的记录记录成日志形式。几点进行了备份操作,生成了什么文件名称。这样可以方便以后查阅哪天是否没有成功备份
删除的文件作为日志信息也记录下来。
5.3  让linux下的crontab进程调用脚本执行。
命令:crontab -e
打开的文件中加入代码:0 05 * * * 脚本的路径/mysqlback.sh
mysqlback.sh的内容:
# /bin/bash
DB_NAME="****"
DB_USER="****"
DB_PASS="****"
BIN_DIR="/usr/bin"
BACK_DIR="/data/backdata"
DATE="mysql-`date +'%Y%m%d-%H:%M:%S'`"
LogFile="$BACK_DIR"/dbbakup.log #日志记录保存的目录
BackNewFile=$DATE.sql
$BIN_DIR/mysqldump --opt --force -u$DB_USER  -p$DB_PASS $DB_NAME > $BACK_DIR/$DATE.sql

echo -----------------------"$(date +"%y-%m-%d %H:%M:%S")"----------------------- >> $LogFile

echo  createFile:"$BackNewFile" >> $LogFile

#find "/data/backdata/" -cmin +1 -type f -name "*.sql" -print > deleted.txt
find "/data/backdata/" -ctime +7 -type f -name "*.sql" -print > deleted.txt
echo -e "delete files:n" >> $LogFile
#循环删除匹配到的文件
cat deleted.txt | while read LINE
do
    rm -rf $LINE
    echo $LINE>> $LogFile
done

echo "---------------------------------------------------------------" >> $LogFile

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

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.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

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.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles