Skip to content

Latest commit

 

History

History
443 lines (309 loc) · 20.6 KB

MySQL.md

File metadata and controls

443 lines (309 loc) · 20.6 KB

WAL-G for MySQL

Interface of MySQL and MariaDB now is unstable

You can use wal-g as a tool for encrypting, compressing MySQL backups and push/fetch them to/from storage without saving it on your filesystem.

Configuration

  • WALG_MYSQL_DATASOURCE_NAME

To configure the connection string for MySQL. Required. DSN format user:password@tcp(host)/dbname

  • WALG_MYSQL_SSL_CA

To use SSL, a path to file with certificates should be set to this variable.

  • WALG_STREAM_CREATE_COMMAND

Command to create MySQL backup, should return backup as single stream to STDOUT. Requried.

  • WALG_STREAM_RESTORE_COMMAND

Command to unpack MySQL backup, should take backup (created by WALG_STREAM_CREATE_COMMAND) to STDIN and unpack it to MySQL datadir. Required.

  • WALG_MYSQL_BACKUP_PREPARE_COMMAND

Command to prepare MySQL backup after restoring. Optional. Needed for xtrabackup case.

  • WALG_DELTA_MAX_STEPS

Delta-backup is the difference between previously taken backup and present state. WALG_DELTA_MAX_STEPS determines how many delta backups can be between full backups. Defaults to 0 (disabled). Restoration process will automatically fetch all necessary deltas and base backup and compose valid restored backup (you still need WALs after start of last backup to restore consistent cluster). Delta computation is based on ModTime of file system and LSN number of pages in datafiles.

Note: Incremental backups only supported in wal-g xtrabackup-push command.

  • WALG_DELTA_ORIGIN

To configure base for next delta backup (only if WALG_DELTA_MAX_STEPS is not exceeded). WALG_DELTA_ORIGIN can be LATEST (chaining increments), LATEST_FULL (for bases where volatile part is compact and chaining has no meaning - deltas overwrite each other). Defaults to LATEST.

Note: Incremental backups only supported in wal-g xtrabackup-push command.

  • WALG_MYSQL_BINLOG_REPLAY_COMMAND

Command to replay binlog on running MySQL. Required for binlog-replay command.

  • WALG_MYSQL_BINLOG_DST

To place binlogs in the specified directory during binlog-fetch, binlog-replay or binlog-server

  • WALG_MYSQL_BINLOG_SERVER_HOST
  • WALG_MYSQL_BINLOG_SERVER_PORT
  • WALG_MYSQL_BINLOG_SERVER_USER
  • WALG_MYSQL_BINLOG_SERVER_PASSWORD

To configure the data to connect the replica to for binlog server.

  • WALG_MYSQL_BINLOG_SERVER_ID

To configure the server id of the binlog server. Should be unique for each replica.

  • WALG_MYSQL_BINLOG_SERVER_REPLICA_SOURCE

To configure the connection string that will be used by binlog-server to connect to your MySQL. DSN format: user:password@tcp(host)/dbname

  • WALG_MYSQL_BINLOG_SERVER_DISABLE_HEARTBEAT

Set to true to disable binlog-server heartbeats. Default: false. Required for MySQL versions earlier than 8.0.28, which do not support the HEARTBEAT_LOG_EVENT_V2 events sent by binlog-server.

Operations with binlogs: If you'd like to do binlog operations with wal-g don't forget to activate the binary log by starting mysql/mariadb with --log-bin and --log-basename=[name].

  • WALG_STREAM_SPLITTER_PARTITIONS

To configure split backup stream into several parts and upload them in parallel. Backup file names have a suffix _0000.bz.

  • WALG_STREAM_SPLITTER_BLOCK_SIZE

To configure block size (bytes) into which backup stream split. Blocks of this length size is put into each partition in turn.

  • WALG_STREAM_SPLITTER_MAX_FILE_SIZE

To configure max file size (bytes) before compressing. If partition size become more than max file size, it split on several files. Backup file names have a suffix _0000_0000.bz.

  • WALG_BACKUP_DOWNLOAD_MAX_RETRIES

