{"id":95049,"date":"2022-11-16T19:54:36","date_gmt":"2022-11-16T19:54:36","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=95049"},"modified":"2022-11-16T19:54:36","modified_gmt":"2022-11-16T19:54:36","slug":"backing-up-mysql-part-2-percona-xtrabackup","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/backing-up-mysql-part-2-percona-xtrabackup\/","title":{"rendered":"Backing Up MySQL Part 2: Percona XtraBackup"},"content":{"rendered":"<p><span style=\"font-weight: 400\">There\u2019s no doubt about it &#8211; if you\u2019ve ever heard of Percona, you\u2019ve heard of XtraBackup as well. XtraBackup is one of the primary Percona\u2019s offerings in the backup space: the tool is famous amongst DBAs as one of the primary open-source utilities to take hot backups. The tool is known to avoid locking databases during its backup procedures &#8211; of course, it has a couple of limitations unique to itself, but that\u2019s not an obstacle for experienced database administrators across the globe: the tool is loved by pretty much everyone who uses it, and with Percona at its side, we all know that the tool is going to deliver awesomeness by day and by night.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">What is Percona XtraBackup?<\/span><\/h2>\n<p><span style=\"font-weight: 400\">As already stated above, Percona XtraBackup is one of the primary offerings for MySQL &amp; Percona database administrators developed by Percona. The tool is an open-source backup utility that does not lock our databases during the backup processes it performs. Percona says that their tool can provide automatic verification of backups that have been taken, offer fast dumping and restore times, and above all, it\u2019s supported by their award-winning consulting services helping us make sure that our data and its backups are in safe hands by day and by night.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">How To Use Percona XtraBackup?<\/span><\/h2>\n<p><span style=\"font-weight: 400\">In order to start exploring the features offered by Percona\u2019s XtraBackup, please install the tool before proceeding any further &#8211; the tool can be installed from a repo, from a tarball, from packages, or via source code &#8211; the old-fashioned way of installing the tool from a repository is a favourite option for many. To install the tool from a repo, head over to <\/span><a href=\"https:\/\/docs.percona.com\/percona-xtrabackup\/2.4\/installation\/yum_repo.html\"><span style=\"font-weight: 400\">Percona\u2019s documentation<\/span><\/a><span style=\"font-weight: 400\">, and once you\u2019re done installing, you can backup your data by issuing the command below (the command below will perform a full backup of your database, and then store the backup in a directory named \u201cbackups\u201d):<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup --backup --target-dir=\/backups<\/code><\/span><\/p>\n<p><span style=\"font-weight: 400\">Bear in mind that in this case, contrary to <\/span><span style=\"font-weight: 400\">mysqldump<\/span><span style=\"font-weight: 400\">, the \u201cbackups\u201d directory won\u2019t consist of only one file &#8211; Percona\u2019s XtraBackup would backup the following:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The my.cnf file that consists of the most vital information for MySQL to function correctly (XtraBackup will take a backup of the file and name it <\/span><span style=\"font-weight: 400\">backup-my.cnf<\/span><span style=\"font-weight: 400\">)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The file vital for InnoDB to function correctly &#8211; <\/span><span style=\"font-weight: 400\">ibdata1<\/span><span style=\"font-weight: 400\">. Since <\/span><span style=\"font-weight: 400\">ibdata1<\/span><span style=\"font-weight: 400\"> holds data, indexes, Multiversion Concurrency Control (MVCC) data, and double write &amp; insert buffers that are necessary for InnoDB to work in the way it does, without it InnoDB\u2019s infrastructure would plummet to ashes.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">All databases inside of your MySQL infrastructure including the <\/span><span style=\"font-weight: 400\">test<\/span><span style=\"font-weight: 400\"> and <\/span><span style=\"font-weight: 400\">performance_schema<\/span><span style=\"font-weight: 400\"> databases.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Incremental backups can also be made in a very similar fashion: first, take a full backup using the command provided above, then issue a very similar statement, just add a <\/span><span style=\"font-weight: 400\"><code>--incremental-basedir<\/code><\/span><span style=\"font-weight: 400\"> statement at the end of it like so:<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup --backup --target-dir=\/backups <\/code><\/span><b>&#8211;incremental-basedir=\/incbackups<\/b><\/p>\n<p><span style=\"font-weight: 400\">In order to take a compressed backup, add the <\/span><span style=\"font-weight: 400\"><code>--compress<\/code><\/span><span style=\"font-weight: 400\"> option, and for partial backups, use one or more of the following options:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Use the <\/span><span style=\"font-weight: 400\"><code>--databases<\/code><\/span><span style=\"font-weight: 400\"> or <\/span><span style=\"font-weight: 400\"><code>--databases-file<\/code><\/span><span style=\"font-weight: 400\"> options to back up a database or a list of databases from a file:<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup --databases=\u201ddb1 db2 test_db demo_db\u201d<\/code><\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup --databases-file=databases.txt*<\/code><\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">* The file <\/span><span style=\"font-weight: 400\">databases.txt<\/span><span style=\"font-weight: 400\"> would need to contain databases and tables in the format of <\/span><span style=\"font-weight: 400\">database.table<\/span><span style=\"font-weight: 400\">.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Use the <\/span><span style=\"font-weight: 400\"><code>--tables<\/code><\/span><span style=\"font-weight: 400\"> or <\/span><span style=\"font-weight: 400\"><code>--tables-file<\/code><\/span><span style=\"font-weight: 400\"> options to back up a table or a list of tables in the same fashion you would back up databases.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Woohoo &#8211; you\u2019ve now learned how to take backups using Percona XtraBackup! That\u2019s not everything, though &#8211; when using Percona XtraBackup you would also find yourself needing to prepare your backups for them to be successfully restored: we will tell you how to do that in the next section.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Preparing XtraBackups<\/span><\/h2>\n<p><span style=\"font-weight: 400\">As you could have noticed, Percona XtraBackup doesn\u2019t take backups in a simple <\/span><span style=\"font-weight: 400\">backup.sql<\/span><span style=\"font-weight: 400\"> form you might be used to when using <\/span><span style=\"font-weight: 400\"><code>mysqldump<\/code><\/span><span style=\"font-weight: 400\"> or other database backup tools &#8211; instead, Percona XtraBackup often takes a backup of the files associated with the database with itself too (we have covered those in the previous chapter.)<\/span><\/p>\n<p><span style=\"font-weight: 400\">That\u2019s why all of the backups taken using Percona XtraBackup need to be prepared for recovery before they can be successfully recovered &#8211; here\u2019s how to do that for each flavour of backups:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">To prepare a full or an encrypted backup to be restored, run the following command:<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup --prepare --target-dir=backups\/<\/code><\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">To prepare an incremental backup to be recovered, you would need to ensure that the rollback phase will be skipped by specifying an <\/span><span style=\"font-weight: 400\"><code>--apply-log-only<\/code><\/span><span style=\"font-weight: 400\"> option. Percona themselves state that <\/span><i><span style=\"font-weight: 400\">if the rollback phase isn\u2019t prevented, the incremental backups would be worth nothing and you would have to start over<\/span><\/i><span style=\"font-weight: 400\">, so keep that in mind. To prepare an incremental backup, run the same command as with full backups, just with the <\/span><span style=\"font-weight: 400\"><code>--apply-log-only<\/code><\/span><span style=\"font-weight: 400\"> option, nothing complex here:<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup <\/code><\/span><b>&#8211;apply-log-only<\/b><span style=\"font-weight: 400\"> &#8211;prepare &#8211;target-dir=backups\/<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">To prepare a partial backup, specify the <\/span><span style=\"font-weight: 400\"><code>--export<\/code><\/span><span style=\"font-weight: 400\"> tag (don\u2019t worry about warnings in this case &#8211; they are most likely issued because InnoDB \u201csees\u201d tables, but their files do not exist in the data directory):<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup --prepare --export --target-dir=backups\/<\/code><\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">And.. you\u2019re done! Well, kind of. Now you also need to restore the backups, right?<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Restoring XtraBackups<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Now that you\u2019ve made and prepared your backups, there will obviously be a point in time where you would need to restore what you\u2019ve got in store. Again, such a process is a little different for each backup type, but don\u2019t fret &#8211; we\u2019re here to help. Here\u2019s how to come around this issue:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">To restore full, compressed, incremental, or encrypted backups, add a <\/span><span style=\"font-weight: 400\"><code>--copy-back<\/code><\/span><span style=\"font-weight: 400\"> option to restore your backup to the data directory:<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><code>xtrabackup --copy-back --target-dir=\/backups<\/code><\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">To restore partial backups, all you have to do is restore all of the tables in the partial backup (copy them back to the server of your choice.)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">That\u2019s it &#8211; it\u2019s that simple! Of course, we can run into a couple of issues during these steps as well, so it\u2019s always beneficial to keep an eye out for <\/span><a href=\"https:\/\/docs.percona.com\/percona-xtrabackup\/8.0\/index.html#backup-scenarios\"><span style=\"font-weight: 400\">the documentation<\/span><\/a><span style=\"font-weight: 400\">.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Options Offered by Percona XtraBackup<\/span><\/h2>\n<p><span style=\"font-weight: 400\">As with everything command-line related, Percona XtraBackup has a couple of options associated with itself. Some of them are as follows (all of the options can be found over at <\/span><a href=\"https:\/\/www.percona.com\/doc\/percona-xtrabackup\/2.3\/xtrabackup_bin\/xbk_option_reference.html\"><span style=\"font-weight: 400\">the Percona\u2019s documentation<\/span><\/a><span style=\"font-weight: 400\">):<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p><b>Percona XtraBackup Option<\/b><\/p>\n<\/td>\n<td>\n<p><b>Meaning<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400\"><code>--backup<\/code><\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400\">Takes a backup of the database.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400\"><code>--check-privileges<\/code><\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400\">Checks if Percona XtraBackup has all of the required privileges to be operating properly.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400\"><code>--apply-log-only<\/code><\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400\">Prepares to take incremental backups by ignoring all stages except the redo stage.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400\"><code>--copy-back<\/code><\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400\">Restores a backup. This option is meant to be used in conjunction with other options (see examples above.)<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400\"><code>--databases=x<\/code> | <code>--tables=x<\/code><\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400\">\u201cx\u201d specifies a database or a table inside of a database to be backed up. These options are similar to <\/span><span style=\"font-weight: 400\"><code>--databases-file<\/code><\/span><span style=\"font-weight: 400\"> or <\/span><span style=\"font-weight: 400\"><code>--tables-file<\/code><\/span><span style=\"font-weight: 400\"> options that backs up databases or tables from a file.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400\"><code>--defaults-file<\/code><\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400\">Makes XtraBackup read only the options specified in the file after this parameter (the file will most likely be my.cnf.)<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"font-weight: 400\">Summary<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Percona XtraBackup is the flagship tool in Percona\u2019s backup arsenal &#8211; the tool is widely used by junior and senior database administrators alike and as it avoids the locking of databases during its backup procedures, takes backups in a quick and safe fashion, and allows all kinds of backups to be restored quickly, there\u2019s no doubt that Percona XtraBackup will be the option of choice for database engineers for many years to come. We hope that this blog post has provided you with some of the insight into the Percona\u2019s XtraBackup world and that you will <\/span><a href=\"https:\/\/docs.percona.com\/percona-xtrabackup\/2.4\/manual.html\"><span style=\"font-weight: 400\">refer to the Percona\u2019s manual for more information<\/span><\/a><span style=\"font-weight: 400\">, and we will see you in the next blog!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we walk you through one of the flagship backup offerings by Percona &#8211; XtraBackup.&hellip;<\/p>\n","protected":false},"author":339547,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5564,4168,5854,147593,147594],"coauthors":[146040],"class_list":["post-95049","post","type-post","status-publish","format-standard","hentry","category-blogs","tag-backups","tag-database","tag-mysql","tag-percona","tag-xtrabackup"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/95049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/339547"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=95049"}],"version-history":[{"count":6,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/95049\/revisions"}],"predecessor-version":[{"id":95058,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/95049\/revisions\/95058"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=95049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=95049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=95049"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=95049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}