MySQL automatic backup and the recovery method after the database is destroyed 1th/2 page _mysql

Source: Internet
Author: User
Tags chmod create database mysql automatic backup

First, the foreword:

When the database server is established, the first thing we need to do is not to consider what MySQL-enabled programs are running on this database-supporting server, but how to recover to the last normal state after the database has been compromised, minimizing the loss of data.

Or, just the establishment of a database server, can only show what it can do, does not mean that it can be stable to do something. The efficiency and comprehensiveness of disaster recovery is also a quasi factor of system stability, especially for a server system.

This section describes the methods for automatic database backups and recovery after a database has been compromised. Here, we use mysqlhotcopy and define a shell script to automate the database backup, and make the entire data automatic backup and data recovery process based on the shell.

Conditions required to establish a database backup

[1] Establish an automatic backup script

Here, in order for database backup and recovery to meet our actual requirements, a consistent shell script is used to automate the entire backup process.

[Root@centos ~]# VI mysql-backup.sh← establishes the database automatic backup script, as follows:

#!/bin/bash

Path=/usr/local/sbin:/usr/bin:/bin

# The Directory of Backup
Backdir=/backup/mysql

# The Password of MySQL
rootpass=******** here, replace the asterisk with the MySQL root password

# remake The Directory of Backup
RM-RF $BACKDIR
Mkdir-p $BACKDIR

# get the Name of Database
dblist= ' Ls-p/var/lib/mysql | grep/| tr-d/'

# Backup with Database
For dbname in $DBLIST
Todo
Mysqlhotcopy $dbname-u root-p $ROOTPASS $BACKDIR | Logger-t mysqlhotcopy
Done

[2] Run the database automatic backup script

[Root@centos ~]# chmod mysql-backup.sh Changes script properties so that it can only be executed by the root user
[Root@centos ~]#./mysql-backup.sh Run Script
[Root@centos ~]# ls-l/backup/mysql/Confirm if Backup is successful
Total 8
Drwxr-x---2 mysql mysql 4096 Sep 1 16:54 MySQL has been successfully backed up to/backup/mysql directory

[3] Make database backup scripts run automatically every day

[Root@sample ~]# crontab-e← edit Autorun rule (then edit window appears, operation with VI)
* * * * */root/mysql-backup.sh Add this line to the file and make the database backup 3 o'clock in the morning daily

Test the normal operation of automatic backups (method of backup recovery)

Here, to introduce the recovery method after the problem occurs through the actual operation process.

[1] Recovery methods when the database is deleted

First, establish a test database.

[Root@centos ~]# mysql-u root-p← login to MySQL server with root
Enter password:← the root user password for MySQL
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 8 to server version:4.1.20

Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.

mysql> CREATE DATABASE test; ← Create a test database
Query OK, 1 row Affected (0.00 sec)

Mysql> use test← connect to this database
Database changed

Mysql> CREATE TABLE Test (num int, name varchar (50)); ← Create a table in the database
Query OK, 0 rows affected (0.07 sec)

mysql> INSERT INTO test values (1, ' Hello,centos '); ← Insert a value into this table (here take "Hello,centos" for example)
Query OK, 1 row affected (0.02 sec)

Mysql> select * from test; ← View the contents of the database
+------+-----------------+
| num | name |
+------+-----------------+
|1 |  Hello,centos | ← Confirm the existence of the value just inserted into the table
+------+------------------+
1 row in Set (0.01 sec)

mysql> exit← exit MySQL server
Bye

Then, run the database backup script that you just created, and back up the test database you just created.

[Root@sample ~]# cd← back to the root directory of the root user where the script resides
[Root@sample ~]#./mysql-backup.sh← Run scripts for database backups

Next, we log on to the MySQL server again and delete the database test that was just created to test the success of the data recovery.

[Root@centos ~]# mysql-u root-p← login to MySQL server with root
Enter password:← the root user password for MySQL
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is to server version:4.1.20

Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.

Mysql> use test← Connect to the test database for testing
Reading table information for completion of table and column names
You can turn off the feature to get a quicker startup with-a

Database changed
mysql> drop table test; ← Delete the table in the data
Query OK, 0 rows affected (0.04 sec)

mysql> drop database test; ← Delete test Data database test
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+---------------+
| Database |
+---------------+
|  MySQL | ← Confirm test Database no longer exists and has been deleted
+---------------+
1 row in Set (0.01 sec)

mysql> exit← exit MySQL server
Bye

Above, we are tantamount to simulating the destruction of the database process. Next, the database is "corrupted", the method of restoring with backup.

[Root@centos ~]#/bin/cp-rf/backup/mysql/test//var/lib/mysql/← Replication Backup Database test to the appropriate directory
[Root@centos ~]# chown-r mysql:mysql/var/lib/mysql/test/← Changes the ownership of database test to MySQL
[Root@centos ~]# chmod 700/var/lib/mysql/test/← Change Database directory property to 700
[Root@centos ~]# chmod 660/var/lib/mysql/test/*← Change the properties of the data in the database to 660

Then, log on to the MySQL server again to see if the database has been successfully restored.

[Root@centos ~]# mysql-u root-p← login to MySQL server with root
Enter password:← the root user password for MySQL
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is to server version:4.1.20

Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.

mysql> show databases; ← View the currently existing database
+-------------+
| Database |
+-------------+
| MySQL |
|  Test | ← Confirm that the test database that has just been deleted has been successfully restored!
+------------+
2 rows in Set (0.00 sec)

Mysql> use test← Connect to the test database
Reading table information for completion of table and column names
You can turn off the feature to get a quicker startup with-a

Database changed
Mysql> Show tables; ← View the tables that exist in the test database
+-------------------+
| Tables_in_test |
+-------------------+
| Test |
+-------------------+
1 row in Set (0.00 sec)

Mysql> select * from test; ← View the contents of the database
+------+---------------------+
| num | name |
+------+---------------------+
| 1 |  Hello,centos | ← Confirm that the content in the datasheet is the same as the "Hello,centos" defined before the deletion!
+------+---------------------+
1 row in Set (0.01 sec)

mysql> exit← exit MySQL server
Bye

The above results indicate that after the database was deleted, the database was successfully restored to the state before the deletion with the backed up data.

current 1/2 page   1 2 Next read the full text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.