Configure max attempts to download backup file. Default value 1.

Usage

WAL-G mysql extension currently supports these commands:

backup-push

Creates new backup and send it to storage. Runs WALG_STREAM_CREATE_COMMAND to create backup.

wal-g backup-push

Journal (binlog) size accounting

Run backup-push with the --count-journals flag to maintain a journal_<backup> object per backup in storage, tracking the volume of binlog accumulated between that backup and the next one. The size is computed from the actual storage object sizes of the archived binlog segments, so it correctly reflects compression.

wal-g backup-push --count-journals

Journal accounting is skipped for permanent backups (marked with --permanent), since they are not expected to be removed and don't take part in WAL retention planning.

Currently, only delete target cleans up and re-merges the corresponding journal entry when a backup is removed this way.

xtrabackup-push

Creates new backup with xtrabackup tool and send it to storage. Runs WALG_STREAM_CREATE_COMMAND to create backup. WAL-G levereages knowledge of xtrabackup format to support additional feature (e.g. incremental backups)

wal-g xtrabackup-push
  • WALG_MYSQL_INCREMENTAL_BACKUP_DST

To place incremental backup in the specified directory during backup-fetch

backup-list

Lists currently available backups in storage

wal-g backup-list

backup-fetch

Fetches backup from storage and restores it to datadir. Runs WALG_STREAM_RESTORE_COMMAND to restore backup. User should specify the name of the backup to fetch.

wal-g backup-fetch example_backup

WAL-G can also fetch the latest backup using:

wal-g backup-fetch  LATEST

copy

Copies one backup, its incremental ancestors, or all backups between storage configurations without transforming payload objects:

wal-g copy --from=config_from.json --to=config_to.json --backup-name=LATEST

Add --with-history to synchronize binlogs from the selected backup recovery point through the latest continuous archived binlog. Repeating the command later copies only missing immutable objects and refreshes the binlog sentinel when one is present. The older backup-copy command remains available as a compatibility alias, including --add-prefix.

Immutable destination objects with the same path and size are skipped; size conflicts fail the command before any transfers by default. Set WALG_DISASTER_COPY=true in the environment, the main configuration, or either storage configuration to overwrite all size conflicts with source bytes, including zero-byte destinations. Each planned replacement is logged with both sizes. This also applies to backup-copy. Transfer failures remain fatal after applicable retries; cancellation remains fatal.

get-stream

Download the specified backup as single stream (when backup is stream-based backup). This command will:

  • decrypt backup
  • decompress backup
  • assemble parts of backups (when WALG_STREAM_SPLITTER_* used)
  • save to file

Example:

wal-g get-stream BACKUP_NAME path/to/local_file upload the local file to the storage.

wal-g get-stream BACKUP_NAME | xbstream --extract upload the local file to the storage.

binlog-push

Sends (not yet archived) binlogs to storage. Typically run in CRON.

wal-g binlog-push

When WALG_MYSQL_CHECK_GTIDS is set wal-g will try to be upload only binlogs which GTID sets contains events that wasn't seen before. This is done by parsing binlogs and peeking first PREVIOUS_GTIDS_EVENT that holds GTID set of all executed transactions at the moment this particular binlog file created. This feature may be useful when you are uploading binlogs from different hosts (e.g. after master switchower) Note: Don't use WALG_MYSQL_CHECK_GTIDS when GTIDs are not used - it will slow down binlog upload.

binlog-fetch

Fetches binlogs from storage and saves them to WALG_MYSQL_BINLOG_DST folder. User should specify the name of the backup starting with which to fetch an binlog. User may also specify time in RFC3339 format until which should be fetched (used for PITR). User have to replay binlogs manually in that case.

wal-g binlog-fetch --since "backupname"

or

wal-g binlog-fetch --since "backupname" --until "2006-01-02T15:04:05Z07:00"

or

wal-g binlog-fetch --since LATEST --until "2006-01-02T15:04:05Z07:00"

You can stop wal-g from fetching newly created/modified binlogs by specifying --until-binlog-last-modified-time option. This may be useful to achieve exact clones of the same database in scenarios when new binlogs are uploaded concurrently whith your restore process.

