{"id":2021,"date":"2015-02-23T12:42:12","date_gmt":"2015-02-23T12:42:12","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=2021"},"modified":"2024-02-16T11:04:30","modified_gmt":"2024-02-16T11:04:30","slug":"restore-dropped-mysql-database-from-binary-logs","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/restore-dropped-mysql-database-from-binary-logs","title":{"rendered":"How to Restore \/ point in time recovery using binary logs MySQL"},"content":{"rendered":"\n<p>In this post I will share a recovery scenario of a MySQL database restore from the binary logs. This post is also a good example of how we can achieve point in time recovery using binary logs.<\/p>\n\n\n\n<p>Recently someone accidentally dropped an important MySQL database and the backup was not present!<\/p>\n\n\n\n<p>As we know the the binary log contains DMLs to table data and that&#8217;s where our hope lies. Luckily the binary log retention period (expire_logs_days) was set to 30 days and the CREATE DATABASE was still present in the binary logs.<\/p>\n\n\n\n<p>Thus this restore of the dropped MySQL database can be done from extracting respective DMLs point-in-time. Here we will play the binary logs against a temporary MySQL instance and extract the required database.<\/p>\n\n\n\n<p>Let&#8217;s begin the recovery of database.<br><\/p>\n\n\n\n<!--more Continue Reading...-->\n\n\n\n<h3 class=\"wp-block-heading\">Create a temporary MySQL instance<\/h3>\n\n\n\n<p>&#8211; We will quickly bring up a temporary MySQL instance to start the restore from binary log.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Prepare data-directory\nmkdir \/var\/lib\/mysql3307\n\n# Setup mysql user &amp; group permission \nchown -R mysql:mysql \/var\/lib\/mysql3307\n\n# Prepare the configuration\ncp \/etc\/my.cnf \/etc\/my3307.cnf\n\n# Initialize MySQL data directory and create system tables\nmysql_install_db --user=mysql --basedir=\/usr --datadir=\/var\/lib\/mysql3307 --defaults-file=\/etc\/my3307.cnf\n\n# Start MySQL instance\nmysqld_safe --defaults-file=\/etc\/my3307.cnf --user=mysql &amp;\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Converting Binary Logs to SQL<\/h3>\n\n\n\n<p>&#8211; We will need to convert the binary logs to SQL to be loaded The create database syntax was found in bin.000005 while drop was in bin.000008.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># To note here the MySQL version is 5.1.73 and default binlog format is STATEMENT.\nmysqlbinlog bin.000005 &gt; bin.000005.sql\nmysqlbinlog bin.000006 &gt; bin.000006.sql\nmysqlbinlog bin.000007 &gt; bin.000007.sql\nmysqlbinlog bin.000008 &gt; bin.000008.sql\n<\/pre>\n\n\n\n<p>&#8211; Removed lines from &#8220;DROP DATABASE&#8221; command onwards from bin.000008.sql and prepared for load.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Loading SQL Files to Temporary MySQL Instance<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql -uroot --socket=\/var\/lib\/mysql\/mysql3307.sock --force &lt; bin.000005.sql 1&gt; 5.sql.log 2&gt;&amp;1\nmysql -uroot --socket=\/var\/lib\/mysql\/mysql3307.sock --force &lt; bin.000006.sql 1&gt; 6.sql.log 2&gt;&amp;1\nmysql -uroot --socket=\/var\/lib\/mysql\/mysql3307.sock --force &lt; bin.000007.sql 1&gt; 7.sql.log 2&gt;&amp;1\nmysql -uroot --socket=\/var\/lib\/mysql\/mysql3307.sock --force &lt; bin.000008.sql 1&gt; 8.sql.log 2&gt;&amp;1\n<\/pre>\n\n\n\n<p>Here &#8211;force will make sure the sql execution will continue to load despite of any errors.<\/p>\n\n\n\n<p>Connected and verified the database required is available and is in acceptable condition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Backup required database to restore<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">mysqldump -uroot -p --socket=\/var\/lib\/mysql\/mysql3307.sock --databases dropped-database | gzip &gt; dump.sql.gz\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Restoring the Database to the Main MySQL Instance<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">zcat dump.sql.gz | mysql -uroot -p \n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">&nbsp;<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Database restore in case of backups available<\/h3>\n\n\n\n<p>&#8211; In case we have a logical backup available with us, a mysqldump, we can restore a single database by extracting it from the full MySQL dump.<br>We can extract the single database from the mysql dump using <a href=\"http:\/\/kedar.nitty-witty.com\/mydumpsplitter-extract-tables-from-mysql-dump-shell-script\" target=\"_blank\" rel=\"noopener\"><b>mysql-dump-splitter<\/b><\/a> as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\/mydumpsplitter.sh fulldump-filename.sql -d database-to-restore\n<\/pre>\n\n\n\n<p>&#8211; For the case of physical backup say xtrabackup, we will have to do complete restore and extract the required database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Accidental database drops can cause chaos, but armed with the power of MySQL binary logs, recovery is achievable. This step-by-step guide illuminates the process of point-in-time restoration, ensuring that lost data finds its way back to your main MySQL instance. With logical and physical backup insights, you&#8217;re now equipped to tackle database recovery challenges effectively.<\/p>\n","protected":false},"excerpt":{"rendered":"In this post I will share a recovery scenario of a MySQL database restore from the binary logs. This post is also a good example of how we can achieve&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,377],"tags":[400,398,952,403,402,953],"class_list":{"0":"post-2021","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-articles","8":"tag-accidantal-drop","9":"tag-mysql-database-restore","10":"tag-mysql-point-in-time-recovery","11":"tag-mysql-recovery","12":"tag-mysql-restore","13":"tag-restore-point-in-time-mysql"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=2021"}],"version-history":[{"count":17,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2021\/revisions"}],"predecessor-version":[{"id":3015,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2021\/revisions\/3015"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=2021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=2021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=2021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}