{"id":2674,"date":"2022-12-14T17:39:51","date_gmt":"2022-12-14T17:39:51","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=2674"},"modified":"2023-07-22T17:39:57","modified_gmt":"2023-07-22T17:39:57","slug":"restore-single-mysql-table-from-xtrabackup","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/restore-single-mysql-table-from-xtrabackup","title":{"rendered":"Restore single MySQL table (ibd) from Xtrabackup (video demo)"},"content":{"rendered":"\n<p>In this blog we will create MySQL backup and then restore single table from Percona Xtrabackup without doing full restore. This post is basically a work-log in response to a feedback on my previous blog about <a href=\"http:\/\/kedar.nitty-witty.com\/mysql-interview-chatgpt-is-not-your-dba\" target=\"_blank\" rel=\"noopener\" title=\"\">MySQL Interview with ChatGPT<\/a>.<\/p>\n\n\n\n<p>Here is a video recording of the steps described below for restoring single table from xtrabackup.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Restore single MySQL table from Xtrabackup\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/nDMxPn49xLg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><figcaption class=\"wp-element-caption\">Restore single MySQL Table From Xtrabackup<\/figcaption><\/figure>\n\n\n\n<p><strong>Create a compressed MySQL backup using Xtrabackup<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">xtrabackup --backup --compress --compress-threads=8 --target-dir=\/root\/backupdir<\/pre>\n\n\n\n<p><strong>Output in backup directory<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@ip-172-31-83-17 ~]# ls -lhtr \/root\/backupdir<br>total 3.1M<br>-rw-r-----. 1 root root 164K Dec 14 17:11 ibdata1.qp<br>drwxr-x---. 2 root root 31 Dec 14 17:11 sys<br>drwxr-x---. 2 root root 23 Dec 14 17:11 test<br>-rw-r-----. 1 root root 2.3M Dec 14 17:11 mysql.ibd.qp<br>-rw-r-----. 1 root root 287K Dec 14 17:11 undo_002.qp<br>-rw-r-----. 1 root root 287K Dec 14 17:11 undo_001.qp<br>drwxr-x---. 2 root root 161 Dec 14 17:11 mysql<br>drwxr-x---. 2 root root 8.0K Dec 14 17:11 performance_schema<br>-rw-r-----. 1 root root 188 Dec 14 17:11 binlog.000010.qp<br>-rw-r-----. 1 root root 106 Dec 14 17:11 binlog.index.qp<br>-rw-r-----. 1 root root 105 Dec 14 17:11 xtrabackup_binlog_info.qp<br>-rw-r-----. 1 root root 383 Dec 14 17:11 xtrabackup_logfile.qp<br>-rw-r-----. 1 root root 137 Dec 14 17:11 xtrabackup_checkpoints<br>-rw-r-----. 1 root root 1.4K Dec 14 17:11 ib_buffer_pool.qp<br>-rw-r-----. 1 root root 445 Dec 14 17:11 backup-my.cnf.qp<br>-rw-r-----. 1 root root 458 Dec 14 17:11 xtrabackup_info.qp<br>-rw-r-----. 1 root root 130 Dec 14 17:11 xtrabackup_tablespaces.qp<br><\/pre>\n\n\n\n<p><strong>Dropping a table (to create dummy emergency)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@ip-172-31-83-17 ~]# mysql<br>...<br>mysql&gt; drop table test.t1;<br>Query OK, 0 rows affected (0.02 sec)<\/pre>\n\n\n\n<p><strong>Copy backup files to a temporary location to extract single table for restoring<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@ip-172-31-83-17 ~]# mkdir \/tmp\/restore<br>[root@ip-172-31-83-17 ~]# cp -R \/root\/backupdir\/* \/tmp\/restore<\/pre>\n\n\n\n<p><strong>Decompress the backup files<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@ip-172-31-83-17 ~]# xtrabackup --decompress --target-dir=\/tmp\/restore --remove-original\n2022-12-14T17:12:18.239004-00:00 0 [Note] [MY-011825] [Xtrabackup] recognized server arguments: --datadir=\/var\/lib\/mysql --log_bin=\/var\/lib\/mysql\/binlog\n2022-12-14T17:12:18.239194-00:00 0 [Note] [MY-011825] [Xtrabackup] recognized client arguments: --user=root --password=* --decompress=1 --target-dir=\/tmp\/restore --remove-original=1\nxtrabackup version 8.0.30-23 based on MySQL server 8.0.30 Linux (x86_64) (revision id: 873b467185c)\n2022-12-14T17:12:18.259287-00:00 0 [Note] [MY-011825] [Xtrabackup] decompressing .\/backup-my.cnf.qp\n...<\/pre>\n\n\n\n<p><br><br><strong>Prepare the backup for importing with &#8211;export option<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@ip-172-31-83-17 ~]# xtrabackup --prepare --export --target-dir=\/tmp\/restore\n...\n2022-12-14T17:12:27.172446-00:00 0 [Note] [MY-011825] [Xtrabackup] recognized server arguments: --innodb_checksum_algorithm=crc32 --innodb_log_checksums=1 --innodb_data_file_path=ibdata1:12M:autoextend --innodb_log_file_size=50331648 --innodb_page_size=16384 --innodb_undo_directory=.\/ --innodb_undo_tablespaces=2 --server-id=0 --innodb_log_checksums=ON --innodb_redo_log_encrypt=0 --innodb_undo_log_encrypt=0\n2022-12-14T17:12:27.172601-00:00 0 [Note] [MY-011825] [Xtrabackup] recognized client arguments: --prepare=1 --export=1 --target-dir=\/tmp\/restore\nxtrabackup version 8.0.30-23 based on MySQL server 8.0.30 Linux (x86_64) (revision id: 873b467185c)\n2022-12-14T17:12:27.172633-00:00 0 [Note] [MY-011825] [Xtrabackup] auto-enabling --innodb-file-per-table due to the --export option\n2022-12-14T17:12:27.172650-00:00 0 [Note] [MY-011825] [Xtrabackup] cd to \/tmp\/restore\/\n2022-12-14T17:12:27.172750-00:00 0 [Note] [MY-011825] [Xtrabackup] This target seems to be not prepared yet.\n2022-12-14T17:12:27.173185-00:00 0 [Note] [MY-013251] [InnoDB] Number of pools: 1\n2022-12-14T17:12:27.181337-00:00 0 [Note] [MY-011825] [Xtrabackup] xtrabackup_logfile detected: size=8388608, start_lsn=(19690717)\n2022-12-14T17:12:27.185980-00:00 0 [Note] [MY-011825] [Xtrabackup] using the following InnoDB configuration for recovery:\n2022-12-14T17:12:27.186001-00:00 0 [Note] [MY-011825] [Xtrabackup] innodb_data_home_dir = .\n2022-12-14T17:12:27.186011-00:00 0 [Note] [MY-011825] [Xtrabackup] innodb_data_file_path = ibdata1:12M:autoextend\n...\n2022-12-14T17:12:29.477906-00:00 0 [Note] [MY-013072] [InnoDB] Starting shutdown...\n2022-12-14T17:12:29.580573-00:00 0 [Note] [MY-013084] [InnoDB] Log background threads are being closed...\n2022-12-14T17:12:29.598559-00:00 0 [Note] [MY-012980] [InnoDB] Shutdown completed; log sequence number 19691030\n2022-12-14T17:12:29.599239-00:00 0 [Note] [MY-013251] [InnoDB] Number of pools: 1\n2022-12-14T17:12:29.600397-00:00 0 [Note] [MY-011825] [Xtrabackup] completed OK!<\/pre>\n\n\n\n<p><strong>Reviewing exported tablespace files<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@ip-172-31-83-17 ~]# cd \/tmp\/restore\/test\/<br>[root@ip-172-31-83-17 test]# ls -lhtr<br>total 116K<br>-rw-r--r--. 1 root root 112K Dec 14 17:12 t1.ibd<br>-rw-r--r--. 1 root root 706 Dec 14 17:12 t1.cfg<\/pre>\n\n\n\n<p><strong>Creating table, discarding empty tablespace and getting ready for restore<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; use test<br>Database changed<br>mysql&gt; create table t1 (<code>id<\/code> int unsigned NOT NULL AUTO_INCREMENT,<br>-&gt; <code>val<\/code> varchar(10) DEFAULT NULL,<br>-&gt; PRIMARY KEY (<code>id<\/code>)<br>-&gt; ) ENGINE=InnoDB;<br>Query OK, 0 rows affected (0.02 sec)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; select * from test.t1;\nEmpty set (0.00 sec)\nmysql&gt; alter table t1 discard tablespace;\nQuery OK, 0 rows affected (0.00 sec)<\/pre>\n\n\n\n<p><strong>Restoring tablespace files and importing tablespace<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; ! cp \/tmp\/restore\/test\/* \/var\/lib\/mysql\/test\/\nmysql&gt; ! ls -lhtr \/var\/lib\/mysql\/test\/\ntotal 116K\n-rw-r--r--. 1 root root 112K Dec 14 17:13 t1.ibd\n-rw-r--r--. 1 root root 706 Dec 14 17:13 t1.cfg\n\nmysql&gt; alter table t1 import tablespace;\nERROR 1815 (HY000): Internal error: Cannot reset LSNs in table <code>test<\/code>.<code>t1<\/code> : Tablespace not found\n\nmysql&gt; ! chown mysql:mysql \/var\/lib\/mysql\/test -R\nmysql&gt; alter table t1 import tablespace;\nQuery OK, 0 rows affected (0.04 sec)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; select * from test.t1;<br>+----+------+<br>| id | val |<br>+----+------+<br>| 1 | a |<br>| 2 | b |<br>| 3 | c |<br>+----+------+<br>3 rows in set (0.00 sec)<\/pre>\n\n\n\n<p>Alright, that&#8217;s it. We have successfully restored our table that was &#8220;accidentally&#8221;  dropped from the full Xtrabackup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Quick recipes  for Xtrabackup <\/h2>\n\n\n\n<p><strong>Installing Percona Xtrabackup<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install https:\/\/repo.percona.com\/yum\/percona-release-latest.noarch.rpm<br>yum install qpress<br>yum install percona-xtrabackup-80<\/pre>\n\n\n\n<p><strong>Creating Backup<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">xtrabackup --backup --compress --compress-threads=8 --stream=xbstream --parallel=4 --target-dir=\/root\/backupdir &gt; \/root\/backupdir\/backup.xbstream<\/pre>\n\n\n\n<p><strong>Restoring full backup<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">xbstream -x -p 4 -C \/var\/lib\/mysql\/ &lt; \/root\/backupdir\/backup.xbstream<br>xtrabackup --decompress --remove-original --parallel=8 --target-dir=\/var\/lib\/mysql\/<br>xtrabackup --prepare --target-dir=\/var\/lib\/mysql\/<br>chown mysql:mysql -R \/var\/lib\/mysql<\/pre>\n\n\n\n<p>I hope this helps.<\/p>\n\n\n\n<p>Update:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Extract Table Definition from IBD (datafile)<\/h2>\n\n\n\n<p>What if you have the ibd file but don&#8217;t have the table definition? In such scenarios you can use the ibd2sdi, the innodb tablespace extraction utility. It will extract the Serialized dictionary information (SDI) from InnoDB tablespace files. <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/ibd2sdi.html\" target=\"_blank\" rel=\"noopener nofollow\" title=\"\">Refer this<\/a>.<\/p>\n\n\n\n<p>I&#8217;d also like to highlight the tool created by Marcelo here: <a href=\"https:\/\/github.com\/altmannmarcelo\/sdi2ddl\" target=\"_blank\" rel=\"noopener nofollow\" title=\"\">https:\/\/github.com\/altmannmarcelo\/sdi2ddl<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"In this blog we will create MySQL backup and then restore single table from Percona Xtrabackup without doing full restore. This post is basically a work-log in response to a&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":[627,427,366,625,530,516,626,628,369],"class_list":{"0":"post-2674","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-articles","8":"tag-idb2sdi","9":"tag-mysql","10":"tag-restore","11":"tag-restore-definition-from-ibd","12":"tag-restore-ibd","13":"tag-restore-single-table","14":"tag-restore-without-table-definition","15":"tag-sdi2ddl","16":"tag-xtrabackup"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2674","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=2674"}],"version-history":[{"count":8,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2674\/revisions"}],"predecessor-version":[{"id":2951,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2674\/revisions\/2951"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=2674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=2674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=2674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}