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...
Outline
 Using mysqldump utility to back up tables in a
single or multiple databases.
 Using backup files with mysql mon...
Introduce
 Despite the steps you take to secure and protect your
databases, events such as power failures, natural
disast...
Introduce
 Using these backup files, you can recover your data to the
state it was in at the time you performed the last ...
Usage of mysqldump utility
 It is possible to back up databases simply by
copying the data directory to a backup location...
Using mysqldump
Syntax:
mysqldump [options] dbname [tables]
mysqldump [options] --databases [moreoptions]
dbname1 [dbnam...
Using mysqldump
Options:
--no-create-info: Creates no CREATE TABLE
commands, but only the INSERT commands.
--no-data : C...
Set Variables
 MySQL saves some system values in user-defined
variables to restore the original system settings
should th...
Set Variables
/*!40101 SET
@OLD_CHARACTER_SET_CLIENT=CHARACTER_SET_CLIENT */;
/*!40101 SET
CHARACTER_SET_CLIENT=@OLD_CHARA...
Flush Logs
 Binary log files allow you to restore your database
fully. The option --flush-logs flushes your log files
and...
Examples
 Mysqldump –u root –pa sakila >
D:ProgramsEasyPHPtmpsakila1.sql
 mysqldump --flush-logs -u root -pa sakila >
D:...
Restore Data
 If you have backed up your files regularly and
enabled binary logging, restoring your database
consists of ...
Restore Data
 If you are restoring data from a backup file that does
not include a database definition, the database
must...
Update the Restored db From Binary Log
Files
 Once database is reloaded, the data is only as
current as your last backup,...
Update the Restored db From Binary Log
Files

 MySQL provides two methods for applying updates
from a binary log:
– resto...
Restoring Data Directly From a Binary
Log
 Mysqld --log-bin

 mysqlbinlog “D:ProgramsEasyPHPmysqldata
HN-bin.000001”
 m...
Restoring Binary Log Data From a Text
File
 You can sort through the text file to remove any
statements that you don't wa...
Restoring Binary Log Data From a Text
File
 Mysqlbinlog “D:ProgramsEasyPHPmysqldata HNbin.000001” > HN-bin000001.txt
 Af...
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...
Upcoming SlideShare
Loading in...5
×

7. backup & restore data

310

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
310
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

7. backup & restore data

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×