SlideShare a Scribd company logo
Hệ quản trị cơ sở dữ liệu

Backup and Restore Data
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
hanhdp@vnu.edu.vn
Outline
 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.htm
2

Hệ quản trị CSDL @ BM HTTT
Introduce
 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.
3

Hệ quản trị CSDL @ BM HTTT
Introduce
 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.
4

Hệ quản trị CSDL @ BM HTTT
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.

5

Hệ quản trị CSDL @ BM HTTT
Using mysqldump
Syntax:
mysqldump [options] dbname [tables]
mysqldump [options] --databases [moreoptions]
dbname1 [dbname2 ...]
mysqldump [options] --all-databases [moreoptions]

6

Hệ quản trị CSDL @ BM HTTT
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).

7

Hệ quản trị CSDL @ BM HTTT
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.

8

Hệ quản trị CSDL @ BM HTTT
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.
9

Hệ quản trị CSDL @ BM HTTT
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.
10

Hệ quản trị CSDL @ BM HTTT
Examples
 Mysqldump –u root –pa sakila >
D:ProgramsEasyPHPtmpsakila1.sql
 mysqldump --flush-logs -u root -pa sakila >
D:ProgramsEasyPHPtmpsakila1.sql
 mysqldump –u root -pa -X sakila actor >
D:ProgramsEasyPHPtmpactor1.xml
 mysqldump --all-databases >
D:ProgramsEasyPHPtmpall_db.sql;
11

Hệ quản trị CSDL @ BM HTTT
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.
12

Hệ quản trị CSDL @ BM HTTT
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:ProgramsEasyPHPtmpsakila1.sql
or
Mysql –u root –pa test<
13

D:ProgramsEasyPHPtmpsakila1.sql
Hệ quản trị CSDL @ BM HTTT
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.
14

Hệ quản trị CSDL @ BM HTTT
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.

15

Hệ quản trị CSDL @ BM HTTT
Restoring Data Directly From a Binary
Log
 Mysqld --log-bin

 mysqlbinlog “D:ProgramsEasyPHPmysqldata
HN-bin.000001”
 mysqlbinlog
"D:ProgramsEasyPHPmysqldataHN-bin.000001"
> D:ProgramsEasyPHPmysqldatalog.txt
 mysqlbinlog
"D:ProgramsEasyPHPmysqldataHN-bin.000005"
|mysql -u root –p --one-database sakila1
16

Hệ quản trị CSDL @ BM HTTT
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.
17

Hệ quản trị CSDL @ BM HTTT
Restoring Binary Log Data From a Text
File
 Mysqlbinlog “D:ProgramsEasyPHPmysqldata HNbin.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

18

Hệ quản trị CSDL @ BM HTTT
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
19

nhất (dùng 2 cách).
Hệ quản trị CSDL @ BM HTTT

More Related Content

PPT
6.2 my sql queryoptimization_part1
PDF
MySQL and bioinformatics
PDF
2713897 oracle-unix-oracle
PPT
DB2UDB_the_Basics Day2
PPT
DB2UDB_the_Basics Day 6
PPTX
S3 l6 db2 - memory model
PDF
Sapnote 0000071254
PPTX
S3 l4 db2 environment - databases
6.2 my sql queryoptimization_part1
MySQL and bioinformatics
2713897 oracle-unix-oracle
DB2UDB_the_Basics Day2
DB2UDB_the_Basics Day 6
S3 l6 db2 - memory model
Sapnote 0000071254
S3 l4 db2 environment - databases

What's hot (19)

PPTX
database backup and recovery
PPTX
S3 l7 db2 storage model
PPT
DB2UDB_the_Basics Day 5
PDF
Database recovery techniques
PPT
DB2UDB_the_Basics Day 3
PPTX
Memory management in oracle
PDF
DB2 LUW V11.1 CERTIFICATION TRAINING PART #1
PPTX
C++ Memory Management
PDF
Backup and recovery in oracle
PPTX
Recovery Techniques and Need of Recovery
PPTX
Memory management
PDF
DBMS 10 | Database Transactions
PPTX
Introduction of Memory Management
PPSX
Understanding memory management
PPTX
Process & Mutlithreading
PPTX
BACKUP & RECOVERY IN DBMS
PPTX
SHADOW PAGING and BUFFER MANAGEMENT
PPT
4 (1)
PPT
DB2UDB_the_Basics
database backup and recovery
S3 l7 db2 storage model
DB2UDB_the_Basics Day 5
Database recovery techniques
DB2UDB_the_Basics Day 3
Memory management in oracle
DB2 LUW V11.1 CERTIFICATION TRAINING PART #1
C++ Memory Management
Backup and recovery in oracle
Recovery Techniques and Need of Recovery
Memory management
DBMS 10 | Database Transactions
Introduction of Memory Management
Understanding memory management
Process & Mutlithreading
BACKUP & RECOVERY IN DBMS
SHADOW PAGING and BUFFER MANAGEMENT
4 (1)
DB2UDB_the_Basics
Ad

Viewers also liked (20)

