[Transfer from 51cto] http://bbs.51cto.com/thread-820965-1.html
?
? When the database server is established, the first thing we do not consider what programs run by MySQL tie-in to the server database support, but when the database is destroyed, how to safely restore the last normal state, so data loss is minimized.
Or, just to establish a database server, and only shows what it can do, not what it can do on behalf of stable. A quasi-disaster recovery factor of stability and efficiency of comprehensiveness, but also the system, especially for a server system.
This section describes the automatic database backup and recovery methods after the database is damaged. Here, we use mysqlhotcopy, and defined period of Shell script to achieve automatic backup of the database, and the entire automatic data backup and data recovery processes are based on Shell.
The conditions necessary to create a backup database
[1] set up an automated backup script
Here, in order to make the database backup and recovery in line with our actual requirements, with some of the requirements in line with Shell script to automate the entire backup process.
[Root @ sample ~] # vi mysql-backup.sh ← automatically establish a database backup script, as follows:
# SHELL statement and execution path
#!/bin/bash
PATH=/usr/local/sbin:/usr/bin:/bin
# Set the directory backup
BACKDIR=/backup/mysql
# Set the ROOT password
ROOTPASS = ******** ← asterisk will replace the MySQL root password
# Redo the backup directory
rm -rf $BACKDIR
mkdir -p $BACKDIR
# Get the database name
DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /`?#tr - translate or delete characters[ -d, --delete]
# backup database
for dbname in $DBLIST
do
mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy
done
[2] run automatic database backup script
[Root @ sample ~] # chmod 700 mysql-backup.sh ← change script attributes, let only allow root user to perform
[Root @ sample ~] # https://blog.csdn.net/junli_chen/article/details/mysql-backup.sh ← run the script
[Root @ sample ~] # ls -l / backup / mysql / ← confirm whether the backup was successful
total 8
drwxr-x --- 2 mysql mysql 4096 Sep 1 16:54 mysql ← has been successfully backed up to / backup / mysql directory
[3] so that the database backup script to run automatically every day
[Root @ sample ~] # crontab -e ← edit automatically operating rules (and then edit window appears, with the operation vi)
00 03 * * * /root/mysql-backup.sh ← add this line to the file, so that the database backup 3:00 every day
Automatic backup test normal operation or not (method of backup and recovery) here, to process through the actual operation of the recovery method to introduce the problems.
[1] When the recovery method after the database is deleted
First, the establishment of a database of test.
[Root @ sample ~] # mysql -u root -p ← Log in to the MySQL server as root
Enter password: ← Enter MySQL root user password
# Enter the following BANNER
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.
#BANNER end
mysql> create database test; ← a test to establish a database of test
Query OK, 1 row affected (0.00 sec)
mysql> use test ← connect to the database
Database changed
mysql> create table test (num int, name varchar (50)); ← establishing a table in the database
Query OK, 0 rows affected (0.07 sec)
mysql> insert into test values (1, 'Hello, BSD'); ← inserted into this table a value (here for "Hello, BSD" Example)
Query OK, 1 row affected (0.02 sec)
mysql> select * from test; ← view the contents of the database
+------+-------------+
| num? | name |
+------+-------------+
| 1 | Hello, BSD | ← confirmed presence value just inserted into the table
+------+-------------+
1 row in set (0.01 sec)
mysql> exit? ← quit MySQL server
Bye
Then, the database backup script just created, the newly established database backup testing.
[Root @ sample ~] # cd? ← back to the root user script located in the root directory
[Root @ sample ~] # https://blog.csdn.net/junli_chen/article/details/mysql-backup.sh ← database backup script run
Next, we once again log on to the MySQL server, delete the database test test of just created, in order to restore the success of the test data.
[Root @ sample ~] # mysql -u root -p ← Log in to the MySQL server as root
Enter password:? ← Enter MySQL root user password
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use test ← connected to the test database test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> drop table test;? ← delete the data in the table
Query OK, 0 rows affected (0.04 sec)
mysql> drop database test;? ← delete the test database test
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+-------------+
| Database |
+-------------+
| Mysql | test database ← confirmation test no longer exist, has been deleted
+-------------+
1 row in set (0.01 sec)
mysql> exit ← quit MySQL server
Bye
Above, we simulated the process is equivalent to the database is damaged. Next, after the database is "broken", with a backup method for recovery.
[Root @ sample ~] # / bin / cp -Rf / backup / mysql / test / / var / lib / mysql / ← backup copy of the database to the appropriate directory test
[Root @ sample ~] # chown -R mysql: mysql / var / lib / mysql / test / ← changing the home database of test mysql
[Root @ sample ~] # chmod 700 / var / lib / mysql / test / ← change directory attribute database 700
[root@sample ~]# chmod 660 /var/lib/mysql/testbin/bashyourpath...