Skip to content

Commit

Permalink
Backup History: save to remote server for backup in RO replica
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Miguel Biete Banon committed Apr 11, 2024
1 parent fa1f613 commit 90cd062
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
65 changes: 61 additions & 4 deletions storage/innobase/xtrabackup/src/backup_mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,50 @@ MYSQL *xb_mysql_connect() {
return (connection);
}

MYSQL *xb_mysql_history_connect() {
MYSQL *connection = mysql_init(NULL);
char mysql_port_str[std::numeric_limits<int>::digits10 + 3];
char mysql_history_port_str[std::numeric_limits<int>::digits10 + 3];

sprintf(mysql_port_str, "%d", opt_port);
sprintf(mysql_history_port_str, "%d", opt_history_port);

if (connection == NULL) {
xb::error() << "Failed to init MySQL struct: " << mysql_error(connection);
return (NULL);
}

xb::info() << "Connecting to MySQL server host for History: "
<< (opt_history_host ? opt_history_host : (opt_host ? opt_host : "localhost"))
<< ", user: " << (opt_history_user ? opt_history_user : (opt_user ? opt_user : "not set"))
<< ", password: " << (opt_history_password ? opt_history_password : (opt_password ? "set" : "not set"))
<< ", port: " << (opt_history_port ? mysql_history_port_str : (opt_port != 0 ? mysql_port_str : "not set"));

set_client_ssl_options(connection);

if (!mysql_real_connect(connection,
(opt_history_host ? opt_history_host : (opt_host ? opt_host : "localhost")),
(opt_history_user ? opt_history_user : opt_user),
(opt_history_password ? opt_history_password : opt_password),
"" /*database*/,
(opt_history_port ? opt_history_port : opt_port),
(opt_history_host ? "" : opt_socket), 0)) {
xb::error() << "Failed to connect to MySQL server: "
<< mysql_error(connection);
mysql_close(connection);
return (NULL);
}

xb_mysql_query(connection, "SET SESSION wait_timeout=2147483", false, true);

xb_mysql_query(connection, "SET SESSION autocommit=1", false, true);

xb_mysql_query(connection, "SET NAMES utf8", false, true);

return (connection);
}


