The Road to Linux Learning (Basics) (Twenty-three) MySQL Service (Database Backup - Supplement)

Table of contents

1. MySQL database backup

Overview

importance

Causes of data loss

2. Backup type

1. Physical and logical perspectives

1. Physical backup

2. Logical backup

2. Database backup strategy perspective

1. Complete backup

2. Incremental backup

3. Common backup methods

1. Physical backup

2. Use special backup tools

3. Enable binary log incremental backup

4. Third-party tool backup

Precautions

4. Backup

1. Complete backup

1. Pack database file backup

Location

backup

reduction

2. Backup tool backup

backup

reduction

2. Incremental backup

1. Characteristics

2. The significance of MySQL binary logs for backup

3. Enable binary log backup function

4. Incremental recovery

Application scenarios

method

general recovery

Point-in-time recovery

Recovery from a certain point in time to the end of the log

Recovery from a certain point in time to a certain point in time

location based recovery


1. MySQL database backup

Overview

        Database backup refers to backing up data, tables, views, stored procedures, triggers and other information in the database to another place so that it can be restored if the database is lost or damaged. Database backup is an essential task in database management. Through backup, the data and business in the database can be protected.

importance

        The main purpose of backup is disaster recovery. Backup can also be used to test applications, roll back data modifications, query historical data, audit, etc.

Causes of data loss

        Human error: Human operating errors, accidental deletion, hard drive formatting, incorrect storage operations, etc. may lead to data loss.       

        Hardware failure: Hard drive failure, power failure, motherboard failure, tape failure and other hardware problems may lead to data loss.

        Software issues: Software issues such as operating system malfunction, application malfunction, virus attack, system crash, etc. may also lead to data loss.

        Natural disasters: Natural disasters such as fires, floods, and earthquakes can damage equipment and cause data loss.

        Clear backup: Clearing backup data by mistake, or errors during the backup process may also lead to data loss.

2. Backup type

1. Physical and logical perspectives

1. Physical backup

        Backup of physical files of the database operating system (such as data files, log files, etc.).

        Cold backup is a hot backup performed when the database is shut down.
        The database is running. This backup method relies on the log file of the database.

2. Logical backup

        Backup of logical database components (such as tables and other database objects), which are information about the logical database structure (create database, create table, etc. statements) and content (insert statements or split text files).

2. Database backup strategy perspective

1. Complete backup

        A complete backup refers to backing up all information in the entire database, including all data, tables, views, stored procedures, triggers, etc. in the database files. Generally speaking, full backup is the most comprehensive and reliable way to backup, but the backup file size is larger and the backup time is relatively long.

2. Incremental backup

        Incremental backup refers to backing up new data or modified data at certain intervals on the basis of a complete backup. This backup method can save backup time and backup file space, but it requires specific software or commands.

3. Common backup methods

1. Physical backup

        Physical cold backup requires the database to be closed to better ensure the integrity of the database. Physical cold backup is used for non-core services. Such services are allowed to be interrupted. The characteristic of physical cold backup is that it is fast and the recovery is the simplest. By directly packaging the database folder (/usr/local/mysql/data) Implement backup.

2. Use special backup tools

        mysqldump
        mysqlhotcopy

3. Enable binary log incremental backup

        MySQL supports incremental backup, and binary logs must be enabled when performing incremental backup. Binary log files are provided for users to copy. Back up the information required for database changes made after the backup point. If you perform an incremental backup (containing data modifications that occurred since the last full or incremental backup), you need to refresh the binary log.

4. Third-party tool backup

        Percona XtraBackup is a free MySQL hot backup software that supports online backup of innodb and XtraDB, as well as MySQL table backup.

Precautions

        Back up regularly, specify a backup plan, and strictly abide by the naming
        rule of opening the binlog function in addition to full backup.
        Use a unified and easy-to-understand backup name. It is recommended to use the naming rule of library name or table name plus time.

4. Backup

1. Complete backup

1. Pack database file backup

Location

        The location of the source code package/usr/local/mysql/data/
        The location of the rpm package/var/lib/mysql