PPT
6.3 my sql queryoptimization_part2
PPT
8.replication
PPT
PPT
5. indexing
PPT
4.2 transaction 2
PPT
4.2 transaction
PPT
6.1 query optimization overview
PPT
2.2 cac chuong trinh my sql
PPT
01 gioithieu
PPT
2.1 view
PPT
9. partitioning
PPT
07 trigger view
PDF
Pdf bai 6 làm việc với truy vấn cơ bản-slide 06-quan tri csdl voi access-mast...
PPT
C3 2 (tuan6,7)
PPT
PPT
C4 1 tuan 14
PPT
PPT
PPT
2.3 quan ly truy cap
PDF
Pdf bai 1 tổng quan về ms access-quan tri csdl voi access-mastercode.vn
6.3 my sql queryoptimization_part2
8.replication
5. indexing
4.2 transaction 2
4.2 transaction
6.1 query optimization overview
2.2 cac chuong trinh my sql
01 gioithieu
2.1 view
9. partitioning
07 trigger view
Pdf bai 6 làm việc với truy vấn cơ bản-slide 06-quan tri csdl voi access-mast...
C3 2 (tuan6,7)
C4 1 tuan 14
2.3 quan ly truy cap
Pdf bai 1 tổng quan về ms access-quan tri csdl voi access-mastercode.vn
Ad

Similar to 7. backup & restore data (20)

PDF
MySQL Backup & Recovery
PDF
MySQL for Oracle DBAs
DOCX
iLAB OVERVIEWScenario and SummarySuccessful database recovery re.docx
DOCX
All types of backups and restore
PPTX
DBMS: Week 14 - Backup and Recovery in MySQL
PPTX
PPT
C_mysql-1.ppt
PPT
PPT
Mysql ppt
PDF
Data administration
PDF
MySQL Backup and Security Best Practices
PDF
17398351 sap-system-copy-homcopyv1
PPTX
Data Guard New Features
PDF
MySQL Server Backup, Restoration, and Disaster Recovery Planning
PDF
Get mysql clusterrunning-windows
PDF
Introduction Mysql
PDF
Mysql introduction
DOCX
Multiple instance on windows
MySQL Backup & Recovery
MySQL for Oracle DBAs
iLAB OVERVIEWScenario and SummarySuccessful database recovery re.docx
All types of backups and restore
DBMS: Week 14 - Backup and Recovery in MySQL
C_mysql-1.ppt
Mysql ppt
Data administration
MySQL Backup and Security Best Practices
17398351 sap-system-copy-homcopyv1
Data Guard New Features
MySQL Server Backup, Restoration, and Disaster Recovery Planning
Get mysql clusterrunning-windows
Introduction Mysql
Mysql introduction
Multiple instance on windows

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
cuic standard and advanced reporting.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
Teaching material agriculture food technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
A Presentation on Artificial Intelligence
Encapsulation_ Review paper, used for researhc scholars
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
Modernizing your data center with Dell and AMD
cuic standard and advanced reporting.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Per capita expenditure prediction using model stacking based on satellite ima...
Teaching material agriculture food technology

7. backup & restore data

  • 1. Hệ quản trị cơ sở dữ liệu Backup and Restore Data 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 hanhdp@vnu.edu.vn
  • 2. Outline  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.htm 2 Hệ quản trị CSDL @ BM HTTT
  • 3. Introduce  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. 3 Hệ quản trị CSDL @ BM HTTT
  • 4. Introduce  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. 4 Hệ quản trị CSDL @ BM HTTT
  • 5. 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. 5 Hệ quản trị CSDL @ BM HTTT
  • 6. Using mysqldump Syntax: mysqldump [options] dbname [tables] mysqldump [options] --databases [moreoptions] dbname1 [dbname2 ...] mysqldump [options] --all-databases [moreoptions] 6 Hệ quản trị CSDL @ BM HTTT
  • 7. 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). 7 Hệ quản trị CSDL @ BM HTTT
  • 8. 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. 8 Hệ quản trị CSDL @ BM HTTT
  • 9. 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. 9 Hệ quản trị CSDL @ BM HTTT
  • 10. 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. 10 Hệ quản trị CSDL @ BM HTTT
  • 11. Examples  Mysqldump –u root –pa sakila > D:ProgramsEasyPHPtmpsakila1.sql  mysqldump --flush-logs -u root -pa sakila > D:ProgramsEasyPHPtmpsakila1.sql  mysqldump –u root -pa -X sakila actor > D:ProgramsEasyPHPtmpactor1.xml  mysqldump --all-databases > D:ProgramsEasyPHPtmpall_db.sql; 11 Hệ quản trị CSDL @ BM HTTT
  • 12. 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. 12 Hệ quản trị CSDL @ BM HTTT
  • 13. 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:ProgramsEasyPHPtmpsakila1.sql or Mysql –u root –pa test< 13 D:ProgramsEasyPHPtmpsakila1.sql Hệ quản trị CSDL @ BM HTTT
  • 14. 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. 14 Hệ quản trị CSDL @ BM HTTT
  • 15. 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. 15 Hệ quản trị CSDL @ BM HTTT
  • 16. Restoring Data Directly From a Binary Log  Mysqld --log-bin  mysqlbinlog “D:ProgramsEasyPHPmysqldata HN-bin.000001”  mysqlbinlog "D:ProgramsEasyPHPmysqldataHN-bin.000001" > D:ProgramsEasyPHPmysqldatalog.txt  mysqlbinlog "D:ProgramsEasyPHPmysqldataHN-bin.000005" |mysql -u root –p --one-database sakila1 16 Hệ quản trị CSDL @ BM HTTT
  • 17. 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. 17 Hệ quản trị CSDL @ BM HTTT
  • 18. Restoring Binary Log Data From a Text File  Mysqlbinlog “D:ProgramsEasyPHPmysqldata HNbin.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 18 Hệ quản trị CSDL @ BM HTTT
  • 19. 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 19 nhất (dùng 2 cách). Hệ quản trị CSDL @ BM HTTT