19
[email protected] Hệ quản trị cơ sở dữ liệu Hệ quản trị cơ sở dữ liệu Dư Phương Hạnh Bộ môn Hệ thống thông tin Khoa CNTT, trường Đại học Công nghệ Đại học Quốc gia Hanoi Backup and Restore Data Backup and Restore Data

7. backup & restore data

Embed Size (px)

Citation preview

Page 1: 7. backup & restore data

[email protected]

Hệ quản trị cơ sở dữ liệuHệ quản trị cơ sở dữ liệu

Dư Phương HạnhBộ môn Hệ thống thông tin

Khoa CNTT, trường Đại học Công

nghệ

Đại học Quốc gia Hanoi

Backup and Restore DataBackup and Restore Data

Page 2: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT2

OutlineOutline

Using mysqldump utility to back up tables in a single or multiple databases.

Using backup files with mysql monitor to restore databases or tables.

Using binary logs to update restored databases. Using various mysqldump Options.

Read more at:

http://mysql-tools.com/en/backup-restore-data-mysql.html

Page 3: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT3

IntroduceIntroduce

Despite the steps you take to secure and protect your

databases, events such as power failures, natural

disasters, and equipment failure can lead to the corruption

and loss of data.

Ensure that MySQL databases are backed up regularly

should be part of any maintenance routine.

These backup files will contain the database and table

definitions necessary to re-create your database structure

as well as the instructions necessary to repopulate your

tables.

Page 4: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT4

IntroduceIntroduce Using these backup files, you can recover your data to the

state it was in at the time you performed the last backup.

Further, you can use the binary log files to recover your data to

post-backup current state.

The mysqldump program is the primary method in MySQL for

backing up tables and databases and it can back up individual

databases, tables in those databases, or multiple databases.

When you run mysqldump, the utility creates a text file that

contains the SQL statements necessary to create your

database and tables safely and add data to those tables.

Page 5: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT5

Usage of mysqldump utility

It is possible to back up databases simply by

copying the data directory to a backup location. This

method has several limitations, though.

– if data is being accessed and updated during file copy,

you might be copying tables that are in an inconsistent

state.

– file-level copying may help MyISAM tables but InnoDB

tables can be more complicated at file level.

Page 6: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT6

Using mysqldump

Syntax:

mysqldump [options] dbname [tables]

mysqldump [options] --databases [moreoptions]

dbname1 [dbname2 ...]

mysqldump [options] --all-databases [moreoptions]

Page 7: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT7

Using mysqldump

Options:

--no-create-info: Creates no CREATE TABLE

commands, but only the INSERT commands.

--no-data : Creates no INSERT commands (but only

CREATE TABLE commands in order to restore the

database schema).

Page 8: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT8

Set Variables

MySQL saves some system values in user-defined

variables to restore the original system settings

should they be changed by any of the statements

while executing the backup file

thereby ensuring that your environment is left in the

same state after the restore via execution of the

statements in the backup file.

Page 9: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT9

Set Variables

/*!40101 SET

@OLD_CHARACTER_SET_CLIENT=CHARACTER_SET_CLIENT */;

/*!40101 SET

CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

SET statements begin with the /*! symbols and end with the */

symbols. The symbols ensure the statements are executed by

MySQL but ignored if they are executed in another database

management system

The number followed those symbols represents a MySQL

server version.

Page 10: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT10

Flush Logs

Binary log files allow you to restore your database

fully. The option --flush-logs flushes your log files

and a new binary log file is created.

By flushing the logs, you get an exact starting point

for using the binary logs when refreshing your

restored database. It is recommended that you use

the --flush- logs option whenever you back up your

data.

Page 11: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT11

Examples

Mysqldump –u root –pa sakila > D:\Programs\

EasyPHP\tmp\sakila1.sql

mysqldump --flush-logs -u root -pa sakila > D:\

Programs\EasyPHP\tmp\sakila1.sql

mysqldump –u root -pa -X sakila actor > D:\

Programs\EasyPHP\tmp\actor1.xml

mysqldump --all-databases > D:\Programs\

EasyPHP\tmp\all_db.sql;

Page 12: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT12

Restore Data

If you have backed up your files regularly and

enabled binary logging, restoring your database

consists of two steps:

– Use the mysql monitor to execute the backup script to

reload a MySQL database.

– Use the appropriate binary logs to update the database.

These two steps restore a database to the point of

database errors, thus preventing any significant

data loss in case of a faulure or a disaster.

Page 13: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT13

Restore Data

If you are restoring data from a backup file that does not

include a database definition, the database must exist

before restoring.

CREATE DATABASE sakila1;

Use sakila1;

Source D:\Programs\EasyPHP\tmp\sakila1.sql

or

Mysql –u root –pa test< D:\Programs\EasyPHP\tmp\

sakila1.sql

Page 14: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT14

Update the Restored db From Binary Log Files Once database is reloaded, the data is only as

current as your last backup, which is where binary

logging comes in.

 After you reload your database into your system,

you will most likely want to get the database to its

most current state since it was backed up. You will

use binary logs which track all data modifications

that occur in your databases.

Page 15: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT15

Update the Restored db From Binary Log Files MySQL provides two methods for applying updates

from a binary log:

– restoring data directly from the binary log file

– exporting binary log data to a text file and then restoring it

from that file.

 You must have binary logging enabled on your

system to be able to use it to update a restored

database, covered in other lessons.

Page 16: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT16

Restoring Data Directly From a Binary Log Mysqld --log-bin

mysqlbinlog “D:\Programs\EasyPHP\mysql\data\ HN-

bin.000001”

mysqlbinlog "D:\Programs\EasyPHP\mysql\data\HN-

bin.000001" > D:\Programs\EasyPHP\mysql\data\

log.txt

mysqlbinlog "D:\Programs\EasyPHP\mysql\data\HN-

bin.000005" |mysql -u root –p --one-database sakila1

Page 17: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT17

Restoring Binary Log Data From a Text File You can sort through the text file to remove any

statements that you don't want to execute.

The larger the log file, the more difficult this process

can be, but there might be times when this is the only

way you can ensure that your database is fully

restored.

After you're satisfied that the text file contains only

the correct statements, you can use the mysql client

utility to execute the statements.

Page 18: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT18

Restoring Binary Log Data From a Text File Mysqlbinlog “D:\Programs\EasyPHP\mysql\data\ HN-

bin.000001” > HN-bin000001.txt

After editing the text file as necessary, execute the

statements in the text file.

Mysql -u root –p –one-database < HN-bin000001.txt

Page 19: 7. backup & restore data

Hệ quản trị CSDL @ BM HTTT19

Exercise Tạo CSDL test

Tạo bảng bảng table1(ID int, Text text);

Bật chế độ log-bin

Chèn dữ liệu vào bảng.

Backup test.

Xoá một bản ghi trong table1.

Khôi phục lại test từ file backup.

Sử dụng logs để khôi phục table1 về trạng thái gần nhất (dùng

2 cách).