wal-g binlog-replay --since LATEST --until "2006-01-02T15:04:05Z07:00" --until-binlog-last-modified-time "2006-01-02T15:04:05Z07:00"

binlog-replay

Fetches binlogs from storage and passes them to WALG_MYSQL_BINLOG_REPLAY_COMMAND to replay on running MySQL server. User should specify the name of the backup starting with which to fetch an binlog. User may also specify time in RFC3339 format until which should be fetched (used for PITR). If until timestamp is in the future, wal-g will search for newly uploaded binlogs until no new found. Binlogs are temporarily save in WALG_MYSQL_BINLOG_DST folder. Replay command gets name of binlog to replay via environment variable WALG_MYSQL_CURRENT_BINLOG and stop-date via WALG_MYSQL_BINLOG_END_TS, which are set for each invocation.

If the backup was taken with xtrabackup/mariabackup, wal-g reads the exact binlog file, position and GTID that were recorded in the backup at the time it finished, and stores them in the backup sentinel. GTID is recorded whenever the source has it enabled — for MySQL that means GTID_MODE=ON, not just MariaDB. Binlogs that are entirely older than that recorded file are not fetched at all (they were already fully captured by the backup). For the one binlog file that straddles the backup boundary, wal-g additionally sets:

  • WALG_MYSQL_BINLOG_START_POSITION — the numeric byte offset within that binlog file to resume from. Use it with --start-position="$WALG_MYSQL_BINLOG_START_POSITION" on either mariadb-binlog (MariaDB) or mysqlbinlog (MySQL).
  • WALG_MYSQL_BINLOG_LAST_GTID — the last GTID applied by the backup, only set when the backup recorded one. Do not pass this to --start-position — it expects a numeric offset, not a GTID string. It is intended for a GTID-based exclude filter instead, e.g. mysqlbinlog --exclude-gtids="$WALG_MYSQL_BINLOG_LAST_GTID" on MySQL.

Both are only set when applicable (e.g. WALG_MYSQL_BINLOG_START_POSITION only for the binlog matching the recorded backup boundary); a replay command should treat their absence as "replay this binlog from its start". Example WALG_MYSQL_BINLOG_REPLAY_COMMAND for MySQL, using both:

WALG_MYSQL_BINLOG_REPLAY_COMMAND='mysqlbinlog --stop-datetime="$WALG_MYSQL_BINLOG_END_TS" ${WALG_MYSQL_BINLOG_START_POSITION:+--start-position="$WALG_MYSQL_BINLOG_START_POSITION"} ${WALG_MYSQL_BINLOG_LAST_GTID:+--exclude-gtids="$WALG_MYSQL_BINLOG_LAST_GTID"} "$WALG_MYSQL_CURRENT_BINLOG" | mysql'
wal-g binlog-replay --since "backupname"

or

wal-g binlog-replay --since "backupname" --until "2006-01-02T15:04:05Z07:00"

or

wal-g binlog-replay --since LATEST --until "2006-01-02T15:04:05Z07:00"

You can stop wal-g from applying newly created/modified binlogs by specifying --until-binlog-last-modified-time option. This may be useful to achieve exact clones of the same database in scenarios when new binlogs are uploaded concurrently whith your restore process.

wal-g binlog-replay --since LATEST --until "2006-01-02T15:04:05Z07:00" --until-binlog-last-modified-time "2006-01-02T15:04:05Z07:00"

binlog-server

Runs mysql server implementation which can be used to fetch binlogs from storage and send them to MySQL slave by replication protocol.

wal-g binlog-server

binlog-list

Lists binlogs in storage with filtering and formatting options.

wal-g binlog-list

backup-mark

Backups can be marked as permanent to prevent them from being removed when running delete. To mark backup as permanent call wal-g backup-mark -b backup_name. To remove permanent flag - call wal-g backup-mark -b backup_name -i When incremental backup is marked as permanent - all parent backups also marked as permanent.

Typical configurations

MySQL - using with xtrabackup

