MySQL Backup & Recovery


Published on

This seminar would include the backup and recovery methods of MySQL database.

Published in: Software, Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MySQL Backup & Recovery

  2. 2. Session Overview When do we need backups? What needs to be backed up? When should backups be performed? Where will the backups be stored? How can backups be performed? Restoring from backups Backup strategy
  3. 3. Backup – Why do it? ● In business today,'You are your data' ● Lose data,lose business ● Lost productivity, customer relationships,etc
  4. 4. Types of Backup ● Logical backup ● Physical backup ● Local backup ● Remote backup ● Full backup ● Incremental backup
  5. 5. When do we need backup? ● Hardware Failures – A system crash may cause some of the data in the database to be lost. – Hardware failure leads to data loss. ● User/Application Failure – Accidental DROP or DELETE statements – Editing table files with text editors,usually leading to corrupt tables.
  6. 6. What needs to be backed up? ● Database content – For full backups – Logical or physical backup ● Log files – For incremental backups – Point in time recovery ● Configuration information – /etc/my.cnf – Cron jobs
  7. 7. When should backups be performed? ● On a regular basis ● Not during high usage peaks(off hours) ● Static data can be backed up less frequently ● Schedule it at particular time intervals
  8. 8. Where to store backup? ● On the database server On a separate file system/volume or hard disk drive ● Copied to another server On or off site Cloud storage ● Backed up to tape/disk ● Choose multiple locations
  9. 9. Database Backup Methods ● Backup programs Mysqldump Mysqlhotcopy ● Copying table files (*.frm, *.MYD, and *.MYI files) ● Delimited-Text files ● Incremental Backups using Binary Log ● Backups using Replication Slaves
  10. 10. Using mysqldump for backups Mysqldump is a backup program originally written by Igor Romanenko. Used to dump a database or a collection of databases for backup or transfer to another server(not necessarily MySQL). Mysqldump writes SQL statements to the standard output. This output consists of CREATE statements to create dumped objects (databases, tables, stored routines etc) and INSERT statements to load data into tables. The output can be saved in a file and reloaded later using mysql to recreate the dumped objects. Options are available to modify the format of the SQL statements, and to control which objects are dumped. Generates files in CSV, other delimited text, or XML format also.
  11. 11. Backup using mysqldump Set of one or more tables $ mysqldump -u [uname] -p[pass] db_name table1 > table_backup.sql $ mysqldump -u [uname] -p[pass] db_name table1 table2 > table_backup.sql Set of one or more complete databases $ mysqldump -u [uname] -p[pass] db_name1 db_name2 > db_backup.sql All databases $ mysqldump -u [uname] -p[pass] –all-databases > all_db_backup.sql An entire MySQL $ mysqldump -u [uname] -p[pass] db_name > db_backup.sql Copying data from one server to another $ mysqldump --opt db_name |mysql –host = remote_host -C db_name Auto-compressing the output using gzip $ mysqldump -u [uname] -p[pass] db_name | gzip > db_backup.sql.gz Remote Backup $ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name > db_backup.sql
  12. 12. Restore backup from mysqldump ● Create a database on the target machine ● Load the file using the mysql command: $ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql] ● To restore to an existing database $ mysqlimport -u [uname] -p[pass] [dbname] < [backupfile.sql]
  13. 13. Making delimited text file backups ● This method saves only table data, not the table structure. ● Writes the selected rows to a file on the server host Syntax SELECT * INTO OUTFILE 'file_name' FROM tbl_name ● Example of file in the comma-separated values (CSV) format SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY 'n' FROM table;
  14. 14. Making Incremental Backups by Enabling the Binary Log ● By default, the server creates binary log files in the data directory. ● The binary log files provide you with the information you need to replicate changes to the database that are made subsequent to the point at which you performed a backup. ● To see a listing of all binary log files mysql> SHOW BINARY LOGS; ● An incremental backup only backs up data that changed since the previous backup. ● Start the server with the --log-bin option to enable binary logging. ● mysqlbinlog utility converts the events in the binary log files from binary format to text so that they can be executed or viewed. mysqlbinlog has options for selecting sections of the binary log based on event times or position of events within the log. ● Copy to the backup location,all binary logs from the moment the last backup was taken to the last but one.
  15. 15. Enable binary logging Configure mysql to do binary logging. Edit : /etc/mysql/my.cnf: Add : log-bin = mybinlog Specify which databases to do binary logging for, or which databases NOT to do binary logging for. 1) “binlog_do_db” turns binary logging on for a given database. binlog_do_db = mydb 2) “binlog_ignore_db” turns binary logging on for all databases except the database(s) names. binlog_ignore_db = mydb Restart mysql server.
  16. 16. Restore from binary logs ● Go to the folder where mysqlbinlog utility is placed and convert binary log file into sql C:xamppmysqlbin>mysqlbinlog C:xamppmysqldatabinlogbin-log.000001 > sql.txt ● Directly execute sql on the database C:xamppmysqlbin>mysqlbinlog C:xamppmysqldatabinlogbin-log.000001 | mysql -u root -p ● Specify specific duration to extract sql C:xamppmysqlbin>mysqlbinlog --start-datetime="2014-06-01 10:31:44" C:xamppmysqldatabinlogbin-log.000001 > sql.txt This will extract sql after the date of 2014-06-01 10:31:44.
  17. 17. Point In Time Recovery using binary logs 1) Point-in-Time Recovery Using Event Times C:xamppmysqlbin>mysqlbinlog --start-datetime="2014-05-27 10:01:00" C:xamppmysqldatabinlogbin-log.000001 | mysql -u root -p C:xamppmysqlbin>mysqlbinlog --stop-datetime="2014-05-27 9:59:59" C:xamppmysqldatabinlogbin-log.000001 | mysql -u root -p 2) Point-in-Time Recovery Using Event Position C:xamppmysqlbin>mysqlbinlog --stop-position=368312 C:xamppmysqldatabinlogbin-log.000001 | mysql -u root -p C:xamppmysqlbin>mysqlbinlog --start-position=368315 C:xamppmysqldatabinlogbin-log.000001 | mysql -u root -p
  18. 18. Making Backups Using Replication Slaves ● Used when there are performance problems with your master server while making backups. ● Set up replication and perform backups on the slave rather than on the master. ● Put the master server db in a read-only state by executing these statements: mysql> FLUSH TABLES WITH READ LOCK; mysql> SET GLOBAL read_only = ON; ● While Master is read only, perform the backup using mysqldump. ● Restore Master to its normal operational state by executing these statements: mysql> SET GLOBAL read_only = OFF; mysql> UNLOCK TABLES;
  19. 19. Recovering from Backups ● Restoring tables to the state before a crash requires both the backup files and the binary log. Backup files restore the table to the state they were at the time of backup. The synchronised binary logs are used to extract queries issued between the backup and now. ● If recovering data lost due to unwise queries,dont issue them again. DB Recovery = Last full backup & binlogs
  20. 20. Recovering from Corrupt tables ● Bring the database in recovery mode Shut down incase it's still running Add innodb_force_recovery=1 to my.cnf Change the port from 3306 to some random value. ● Check for the corrupt tables using mysqlcheck –all-databases ● Backup and drop corrupted tables using mysqldump ● Restart mysql in normal mode without changing the port ● Import the backup file ● Change port
  21. 21. Backup Strategy Perform backups regularly and frequently Performing Backups Before and After You Make Structural Changes Turn on the binary update log Synchronise update logs with the backup files Store the backups on a different file system than where your databases are Make periodic full backups, using the mysqldump command Make periodic incremental backups by flushing the logs Scheduling backups Choose the right storage platform for backups
  22. 22. References ● ● ●
  23. 23. THANK YOU