MySQL Data destruction restoration and data automatic backup

Source: Internet
Author: User
Tags chmod mysql tutorial centos create database

Establish an automatic backup script
Here, in order to make the database tutorials backup and restore meet our actual requirements, use a qualifying shell script to automate the entire backup process.
[Root@centos ~]# vi mysql Tutorial-backup.sh← set up a 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

[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

This is similar to the "recovery method after database deletion" described above. Here, the test database then uses test that you just used earlier. In order to make the friends who just contacted the database not understand the confusion, we log on to the MySQL server again to confirm the information about the test database tests that we just created.
[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 |
+------------+
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|
+------+--------------------+
1 row in Set (0.01 sec)

mysql> exit← exit MySQL server
Bye

Then we run the database backup script again and back up the current state of the database again.
[Root@centos ~]# cd← back to the root directory of the root user where the script resides
[Root@centos ~]#./mysql-backup.sh← Run scripts for database backups

Next, we log on to the MySQL server again and make some changes to the test's database test to be able to test the success of the data recovery.
[Root@sample ~]# 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
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> Update test set name= ' Shit,windows '; ← Then redefine the value of the table in test to "Shit,windows" (originally "Hello,centos")
Query OK, 1 row affected (0.07 sec)
Rows matched:1 changed:1 warnings:0

Mysql> select * from test; ← Confirm that the table in test has a defined value
+------+--------------------+
| num | name |
+------+-------------------+
| 1 |  Shit,windows | ← Confirm that the values in the original test database table have been modified to the new value "Shit,windows"
+------+-------------------+
1 row in Set (0.00 sec)

mysql> exit← exit MySQL server
Bye

Above, we are tantamount to simulating the process of database tampering. Next, the database is "tampered" with 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

Then, log on to the MySQL server again to see if the database was restored to the state before it was tampered with.
[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
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> 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 it is modified!
+------+----------------+
1 row in Set (0.01 sec)

mysql> exit← exit MySQL server
Bye

The above results indicate that the database has been modified to successfully restore the data to the state before it was "tampered" with the backed-up database.
After the test ...
After the test is complete, delete the legacy information that was used for the test.
[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
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 test database
Query OK, 0 rows affected (0.01 sec)

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

mysql> show databases; ← View the currently existing database
+-------------+
| Database |
+-------------+
|  MySQL | ← Confirm that test data database test does not exist and has been deleted
+-------------+
1 row in Set (0.00 sec)

mysql> exit← exit MySQL server
Bye

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.