2023-12-03 14:09:43 +02:00
|
|
|
/* Copyright (c) 2017, MariaDB Corporation.
|
2017-06-19 06:34:38 +03:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
|
|
|
|
|
2017-08-21 16:57:08 +02:00
|
|
|
#include <my_global.h>
|
2017-04-18 19:05:57 +00:00
|
|
|
#include <mysqld.h>
|
|
|
|
#include <mysql.h>
|
|
|
|
#include <xtrabackup.h>
|
2023-12-03 14:09:43 +02:00
|
|
|
#include <encryption_plugin.h>
|
2017-04-18 19:05:57 +00:00
|
|
|
#include <sql_plugin.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <common.h>
|
|
|
|
#include <backup_mysql.h>
|
2023-12-03 14:09:43 +02:00
|
|
|
#include <log0crypt.h>
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
extern struct st_maria_plugin *mysql_optional_plugins[];
|
|
|
|
extern struct st_maria_plugin *mysql_mandatory_plugins[];
|
2023-12-03 14:09:43 +02:00
|
|
|
static void encryption_plugin_init(int argc, char **argv);
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
extern char *xb_plugin_load;
|
|
|
|
extern char *xb_plugin_dir;
|
|
|
|
|
|
|
|
const int PLUGIN_MAX_ARGS = 1024;
|
MDEV-12548 Initial implementation of Mariabackup for MariaDB 10.2
InnoDB I/O and buffer pool interfaces and the redo log format
have been changed between MariaDB 10.1 and 10.2, and the backup
code has to be adjusted accordingly.
The code has been simplified, and many memory leaks have been fixed.
Instead of the file name xtrabackup_logfile, the file name ib_logfile0
is being used for the copy of the redo log. Unnecessary InnoDB startup and
shutdown and some unnecessary threads have been removed.
Some help was provided by Vladislav Vaintroub.
Parameters have been cleaned up and aligned with those of MariaDB 10.2.
The --dbug option has been added, so that in debug builds,
--dbug=d,ib_log can be specified to enable diagnostic messages
for processing redo log entries.
By default, innodb_doublewrite=OFF, so that --prepare works faster.
If more crash-safety for --prepare is needed, double buffering
can be enabled.
The parameter innodb_log_checksums=OFF can be used to ignore redo log
checksums in --backup.
Some messages have been cleaned up.
Unless --export is specified, Mariabackup will not deal with undo log.
The InnoDB mini-transaction redo log is not only about user-level
transactions; it is actually about mini-transactions. To avoid confusion,
call it the redo log, not transaction log.
We disable any undo log processing in --prepare.
Because MariaDB 10.2 supports indexed virtual columns, the
undo log processing would need to be able to evaluate virtual column
expressions. To reduce the amount of code dependencies, we will not
process any undo log in prepare.
This means that the --export option must be disabled for now.
This also means that the following options are redundant
and have been removed:
xtrabackup --apply-log-only
innobackupex --redo-only
In addition to disabling any undo log processing, we will disable any
further changes to data pages during --prepare, including the change
buffer merge. This means that restoring incremental backups should
reliably work even when change buffering is being used on the server.
Because of this, preparing a backup will not generate any further
redo log, and the redo log file can be safely deleted. (If the
--export option is enabled in the future, it must generate redo log
when processing undo logs and buffered changes.)
In --prepare, we cannot easily know if a partial backup was used,
especially when restoring a series of incremental backups. So, we
simply warn about any missing files, and ignore the redo log for them.
FIXME: Enable the --export option.
FIXME: Improve the handling of the MLOG_INDEX_LOAD record, and write
a test that initiates a backup while an ALGORITHM=INPLACE operation
is creating indexes or rebuilding a table. An error should be detected
when preparing the backup.
FIXME: In --incremental --prepare, xtrabackup_apply_delta() should
ensure that if FSP_SIZE is modified, the file size will be adjusted
accordingly.
2017-06-30 10:49:37 +03:00
|
|
|
std::vector<std::string> backup_plugins_args;
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
const char *QUERY_PLUGIN =
|
|
|
|
"SELECT plugin_name, plugin_library, @@plugin_dir"
|
|
|
|
" FROM information_schema.plugins WHERE plugin_type='ENCRYPTION'"
|
2021-10-08 09:48:31 +02:00
|
|
|
" OR (plugin_type = 'DAEMON' AND plugin_name LIKE 'provider\\_%')"
|
2017-04-18 19:05:57 +00:00
|
|
|
" AND plugin_status='ACTIVE'";
|
|
|
|
|
2023-12-03 14:09:43 +02:00
|
|
|
std::string encryption_plugin_config;
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
static void add_to_plugin_load_list(const char *plugin_def)
|
|
|
|
{
|
|
|
|
opt_plugin_load_list_ptr->push_back(new i_string(plugin_def));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char XTRABACKUP_EXE[] = "xtrabackup";
|
|
|
|
|
2018-09-07 18:18:14 +01:00
|
|
|
/*
|
2023-12-03 14:09:43 +02:00
|
|
|
Read "plugin-load" value (encryption plugin) from backup-my.cnf during
|
|
|
|
prepare phase.
|
2018-09-07 18:18:14 +01:00
|
|
|
The value is stored during backup phase.
|
|
|
|
*/
|
2023-12-03 14:09:43 +02:00
|
|
|
static std::string get_encryption_plugin_from_cnf()
|
2018-09-07 18:18:14 +01:00
|
|
|
{
|
2023-12-03 14:09:43 +02:00
|
|
|
FILE *f = fopen("backup-my.cnf", "r");
|
2018-09-07 18:18:14 +01:00
|
|
|
if (!f)
|
|
|
|
{
|
2023-12-03 14:09:43 +02:00
|
|
|
die("Can't open backup-my.cnf for reading");
|
2018-09-07 18:18:14 +01:00
|
|
|
}
|
|
|
|
char line[512];
|
|
|
|
std::string plugin_load;
|
|
|
|
while (fgets(line, sizeof(line), f))
|
|
|
|
{
|
|
|
|
if (strncmp(line, "plugin_load=", 12) == 0)
|
|
|
|
{
|
|
|
|
plugin_load = line + 12;
|
|
|
|
// remote \n at the end of string
|
|
|
|
plugin_load.resize(plugin_load.size() - 1);
|
2024-04-24 13:13:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(line, "innodb_encrypt_tables=", 22) == 0)
|
|
|
|
{
|
|
|
|
if (!strncmp(line + 22, "ON", 2) ||
|
|
|
|
!strncmp(line + 22, "1", 1))
|
|
|
|
srv_encrypt_tables= 1;
|
|
|
|
else if (!strncmp(line + 22, "FORCE", 5) ||
|
|
|
|
!strncmp(line + 22, "2", 1))
|
|
|
|
srv_encrypt_tables= 2;
|
2018-09-07 18:18:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return plugin_load;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-03 14:09:43 +02:00
|
|
|
void encryption_plugin_backup_init(MYSQL *mysql)
|
2017-04-18 19:05:57 +00:00
|
|
|
{
|
|
|
|
MYSQL_RES *result;
|
|
|
|
MYSQL_ROW row;
|
MDEV-12548 Initial implementation of Mariabackup for MariaDB 10.2
InnoDB I/O and buffer pool interfaces and the redo log format
have been changed between MariaDB 10.1 and 10.2, and the backup
code has to be adjusted accordingly.
The code has been simplified, and many memory leaks have been fixed.
Instead of the file name xtrabackup_logfile, the file name ib_logfile0
is being used for the copy of the redo log. Unnecessary InnoDB startup and
shutdown and some unnecessary threads have been removed.
Some help was provided by Vladislav Vaintroub.
Parameters have been cleaned up and aligned with those of MariaDB 10.2.
The --dbug option has been added, so that in debug builds,
--dbug=d,ib_log can be specified to enable diagnostic messages
for processing redo log entries.
By default, innodb_doublewrite=OFF, so that --prepare works faster.
If more crash-safety for --prepare is needed, double buffering
can be enabled.
The parameter innodb_log_checksums=OFF can be used to ignore redo log
checksums in --backup.
Some messages have been cleaned up.
Unless --export is specified, Mariabackup will not deal with undo log.
The InnoDB mini-transaction redo log is not only about user-level
transactions; it is actually about mini-transactions. To avoid confusion,
call it the redo log, not transaction log.
We disable any undo log processing in --prepare.
Because MariaDB 10.2 supports indexed virtual columns, the
undo log processing would need to be able to evaluate virtual column
expressions. To reduce the amount of code dependencies, we will not
process any undo log in prepare.
This means that the --export option must be disabled for now.
This also means that the following options are redundant
and have been removed:
xtrabackup --apply-log-only
innobackupex --redo-only
In addition to disabling any undo log processing, we will disable any
further changes to data pages during --prepare, including the change
buffer merge. This means that restoring incremental backups should
reliably work even when change buffering is being used on the server.
Because of this, preparing a backup will not generate any further
redo log, and the redo log file can be safely deleted. (If the
--export option is enabled in the future, it must generate redo log
when processing undo logs and buffered changes.)
In --prepare, we cannot easily know if a partial backup was used,
especially when restoring a series of incremental backups. So, we
simply warn about any missing files, and ignore the redo log for them.
FIXME: Enable the --export option.
FIXME: Improve the handling of the MLOG_INDEX_LOAD record, and write
a test that initiates a backup while an ALGORITHM=INPLACE operation
is creating indexes or rebuilding a table. An error should be detected
when preparing the backup.
FIXME: In --incremental --prepare, xtrabackup_apply_delta() should
ensure that if FSP_SIZE is modified, the file size will be adjusted
accordingly.
2017-06-30 10:49:37 +03:00
|
|
|
std::ostringstream oss;
|
2017-04-18 19:05:57 +00:00
|
|
|
char *argv[PLUGIN_MAX_ARGS];
|
2021-10-08 09:48:31 +02:00
|
|
|
char show_query[1024] = "";
|
|
|
|
std::string plugin_load;
|
2017-04-18 19:05:57 +00:00
|
|
|
int argc;
|
|
|
|
|
|
|
|
result = xb_mysql_query(mysql, QUERY_PLUGIN, true, true);
|
2021-10-08 09:48:31 +02:00
|
|
|
while ((row = mysql_fetch_row(result)))
|
2017-04-18 19:05:57 +00:00
|
|
|
{
|
2021-10-08 09:48:31 +02:00
|
|
|
char *name= row[0];
|
|
|
|
char *library= row[1];
|
|
|
|
char *dir= row[2];
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
if (!plugin_load.length())
|
|
|
|
{
|
2017-04-18 19:05:57 +00:00
|
|
|
#ifdef _WIN32
|
2021-10-08 09:48:31 +02:00
|
|
|
for (char *p = dir; *p; p++)
|
|
|
|
if (*p == '\\') *p = '/';
|
2017-04-18 19:05:57 +00:00
|
|
|
#endif
|
2021-10-08 09:48:31 +02:00
|
|
|
strncpy(opt_plugin_dir, dir, FN_REFLEN - 1);
|
|
|
|
opt_plugin_dir[FN_REFLEN - 1] = '\0';
|
|
|
|
oss << "plugin_dir=" << '"' << dir << '"' << std::endl;
|
|
|
|
}
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
plugin_load += std::string(";") + name;
|
|
|
|
|
|
|
|
if (library)
|
2018-09-07 18:18:14 +01:00
|
|
|
{
|
2021-10-08 09:48:31 +02:00
|
|
|
/* Remove shared library suffixes, in case we'll prepare on different OS.*/
|
|
|
|
const char *extensions[] = { ".dll", ".so", 0 };
|
|
|
|
for (size_t i = 0; extensions[i]; i++)
|
|
|
|
{
|
|
|
|
const char *ext = extensions[i];
|
|
|
|
if (ends_with(library, ext))
|
|
|
|
library[strlen(library) - strlen(ext)] = 0;
|
|
|
|
}
|
|
|
|
plugin_load += std::string("=") + library;
|
2018-09-07 18:18:14 +01:00
|
|
|
}
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
if (strncmp(name, "provider_", 9) == 0)
|
|
|
|
continue;
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
/* Read plugin variables. */
|
|
|
|
snprintf(show_query, sizeof(show_query), "SHOW variables like '%s_%%'", name);
|
|
|
|
}
|
|
|
|
mysql_free_result(result);
|
|
|
|
if (!plugin_load.length())
|
|
|
|
return;
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
oss << "plugin_load=" << plugin_load.c_str() + 1 << std::endl;
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
/* Required to load the plugin later.*/
|
|
|
|
add_to_plugin_load_list(plugin_load.c_str() + 1);
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
if (*show_query)
|
2017-04-18 19:05:57 +00:00
|
|
|
{
|
2021-10-08 09:48:31 +02:00
|
|
|
result = xb_mysql_query(mysql, show_query, true, true);
|
|
|
|
while ((row = mysql_fetch_row(result)))
|
|
|
|
{
|
|
|
|
std::string arg("--");
|
|
|
|
arg += row[0];
|
|
|
|
arg += "=";
|
|
|
|
arg += row[1];
|
|
|
|
backup_plugins_args.push_back(arg);
|
|
|
|
oss << row[0] << "=" << row[1] << std::endl;
|
|
|
|
}
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
mysql_free_result(result);
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
/* Check whether to encrypt logs. */
|
|
|
|
result = xb_mysql_query(mysql, "select @@innodb_encrypt_log", true, true);
|
|
|
|
row = mysql_fetch_row(result);
|
|
|
|
srv_encrypt_log = (row != 0 && row[0][0] == '1');
|
|
|
|
oss << "innodb_encrypt_log=" << row[0] << std::endl;
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
mysql_free_result(result);
|
|
|
|
}
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2024-04-24 13:13:57 +05:30
|
|
|
result = xb_mysql_query(mysql, "select @@innodb_encrypt_tables", true, true);
|
|
|
|
row = mysql_fetch_row(result);
|
|
|
|
if (!row);
|
|
|
|
else if (const char *r= row[0])
|
|
|
|
{
|
|
|
|
if (!strcmp(r, "ON")) srv_encrypt_tables= 1;
|
|
|
|
else if (!strcmp(r, "FORCE")) srv_encrypt_tables= 2;
|
|
|
|
oss << "innodb_encrypt_tables=" << r << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql_free_result(result);
|
2023-12-03 14:09:43 +02:00
|
|
|
encryption_plugin_config = oss.str();
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
argc = 0;
|
|
|
|
argv[argc++] = XTRABACKUP_EXE;
|
|
|
|
for(size_t i = 0; i < backup_plugins_args.size(); i++)
|
|
|
|
{
|
|
|
|
argv[argc++] = (char *)backup_plugins_args[i].c_str();
|
|
|
|
if (argc == PLUGIN_MAX_ARGS - 2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
argv[argc] = 0;
|
|
|
|
|
2023-12-03 14:09:43 +02:00
|
|
|
encryption_plugin_init(argc, argv);
|
2017-04-18 19:05:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-03 14:09:43 +02:00
|
|
|
const char *encryption_plugin_get_config()
|
2017-04-18 19:05:57 +00:00
|
|
|
{
|
2023-12-03 14:09:43 +02:00
|
|
|
return encryption_plugin_config.c_str();
|
2017-04-18 19:05:57 +00:00
|
|
|
}
|
|
|
|
|
2024-10-26 13:53:51 -06:00
|
|
|
extern int finalize_encryption_plugin(void *plugin);
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
|
2023-12-03 14:09:43 +02:00
|
|
|
void encryption_plugin_prepare_init(int argc, char **argv)
|
2017-04-18 19:05:57 +00:00
|
|
|
{
|
2023-12-03 14:09:43 +02:00
|
|
|
std::string plugin_load= get_encryption_plugin_from_cnf();
|
2018-09-07 18:18:14 +01:00
|
|
|
if (plugin_load.size())
|
|
|
|
{
|
2023-12-03 14:09:43 +02:00
|
|
|
msg("Loading encryption plugin from %s", plugin_load.c_str());
|
2018-09-07 18:18:14 +01:00
|
|
|
}
|
|
|
|
else
|
2017-04-18 19:05:57 +00:00
|
|
|
{
|
|
|
|
finalize_encryption_plugin(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-07 18:18:14 +01:00
|
|
|
add_to_plugin_load_list(plugin_load.c_str());
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
if (xb_plugin_dir)
|
2019-03-06 11:22:27 +02:00
|
|
|
{
|
|
|
|
strncpy(opt_plugin_dir, xb_plugin_dir, FN_REFLEN - 1);
|
|
|
|
opt_plugin_dir[FN_REFLEN - 1] = '\0';
|
|
|
|
}
|
2017-04-18 19:05:57 +00:00
|
|
|
|
2021-10-08 09:48:31 +02:00
|
|
|
char **new_argv = new char *[argc + 2];
|
2017-04-18 19:05:57 +00:00
|
|
|
new_argv[0] = XTRABACKUP_EXE;
|
|
|
|
memcpy(&new_argv[1], argv, argc*sizeof(char *));
|
|
|
|
|
2023-12-03 14:09:43 +02:00
|
|
|
encryption_plugin_init(argc+1, new_argv);
|
2017-04-18 19:05:57 +00:00
|
|
|
|
|
|
|
delete[] new_argv;
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:09:43 +02:00
|
|
|
static void encryption_plugin_init(int argc, char **argv)
|
2017-04-18 19:05:57 +00:00
|
|
|
{
|
|
|
|
/* Patch optional and mandatory plugins, we only need to load the one in xb_plugin_load. */
|
|
|
|
mysql_optional_plugins[0] = mysql_mandatory_plugins[0] = 0;
|
2017-11-24 21:56:13 +00:00
|
|
|
plugin_maturity = MariaDB_PLUGIN_MATURITY_UNKNOWN; /* mariabackup accepts all plugins */
|
2023-12-03 14:09:43 +02:00
|
|
|
msg("Loading encryption plugin");
|
2017-04-18 19:05:57 +00:00
|
|
|
for (int i= 1; i < argc; i++)
|
2023-12-03 14:09:43 +02:00
|
|
|
msg("\t Encryption plugin parameter : '%s'", argv[i]);
|
2017-04-18 19:05:57 +00:00
|
|
|
plugin_init(&argc, argv, PLUGIN_INIT_SKIP_PLUGIN_TABLE);
|
|
|
|
}
|
|
|
|
|