backup

        Create a data table in the database and write data.
        Stop the database service
        . Create a backup directory.
        Back up the database directory in a specific format. tar czf mysql_all-$(date +%F).tar.gz /var/lib/mysql/*

reduction

        Create a restore directory
        to simulate metadata loss
        and restore the decompressed data
        to the original directory to view.

2. Backup tool backup

backup

        Tool mysqldump
        performs a full backup of a single library mysqldump -u username -p [password] [options] --databases [database name] > /backup path/backup file name performs a
        full backup of multiple libraries mysqldump -u username -p [Password] [Options] --databases Library name 1 [Library name 2]...>/Backup path/Backup file name
        Perform a full backup of all libraries mysqldump -u username -p [Password] [Options] --opt - -all-databases > /backup path / backup file name
        Make a full backup of the table mysqldump -u username -p [password] [options] Database name table name > / backup path / backup file name Make a
        backup of the table structure mysqldump -u Username -p [password] -d database name table name >/backup path/backup file name

reduction

Tool source login mysql database execution source backup sql script path
            mysql mysql -u username-p[password] < path of library backup script
                             mysql -u username-p[password] library name < path of table backup script

2. Incremental backup

        Backup and restore take too long when using full backup

        Incremental backup is to back up the file content that has been added or changed since the last backup.

1. Characteristics

        There is no duplicate data, the backup volume is not large, and the
        recovery time is short and troublesome. It requires the last full backup and all the incremental backups after the full backup to restore, and all incremental backups need to be restored one by one.
        MySQL does not provide direct incremental backups. The backup method can indirectly implement incremental backup through the binary logs (binlog) provided by MySQL.

2. The significance of MySQL binary logs for backup

        The binary log stores all operations that update or may update the database.
        The binary log starts recording after starting the MySQL server, and re-creates a new log file after the file reaches the size set by max_binlog_size or the flush logs command is received.

3. Enable binary log backup function

        Add log-bin=file storage path/file prefix to the [mysqld] item of the MySQL configuration file, such as log-bin=mysql-bin, and then restart the mysqld service. This configuration exists by default.
        Use mysqld --log-bin=file storage path/file prefix to restart the mysqld service. Select a time period every week when the server load is light, or when users access less time for backup.

4. Incremental recovery

Application scenarios

        Artificial SQL statements destroyed the database.
        A system failure occurred before the next full backup, causing the database to be lost
        in the master-slave architecture. The master database data failed to ensure the consistency of the slave database data.

method
general recovery

    All backup binary log contents are restored
    in the format: mysqldbinlog [--no-defaults] incremental backup file | mysql -u username -p password

Point-in-time recovery

    It is convenient to skip a certain time point when an error occurs to implement data recovery
    format: recovery from the beginning of the log to a certain time point:
    mysqlbinlog [--no-defaults] --stop-datetime='year-month-day hour: minute :sec' binary log | mysql -u username -p password

Recovery from a certain point in time to the end of the log

    mysqlbinlog [--no-defaults] --start-datetime='Year-Month-Day Hour:Minute:Second' Binary log | mysql -u username -p password

Recovery from a certain point in time to a certain point in time

    mysqlbinlog [--no-defaults] --start-datetime='Year-Month-Day Hour:Minute:Second' --stop-datetime='Year-Month-Day Hour:Minute:Second' Binary log | mysql -u username-ppassword

location based recovery

    There may be both wrong operations and correct operations at the same point in time. Recovery based on position is more accurate.
    mysqlbinlog --stop-position='operation id' Binary log | mysql -u username -p password
    mysqlbinlog --start-position= 'operation id' binary log | mysql -u username -p password

5. Example        

         Add databases, tables, and enter data

         Make a full backup first

         Perform a log rollback (generate a new binary log)

         Continue to enter new data

         Make an incremental backup

         Simulate misoperation to delete user_info table

         Restore full backup

         Restoring an incremental backup

         Point-in-time incremental backup and recovery

        Restore Zhao Liu but not Sun Qi

        Restore Sun Qi but not Zhao Liu

          Location-based recovery. Restoring Sun Qi does not restore Zhao Liu.

Guess you like

Origin blog.csdn.net/a872182042/article/details/131966764