📜 ⬆️ ⬇️

Smart backup MySQL

I think many people in life have a situation where there is a full backup of the MySQL database, and you need to restore only a few tables from it. Or, even worse, you need to restore the old entries in these tables, without overwriting the new ones. And if the dump takes hundreds of megabytes, the process is not very inspiring.

In this article I will describe the concept of smart backup, implemented in Sypex Dumper. And also give examples of use.

Introduction


During the development of Sypex Dumper 2, many solutions for MySQL backup were analyzed. And in the first place, of course, the source of the complete mysqldump.

In mysqldump, a file is created with a set of SQL queries that, when restored, are simply split into a separator and “fed” by MySQL. Such a scheme, in principle, has been working for a long time and, in fact, it has become a standard. But this scheme has the following disadvantages:
  1. All settings are made in mysqldump, which is forced to spend extra time checking all these options (some options are checked for each data row).
  2. One of the main shortcomings follows from the first point - if you need to restore a dump with other options, then you need to re-do a backup with these options or manually “tinker” in the dump.
  3. Despite the many settings in mysqldump, due to its console, complicated or even difficult selection of objects for backup is not possible (for example, so that tables with a cache are not dumped, and only their structure got into the dump).
  4. Mysqldump works with each object independently, which is why additional garbage often appears in the dump (for example, enabling / disabling indexes for empty tables, adding fake tables because of the inability to arrange Views in the correct order).
  5. When restoring a program is used, which is designed, not for dumps, but for any SQL queries, because of this, time is spent on more thorough parsing.
  6. The problem of all imported software is clumsy work with encodings. This is especially felt in runet.

In general, I decided to experiment with the new concept of backup.
')

Smart backup in theory


The following principles were identified:
  1. The dump is done in a format containing only the necessary minimum information.
  2. Minimum backup options for maximum speed.
  3. All auxiliary SQL queries (DROP TABLE, etc.) are removed from the dump and added automatically during the recovery process.
  4. Adding advanced settings when restoring dump.
  5. SQL parser sharpened solely on dumps, and able to break long INSERT queries into shorter ones.
  6. Adding meta information to the file itself.
  7. Automatic and most importantly correct work with encodings.

And then - the matter of technology. This concept is implemented in Sypex Dumper 2. A special dump format was also developed, which uses separators and labels with special characters. This has significantly accelerated the parsing of the SQL file.

For example, the parsing speed was checked on a RAM disk, the 860 MB file was swallowed in 0.5 seconds, and phpMyAdmin took tens of minutes to a similar file (queries were turned off in both cases).

Smart backup in practice


One of the nice features of a smart backup is the ability to restore individual tables (and other objects) from a dump. Moreover, they can be restored in different ways. The simplest, classic recovery is when a table is deleted, then a new one is created and data is uploaded, but more complex variants are possible.

For example, if part of the data in a table is deleted or changed, and they need to be restored, but new rows have been added to the table since the last backup, and you need to prevent them from being affected. In this case, it is sufficient to select the desired table and the REPLACE recovery mode in the dumper. As a result, the dumper will restore only those lines that are in the dump, and the new lines will remain intact. If you only need to restore deleted rows, you can use the INSERT IGNORE mode.



Also, the dumper can restore the data to the table in which the structure was changed (added columns or changed their order). In all recovery modes, it is checked whether the table exists, and if it does not exist, it will be automatically created.

Another useful feature is the ability to restore tables with the replacement of the prefix in the titles.
All recovery settings (as well as backup) can be saved, and then performed in a couple of clicks or by cron (convenient for demo sites).

Encodings


I will devote a separate paragraph to encodings, since quite a few people handle problems.
Sypex Dumper has quite advanced functions for working with encodings. It quietly automatically “digests” tables with different encodings in the same dump, without converting all the data into UTF-8. It also provides functions for the correction of encodings.

It is surprising that a lot of software, including well-known commercial scripts, still do not really know how to work with encodings (for example, the same vBulletin 4). Yes, they make tables in UTF-8 encoding, they make encoding in UTF-8 headers and templates, but they forget to set the connection encoding to MySQL to UTF-8. As a result, MySQL thinks that the data came in latin1, and tries to convert them from latin1 to UTF-8. And the worst thing about such “jambs” is that you won't notice them right away, since the forum looks fine, except that searching and sorting by text do not work very well.

With the help of the dumper you can solve these problems quite easily. But, this is a topic for a separate article.

Bonus


As a bonus for the fastest multiple promo codes for getting free Sypex Dumper Pro licenses:

SX-1ADN-ZXJN
SX-ZIPE-4K4G
SX-TXFE-ZZUF
SX-TGGS-TR8F
SX-OJUS-A9YL
SX-RGL5-HROR
SX-L9A5-BK2R
SX-MNOW-ZOSP
SX-FLFJ-GWIV
SX-6B3I-WMWU
SX-SVZY-TG1K
SX-AUOR-1FGD
SX-VJEQ-9FV5
SX-YTHD-OE9P
SX-LXXB-PRBQ
SX-LRYB-3IHV
SX-C89A-UPPI
SX-PXOI-3SFN
SX-EUF2-IQWH
SX-RWJA-JCZ8
SX-Y4BS-C038
SX-SSFS-KYV0
SX-FUJ0-7AZY
SX-GHLR-8N1Z
SX-PK15-HRIE
SX-I3HJ-9JYR
SX-JPKK-WLBP
SX-6RNH-XY3Q
SX-FV4W-WBKV
SX-7DK9-L9BP
SX-UFNH-MWDG
SX-V2Q8-NI6H
SX-PQT8-TLLN
SX-VSYE-NZOM
SX-0YM6-PGFS
SX-L0TD-WILK
SX-7X9P-LT04
SX-8LOQ-YWQC

Yeah, I didn’t expect that in the evening the first ten codes would fly away in 9 minutes, so I’ll post it in portions of 4 each.

And the last four
SX-IUPV-UUSO
SX-JBBN-9MD9
SX-PFCT-QQ4F
SX-DZZN-9DT1

Unsubscribe from the codes taken, later I will add more.

Source: https://habr.com/ru/post/131797/


All Articles