It's recommended to use wal-g with xtrabackup tool in case of MySQL for creating lock-less backups. Here's typical wal-g configuration for that case:

 WALG_MYSQL_DATASOURCE_NAME=user:pass@tcp(localhost:3306)/mysql                                                                                                                                      
 WALG_STREAM_CREATE_COMMAND="xtrabackup --backup --stream=xbstream --datadir=/var/lib/mysql"                                                                                                                               
 WALG_STREAM_RESTORE_COMMAND="xbstream -x -C /var/lib/mysql"                                                                                                                       
 WALG_MYSQL_BACKUP_PREPARE_COMMAND="xtrabackup --prepare --target-dir=/var/lib/mysql"                                                                                              
 WALG_MYSQL_BINLOG_REPLAY_COMMAND='mysqlbinlog --stop-datetime="$WALG_MYSQL_BINLOG_END_TS" "$WALG_MYSQL_CURRENT_BINLOG" | mysql'

Restore procedure is a bit tricky:

  • stop mysql
  • clean a datadir (typically /var/lib/mysql)
  • fetch and prepare desired backup using wal-g backup-fetch "backup_name"
  • start mysql
  • in case of you have replication and GTID enabled: set mysql GTID_PURGED variable to value from /var/lib/mysql/xtrabackup_binlog_info, using
gtids=$(tr -d '\n' < /var/lib/mysql/xtrabackup_binlog_info | awk '{print $3}')
mysql -e "RESET BINARY LOGS AND GTIDS; SET @@GLOBAL.GTID_PURGED='$gtids';"

RESET BINARY LOGS AND GTIDS is available in MySQL 8.4 and newer. Use RESET MASTER on MySQL 5.7 and 8.0.

  • for PITR, replay binlogs with
wal-g binlog-replay --since "backup_name" --until "2006-01-02T15:04:05Z"

MySQL - using with mysqldump

It's possible to use wal-g with standard mysqldump/mysql tools. In that case MySQL backup is a plain SQL script. Here's typical wal-g configuration for that case:

 WALG_MYSQL_DATASOURCE_NAME=user:pass@tcp(localhost)/mysql                                                                                                               
 WALG_STREAM_CREATE_COMMAND="mysqldump --all-databases --single-transaction --set-gtid-purged=ON"                                                                                                                               
 WALG_STREAM_RESTORE_COMMAND="mysql"
 WALG_MYSQL_BINLOG_REPLAY_COMMAND='mysqlbinlog --stop-datetime="$WALG_MYSQL_BINLOG_END_TS" "$WALG_MYSQL_CURRENT_BINLOG" | mysql'

Restore procedure is straightforward:

  • start mysql (it's recommended to create new mysql instance)
  • fetch and apply desired backup using wal-g backup-fetch "backup_name"

MySQL - PiTR with wal-g binlog-server

wal-g can work as replication source to do fast PiTR. In this case it will serve binlogs from storage directly to MySQL. It is expected that PiTR in this mode will be much faster than in classic approach because there won't be additional transformations from binlog to SQL, also you can benefit from multi-threaded replication.

 WALG_MYSQL_BINLOG_SERVER_HOST="127.0.0.1"
 WALG_MYSQL_BINLOG_SERVER_PORT=9306
 WALG_MYSQL_BINLOG_SERVER_USER="walg"
 WALG_MYSQL_BINLOG_SERVER_PASSWORD="walgpwd"
 WALG_MYSQL_BINLOG_SERVER_ID=99

 WALG_MYSQL_BINLOG_SERVER_REPLICA_SOURCE="user:password@tcp(127.0.0.1:3306)/db"

binlog-server uses heartbeat V2 to keep the replication connection alive while idle. For MySQL versions earlier than 8.0.28, disable heartbeats in the environment where you start WAL-G:

export WALG_MYSQL_BINLOG_SERVER_DISABLE_HEARTBEAT=true

With heartbeats disabled, consider increasing the replica's slave_net_timeout to avoid reconnects during delays in fetching binlogs or waiting for the replica to apply them. The default is 60 seconds. For example, set it to one hour before START SLAVE:

SET GLOBAL slave_net_timeout = 3600;

Choose a timeout longer than the expected idle periods; a larger value also delays detection of a broken connection.

Restore procedure is straightforward:

  • restore backup

  • disable automatic replication startup in MySQL (skip_replica_start on current versions, skip_slave_start on older versions)

  • start MySQL, purge GTIDs (see above)

  • in second terminal start binlog-server: wal-g binlog-server --until "1985-10-26T01:21:00Z"

  • in MySQL:

      SET GLOBAL SERVER_ID=999;
      CHANGE REPLICATION SOURCE TO SOURCE_HOST="127.0.0.1", SOURCE_PORT=9306, SOURCE_USER="walg", SOURCE_PASSWORD="walgpwd", SOURCE_AUTO_POSITION=1, SOURCE_SSL=0;
      SHOW REPLICA STATUS \G
      START REPLICA;

    On MySQL versions older than 8.0.23 use CHANGE MASTER TO and its MASTER_* options. On versions older than 8.0.22 use SHOW SLAVE STATUS and START SLAVE.

    SOURCE_SSL=0 is needed for this local connection: WAL-G binlog-server does not provide TLS, while MySQL 9.7 enables it by default for new replication channels. For a connection between hosts, use a secure tunnel.

  • wait until wal-g exit (it will wait until binlogs will be applied)

  • in case of errors use classic approach

MariaDB - using with mariabackup

It's recommended to use wal-g with mariabackup tool in case of MariaDB for creating lock-less backups. Here's typical wal-g configuration for that case:

 WALG_MYSQL_DATASOURCE_NAME=user:pass@tcp(localhost:3305)/mysql                                                                                                                                      
 WALG_STREAM_CREATE_COMMAND="mariabackup --backup --stream=xbstream --datadir=/var/lib/mysql"                                                                                                                               
 WALG_STREAM_RESTORE_COMMAND="mbstream -x -C /var/lib/mysql"                                                                                                                       
 WALG_MYSQL_BACKUP_PREPARE_COMMAND="mariabackup --prepare --target-dir=/var/lib/mysql"                                                                                              
 WALG_MYSQL_BINLOG_REPLAY_COMMAND='mysqlbinlog --stop-datetime="$WALG_MYSQL_BINLOG_END_TS" "$WALG_MYSQL_CURRENT_BINLOG" | mysql'

For the restore procedure you have to do similar things to what the offical docs says about full backup and restore:

  • stop mariadb
  • clean a datadir (typically /var/lib/mysql)
  • fetch and prepare desired backup using wal-g backup-fetch "backup_name"
  • after the previous step you might have to fix file permissions: chown -R mysql:mysql /var/lib/mysql
  • start mariadb
  • WAL-G doesn't support automatic PITR for MariaDB. There are 2 possible workarounds:
    • You can configure restored database to replicate from your master, so it will be able to catch up (follow the official docs)
    • You can manually replay events with binlog and position:
wal-g binlog-fetch --since [backup name | LATEST]
# Get binlog-name, position and GTIDs:
tail -n 1 < /var/lib/mysql/xtrabackup_binlog_info
# eg 'mysql-bin.000005	385	0-1-5763'
# then replay it manually:
mysql -e "STOP ALL SLAVES; SET GLOBAL gtid_slave_pos='$gtids';"
mysqlbinlog --stop-datetime="some point in time" --start-position [position above] [all binlogs starting from thouse we seen above] | mysql --user XXX --host YYY [other options]

MariaDB - using with mysqldump

The procedure is same as in case of MySQL. You can follow the instructions from the previous section.

MySQL running integration tests

Use MYSQL_SERIES to select 57, 80, 84, or 97.

# Run all tests for MySQL 8.4 (the default).
make mysql_integration_test

# Run all tests for MySQL 5.7.
make MYSQL_SERIES=57 mysql_integration_test

MYSQL_TEST_FILTER selects tests whose filenames contain the given substring. Matching is case-sensitive.

# Run all tests with pitr in their filenames for MySQL 8.4 (the default).
make MYSQL_TEST_FILTER=pitr mysql_integration_test