/*********************************************************************/ /**
Execute mysql query. */
MYSQL_RES *xb_mysql_query(MYSQL *connection, const char *query, bool use_result,
Expand Down Expand Up @@ -1810,6 +1854,7 @@ bool write_xtrabackup_info(MYSQL *connection) {
char *xtrabackup_info_data = NULL;
int idx;
bool null = true;
MYSQL *conn = connection;

const char *ins_query =
"insert into PERCONA_SCHEMA.xtrabackup_history("
Expand Down Expand Up @@ -1837,9 +1882,17 @@ bool write_xtrabackup_info(MYSQL *connection) {
uuid = get_backup_uuid(connection);
server_version = read_mysql_one_value(connection, "SELECT VERSION()");

xb_mysql_query(connection, "CREATE DATABASE IF NOT EXISTS PERCONA_SCHEMA",
if (opt_history_host) {
// create a new connection to the history host
conn = xb_mysql_history_connect();
if (!conn) {
goto cleanup;
}
}

xb_mysql_query(conn, "CREATE DATABASE IF NOT EXISTS PERCONA_SCHEMA",
false);
xb_mysql_query(connection,
xb_mysql_query(conn,
"CREATE TABLE IF NOT EXISTS PERCONA_SCHEMA.xtrabackup_history("
"uuid VARCHAR(40) NOT NULL PRIMARY KEY,"
"name VARCHAR(255) DEFAULT NULL,"
Expand All @@ -1864,12 +1917,12 @@ bool write_xtrabackup_info(MYSQL *connection) {
false);

/* Upgrade from previous versions */
xb_mysql_query(connection,
xb_mysql_query(conn,
"ALTER TABLE PERCONA_SCHEMA.xtrabackup_history MODIFY COLUMN "
"binlog_pos TEXT DEFAULT NULL",
false);

stmt = mysql_stmt_init(connection);
stmt = mysql_stmt_init(conn);

mysql_stmt_prepare(stmt, ins_query, strlen(ins_query));

Expand Down Expand Up @@ -2008,6 +2061,10 @@ bool write_xtrabackup_info(MYSQL *connection) {
mysql_stmt_execute(stmt);
mysql_stmt_close(stmt);

if (opt_history_host) {
mysql_close(conn);
}

cleanup:

free(xtrabackup_info_data);
Expand Down
36 changes: 36 additions & 0 deletions storage/innobase/xtrabackup/src/xtrabackup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ uint opt_backup_lock_timeout = 0;
uint opt_backup_lock_retry_count = 0;

const char *opt_history = NULL;
char *opt_history_user = NULL;
char *opt_history_password = NULL;
char *opt_history_host = NULL;
uint opt_history_port = 0;

bool opt_decrypt = false;
uint opt_read_buffer_size = 0;

Expand Down Expand Up @@ -732,6 +737,10 @@ enum options_xtrabackup {
OPT_LOCK_WAIT_QUERY_TYPE,
OPT_KILL_LONG_QUERY_TYPE,
OPT_HISTORY,
OPT_HISTORY_HOST,
OPT_HISTORY_PORT,
OPT_HISTORY_USER,
OPT_HISTORY_PASSWORD,
OPT_KILL_LONG_QUERIES_TIMEOUT,
OPT_LOCK_WAIT_TIMEOUT,
OPT_LOCK_WAIT_THRESHOLD,
Expand Down Expand Up @@ -1266,6 +1275,33 @@ struct my_option xb_client_options[] = {
"record for the current backup being taken.",
NULL, NULL, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},

{"history-user", OPT_HISTORY_USER,
"This option specifies the MySQL username used "
"when connecting to the server, if that's not the current user. "
"The option accepts a string argument. See mysql --help for details.",
(uchar *)&opt_history_user, (uchar *)&opt_history_user, 0, GET_STR, OPT_ARG, 0, 0, 0,
0, 0, 0},

{"history-host", OPT_HISTORY_HOST,
"This option specifies the host to use when "
"saving the backup history. The option accepts "
"a string argument. See mysql --help for details.",
(uchar *)&opt_history_host, (uchar *)&opt_history_host, 0, GET_STR, OPT_ARG, 0, 0, 0,
0, 0, 0},

{"history-port", OPT_HISTORY_PORT,
"This option specifies the port to use when "
"connecting to the database server with TCP/IP. The option accepts "
"a string argument. See mysql --help for details.",
&opt_history_port, &opt_history_port, 0, GET_UINT, OPT_ARG, 0, 0, 0, 0, 0, 0},

{"history-password", OPT_HISTORY_PASSWORD,
"This option specifies the password to use "
"when connecting to the database. It accepts a string argument. "
"See mysql --help for details.",
(uchar *)&opt_history_password, (uchar *)&opt_history_password, 0, GET_STR, OPT_ARG, 0, 0, 0,
0, 0, 0},

{"kill-long-queries-timeout", OPT_KILL_LONG_QUERIES_TIMEOUT,
"This option specifies the number of seconds innobackupex waits "
"between starting FLUSH TABLES WITH READ LOCK and killing those "
Expand Down
5 changes: 5 additions & 0 deletions storage/innobase/xtrabackup/src/xtrabackup.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ extern uint opt_backup_lock_timeout;
extern uint opt_backup_lock_retry_count;

extern const char *opt_history;
extern char *opt_history_user;
extern char *opt_history_password;
extern char *opt_history_host;
extern uint opt_history_port;

extern bool opt_decrypt;

extern uint opt_read_buffer_size;
Expand Down

0 comments on commit 90cd062

Please sign in to comment.