Wednesday, October 3, 2012

MySQL - backup and restore manually

If you are able to shut down your MySQL server, you can make a binary backup that consists of all files used byInnoDB to manage its tables. Use the following procedure: 
Stage 1:  Copy binary data
  1. Shut down the MySQL server and make sure that it stops without errors.
  2. Copy all InnoDB data files (ibdata files and .ibd files) into a safe place.
  3. Copy all the .frm files for InnoDB tables to a safe place.
  4. Copy all InnoDB log files (ib_logfile files) to a safe place.
  5. Copy your my.cnf configuration file or files to a safe place.
Stage 2: Dump and restore full database
In addition to making binary backups as just described, you should also regularly make dumps of your tables with mysqldump. The reason for this is that a binary file might be corrupted without you noticing it. Dumped tables are stored into text files that are human-readable, so spotting table corruption becomes easier. Also, because the format is simpler, the chance for serious data corruption is smaller. mysqldump also has a --single-transaction option for making a consistent snapshot without locking out other clients.
Stage 3: Restore additional binary logs
To be able to recover your InnoDB database to the present from the time at which the binary backup was made, you must run your MySQL server with binary logging turned on. To achieve point-in-time recovery after restoring a backup, you can apply changes from the binary log that occurred after the backup was made
  • To see a listing of all binary log files, use this statement:
    mysql> SHOW BINARY LOGS;
    To determine the name of the current binary log file, issue the following statement:
  • The 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. See Section 4.6.7, “mysqlbinlog — Utility for Processing Binary Log Files”.
  • Executing events from the binary log causes the data modifications they represent to be redone. This enables recovery of data changes for a given span of time. To execute events from the binary log, process mysqlbinlog output using the mysql client:
    shell> mysqlbinlog binlog_files | mysql -u root -p

No comments: