Skip to content

MDEV-14992 BACKUP SERVER - #4817

Open
dr-m wants to merge 1 commit into
mainfrom
MDEV-14992
Open

dr-m wants to merge 1 commit into
mainfrom
MDEV-14992

Conversation

@dr-m

@dr-m dr-m commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

The following SQL statements will be introduced:

BACKUP SERVER TO '/path/to/directory';
BACKUP SERVER TO '/path/to/directory' OPTIONS backup_options;
BACKUP SERVER WITH 'command';
BACKUP SERVER WITH OPTIONS backup_options;

The backup_options is a comma-separated list of the following:

  • CONCURRENT=1: number of concurrent threads to use

  • BINLOG=NONE: omit any innodb_storage_engine=innodb files

  • BINLOG=MINIMAL: include the necessary innodb_storage_engine=innodb files

  • BINLOG=FULL: include all available innodb_storage_engine=innodb files

In place of the 1, any positive number of threads may be specified. For the first variant, '/path/to' must exist and '/path/to/directory' must not exist; that is where the backup will be written to.

For the second variant, 'command' must be the name of a script or command that will be executed in a child process. The standard input of that command will be in a format that is compatible with GNU tar --format=oldgnu (and also BSD tar variants that are also part of Microsoft Windows and Apple macOS). The command is expected to optionally compress and encrypt the stream and redirect it to a file on a local or a remote server. The BACKUP SERVER WITH will append an additional argument, a positive base-ten number in ASCII, starting with 1, to identify the current thread. In this way, each concurrent stream can write a separate file.

The backup or the first stream will contain a file backup.cnf, which includes parameters needed for restoring the backup. Currently, these are innodb_log_recovery_start and innodb_log_recovery_target. If innodb_log_recovery_target>0, InnoDB will be in read-only mode, not allowing any writes to persistent files other than via the log application.

To restore a streaming backup made with BACKUP SERVER WITH, an empty directory needs to be created and all streams be extracted there using the standard tar utility of the operating system, optionally after undoing any encryption or compression that had been added by the backup command. Then, the backup is prepared or MariaDB server started up on the extracted directory, similar to as if the BACKUP SERVER TO statement had been used.

Note: The parameter innodb_log_recovery_start in backup.cnf is STRICTLY NECESSARY TO AVOID CORRUPTION! By default, InnoDB crash recovery starts from the latest available log checkpoint. However, for restoring a backup, recovery must start from the checkpoint that was the latest when the backup was started. Starting recovery from a possible later checkpoint will result in a corrupted database!

The following will be implemented separately:

MDEV-39061 mariadb-backup compatible wrapper script for BACKUP SERVER
MDEV-40163 Partial backup and restore
MDEV-39091 Back up ENGINE=RocksDB
MDEV-39092 Less blocking backup of ENGINE=Aria

The implementation introduces a basic driver Sql_cmd_backup, storage engine interfaces, and basic copying of the storage engines InnoDB, Aria, MyISAM, MERGE (MyISAM), Archive, CSV.

backup_target: A structured data type to represent a target directory. On Microsoft Windows, we must use directory paths because there is no variant of CopyFileEx() that would work on file handles.

backup_sink: Wraps a per-thread output stream as well as storage engine specific context.

handlerton::backup_start(), handlerton::backup_end(): Invoked at the start or end of a backup phase, in the thread that executes a BACKUP SERVER statement.

handlerton::backup_step(): A backup step that can be invoked from multiple threads concurrently, between the execution of the corresponding handlerton::backup_start() and handlerton::backup_end() of the same phase.

copy_entire_file(): A file copying service for POSIX systems.

copy_file(): A partial or sparse file-copying service for all systems.

backup_stream_append(): Equivalent to copy_file(), but appending to a stream. On Linux, this uses sendfile(2), which assumes that the source data will not be changed before the data has been consumed from the pipe.

backup_stream_append_async(): A variant of backup_stream_append() where the source file region is guaranteed to be immutable after the call returns. We must not use Linux sendfile(2) for copying data files that may be modified in place, because it could introduce a race condition between a page write that runs concurrently with a child process that is reading the data from the pipe.

InnoDB_backup::context: Backup context, attached to backup_sink so that context can continue to exist between the time a BACKUP SERVER releases all locks and another BACKUP SERVER starts executing, with innodb_backup pointing to the new backup, while the old backup is still being finished.

fil_space_t::write_or_backup: Keep track of in-flight page writes and pending backup operation. We must not allow them concurrently, because that could lead into torn pages in the backup.

fil_space_t::backup_end: The first page number that is not being backed up (by default 0, to indicate that no backup is in progress).

log_sys.backup: Whether BACKUP SERVER is in progress. The purpose of this is to make BACKUP SERVER prevent the concurrent execution of SET GLOBAL innodb_log_archive=OFF or SET GLOBAL innodb_log_file_size when innodb_log_archive=OFF.

log_sys.archived_checkpoint: Keep track of the earliest available checkpoint, corresponding to log_sys.archived_lsn. This reflects SET GLOBAL innodb_log_recovery_start (which is settable now), for incremental backup.

buf_flush_list_space(): Check for concurrent backup before writing each page. This is inefficient, but this function may be invoked from multiple threads concurrently, and it cannot be changed easily, especially for fil_crypt_thread().

fil_system.have_all_spaces: Whether all tablespace metadata is guaranteed to be known. To speed up startup, InnoDB does not normally open all tablespace files.

@dr-m dr-m self-assigned this Mar 17, 2026
@CLAassistant

CLAassistant commented Mar 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@dr-m
dr-m force-pushed the MDEV-14992 branch 2 times, most recently from 2723322 to 1703796 Compare March 18, 2026 11:01
Comment thread sql/sql_backup.cc Resolved
@dr-m
dr-m force-pushed the MDEV-14992 branch 2 times, most recently from 9a529de to 857edeb Compare March 23, 2026 08:28
@dr-m
dr-m changed the base branch from 11.4 to 12.3 March 24, 2026 11:51
@dr-m
dr-m force-pushed the MDEV-14992 branch 3 times, most recently from 8149b3d to c08d121 Compare March 27, 2026 09:48
@dr-m
dr-m changed the base branch from 12.3 to main May 5, 2026 10:49
Comment thread sql/sql_backup.cc Resolved
Comment thread storage/innobase/buf/buf0flu.cc Resolved
@dr-m
dr-m force-pushed the MDEV-14992 branch 2 times, most recently from d684e5e to 676bf8b Compare October 27, 2026 10:02
@dr-m
dr-m changed the base branch from main to 13.1 October 30, 2026 09:02
@dr-m
dr-m changed the base branch from 13.1 to main October 30, 2026 13:48
@Thirunarayanan
Thirunarayanan self-requested a review October 30, 2026 13:48
@dr-m
dr-m force-pushed the MDEV-14992 branch 5 times, most recently from 5b7e53c to ce972cd Compare November 9, 2026 05:51
@dr-m
dr-m added this pull request to stack #6103 November 9, 2026 10:51
@dr-m
dr-m removed this pull request from stack #6103 November 9, 2026 12:59
@dr-m
dr-m added this pull request to stack #6105 November 9, 2026 13:00
Comment thread storage/innobase/buf/buf0buf.cc Resolved
@knielsen

Copy link
Copy Markdown
Member

I tried backing up a server where --innodb-undo-directory is non-empty.
If using a relative path, the backup failed with an error:

MariaDB [test]> backup server to '/tmp/x/hulu';
ERROR 1004 (HY000): Can't create file 'myundo/undo003' (errno: 2 "No such file or directory")

When using an absolute path, no error, but the undo files were silently missing from the backup and starting a server on the backup fails:

2026-11-20  3:40:15 0 [ERROR] InnoDB: Missing FILE_CREATE, FILE_DELETE or FILE_MODIFY before FILE_CHECKPOINT for tablespace 1

A solution for this is also needed for the similar problem with --binlog-directory for my patch to include InnoDB binlog files in the backup. For relative path, currently --binlog-directory includes the file with the same relative path in the backup, and the user would use the same --binlog-directory in the config file as on the originating server.

For absolute path, some solution will be needed, and I think the user will need to modify the config or the backup target (or both) before starting the new server. One solution could be to put the full, original path under some $target_dir/.abs_paths/orig/full/path/to/file in the backup. Another option could be to include the full absolute path in the tar output of a stream backup, and give an error for absolute path in the non-stream backup.

Comment thread storage/innobase/log/log0recv.cc Resolved
@dr-m
dr-m force-pushed the MDEV-14992 branch 2 times, most recently from 7e4746c to 2b34795 Compare November 25, 2026 07:56
@dr-m

dr-m commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@knielsen, thank you for your comment. Because you did not attach your comment to a file, I can’t mark it as resolved nor post a reply that would appear as a thread.

I tried backing up a server where --innodb-undo-directory is non-empty. If using a relative path, the backup failed with an error:

MariaDB [test]> backup server to '/tmp/x/hulu';
ERROR 1004 (HY000): Can't create file 'myundo/undo003' (errno: 2 "No such file or directory")

When using an absolute path, no error, but the undo files were silently missing from the backup and starting a server on the backup fails:

2026-11-20  3:40:15 0 [ERROR] InnoDB: Missing FILE_CREATE, FILE_DELETE or FILE_MODIFY before FILE_CHECKPOINT for tablespace 1

Thank you for the report. I will check this and try to find a solution or work-around.

A solution for this is also needed for the similar problem with --binlog-directory for my patch to include InnoDB binlog files in the backup. For relative path, currently --binlog-directory includes the file with the same relative path in the backup, and the user would use the same --binlog-directory in the config file as on the originating server.

For absolute path, some solution will be needed, and I think the user will need to modify the config or the backup target (or both) before starting the new server. One solution could be to put the full, original path under some $target_dir/.abs_paths/orig/full/path/to/file in the backup. Another option could be to include the full absolute path in the tar output of a stream backup, and give an error for absolute path in the non-stream backup.

Currently, I believe that it might be easiest to copy everything into a single directory, similar to how we lose INDEX DIRECTORY and DATA DIRECTORY attributes on Aria and MyISAM tables also in the legacy mariadb-backup. The user would have to either move files or adjust the start-up parameters before starting a server on the backup. For DATA DIRECTORY in InnoDB, BACKUP SERVER TO is currently broken, but BACKUP SERVER WITH should write absolute path names. I think that we must fix MDEV-41744 to have uniform treatment with Aria and MyISAM.

The following SQL statements will be introduced:

BACKUP SERVER TO '/path/to/directory' [ OPTIONS backup_options ]
BACKUP SERVER WITH 'command' [ OPTIONS backup_options ];

The backup_options is a comma-separated list of the following:
CONCURRENT=1: number of concurrent threads to use
BINLOG=NONE: omit any innodb_storage_engine=innodb files
BINLOG=MINIMAL: include the necessary innodb_storage_engine=innodb files
BINLOG=FULL: include all available innodb_storage_engine=innodb files

In place of the 1, any positive number of threads may be specified.
For the first variant, '/path/to' must exist and '/path/to/directory'
must not exist; that is where the backup will be written to.

In BACKUP SERVER TO, when innodb_undo_directory or binlog_directory
differ from the datadir, the undo* or *.ibb files will be placed
directly in the backup target directory. The user can move them
to the appropriate place after restoring the backup, as appropriate.

For BACKUP SERVER WITH, the string mariadb-backup-
will be prepended to the specified command and quoted.
Path separators or quotes are not allowed in command.
We expect the mariadb-backup-command to be in the
search PATH of the mariadbd process.
For now, we distribute no such script or program;
the user has to write one, something like this:

zstdmt|exec ssh backup@remote.example.com "exec cat > $1.tar.zstd"

The standard input of that command will be in a format compatible with
GNU tar --format=oldgnu (and also BSD tar variants that are also part of
Microsoft Windows and Apple macOS). The command is expected to optionally
compress and encrypt the stream and redirect it to a file on a local or
a remote server. The BACKUP SERVER WITH will append an additional argument,
a positive base-ten number in ASCII, starting with 1, to identify the
current thread. In this way, there can be multiple concurrent streams.
The InnoDB write-ahead log will be streamed near the end, because
the stream format requires the file sizes to be declared upfront.

In BACKUP SERVER TO ... OPTIONS CONCURRENT=2 or more, one thread will be
responsible for copying the InnoDB write-ahead log from the beginning
of the backup, running InnoDB_backup::log_track(). Other files will be
copied by other threads.

Note: In single-threaded BACKUP SERVER TO, the parameter
innodb_log_recovery_start that is written out to backup.cnf is
STRICTLY NECESSARY TO AVOID CORRUPTION during recovery! By default,
InnoDB crash recovery starts from the latest available log
checkpoint. However, for restoring a backup, recovery must start from
the checkpoint that was the latest when the backup was
started. Starting recovery from a possible later checkpoint will
result in a corrupted database!

The backup or the first stream will contain a file backup.cnf, which
includes parameters needed for restoring the backup. Currently,
these are innodb_log_recovery_start and innodb_log_recovery_target.
If innodb_log_recovery_target>0, InnoDB will be in read-only mode,
not allowing any writes to persistent files other than via the log
application.

To restore a streaming backup made with BACKUP SERVER WITH, an empty
directory needs to be created and all streams be extracted there using
the standard tar utility of the operating system, optionally after
undoing any encryption or compression that had been added by the
backup command. Then, the backup is prepared or MariaDB server started
up on the extracted directory, similar to as if the BACKUP SERVER TO
statement had been used.

The following will be implemented separately:

MDEV-39061 mariadb-backup compatible wrapper script for BACKUP SERVER
MDEV-40163 Partial backup and restore
MDEV-39091 Back up ENGINE=RocksDB
MDEV-40333 Concurrent DDL for Aria tables in BACKUP SERVER
MDEV-39987 Improve BACKUP SERVER performance for ENGINE=Aria

The implementation introduces a basic multi-threaded driver
Sql_cmd_backup, storage engine interfaces, and basic copying of the
storage engines InnoDB, Aria, MyISAM, MERGE (MyISAM), Archive, CSV.

backup_target: A target directory and a set of operations,
such as backup_target::create_file().
On Microsoft Windows, we must use directory paths because there is
no variant of CopyFileEx() that would work on file handles.

backup_sink: Per-thread storage engine context and output stream,
and a set of operations such as backup_sink::create_file() that
are valid if an output stream exists. The member "id" identifies
the thread, between 0 and CONCURRENT-1.

backup_context: Process-wide BACKUP SERVER context.
Handles the directory traversal and copying of
files for built-in storage engines that do not
implement this backup interface.

backup_target_stage: Wrap backup_context.

backup_target_stage::step(), backup_context::step(): Process a file
from a directory scan, or by invoking handlerton::backup_step().

handlerton::backup_start(), handlerton::backup_end(): Invoked at the
start or end of a backup phase, in the thread that executes a
BACKUP SERVER statement.

handlerton::backup_step(): A backup step that can be invoked from
multiple threads concurrently, between the execution of the corresponding
handlerton::backup_start() and handlerton::backup_end() of the same
phase.

my_clone_file_range(): A wrapper for Linux block-cloning ioctl(2)
FICLONERANGE, which may fail depending on the parameters
and thus require a fallback to my_copy_file_range().

my_copy_file_range(): A partial or sparse file-copying service.
On FreeBSD, this simply wraps copy_file_range(2).
On Linux, for small files we attempt copy_file_range(2) before
falling back to specialised a pread/pwrite loop, which turned out
to be the fastest way to copy large files. Even though sendfile(2)
would work for copying between files, it as well as
copy_file_range(2) can be much slower than the alternatives.
For large files on Linux, we always invoke a pread/pwrite loop
with special cache control. It is also needed as a fallback,
because the Linux copy_file_range(2) can fail depending on the
file system.

On Windows, my_copy_file_range() is not usable on files that have
been opened for asynchronous I/O, such as InnoDB data files.
Such files have to be copied via backup::copy(), which uses the
native_file_handle wrapper of HANDLE.

my_copy_file(): Copy files between two POSIX file descriptors.
On Microsoft Windows, my_copy() or CopyFileEx() should be used instead.
On Linux, we attempt the ioctl(2) FICLONE before falling back to
my_copy_file_range().

my_copy(): Make use of CopyFileEx() on Microsoft Windows.

backup::copy(): Wraps my_copy_file_range() or the
Windows native_file_handle based copied outside mysys.

backup::copy_or_stream(): Copy or stream a file.

backup_sink::append(): The streaming streaming equivalent of backup::copy().

backup_sink::append_async(): A variant of backup_sink::append()
where the source file region is guaranteed to be immutable after the
call returns. Zero-copy Linux sendfile(2) is inherently risky for copying
data files that may be modified in place, because it could introduce
a race condition between a page write that runs concurrently with a
child process that is reading the data from the pipe. This could be
improved with io_uring later.

backup_sink::zeropad(): Zero-pad the last tar block if needed.

backup_sink::write(): Write to a stream or a file, with a retry
mechanism in case a partial write occurred.

pread_write(): Allocate a buffer of up to 4 MiB.
This is the "slow path" of copying or streaming files.
For InnoDB data files, BACKUP SERVER TO invokes this on output that
has been opened in unbuffered I/O (O_DIRECT) mode, to avoid polluting
the Linux file system cache (block cache). For the InnoDB log,
regular buffered I/O performed best in our tests.

maria_backup_start(): The entry point for ENGINE=Aria.
Currently, the directory traversal is single-threaded and a memory hog.
This could be improved in MDEV-39987. The Aria log and data file
copying were implemented by Michael 'Monty' Widenius and
Alessandro Vetere.

maria_backup_log_tail(): Copy the necessary part of the last
ENGINE=Aria log, by my_clone_file_range() or my_copy_file_range().
The copy will be trimmed to the logical end of the backup.
The logic was developed by Alessandro Vetere.

InnoDB_backup::init(): Wait for a possible previous BACKUP SERVER
operation to reach the very end of InnoDB_backup::context::cleanup()
so that the context can be safely reused.

InnoDB_backup::queue: Collection of tablespace IDs and payload sizes
at the start of the backup, and the log_sys.first_lsn of log files
that have to be included in the backup. In the BACKUP_FINISHED stage,
this identifies the binlog_storage_engine=innodb files that are to
be copied. If any data file is created or extended while the backup
is executing, we must have the corresponding write-ahead-log entries
that we are copying since the latest checkpoint that was completed when
the backup started. If any tablespaces are deleted during the backup,
we may or may not copy them, and the application of a FILE_DELETE record
will remove them. Similarly, applying FILE_RENAME or FILE_CREATE records
will rename or create files during recovery as needed.

InnoDB_backup::context::tracked: Log file queue for the dedicated
log-copying task InnoDB_backup::log_track().

innodb_backup_log_tracking(): Determine if log-tracking backup is
active, which blocks concurrent changes of innodb_log_file_buffering
and innodb_log_file_write_through.

log_sys.backup: Whether BACKUP SERVER is in progress. The purpose of this
is to make BACKUP SERVER prevent the concurrent execution of
SET GLOBAL innodb_log_archive=OFF or SET GLOBAL innodb_log_file_size
when innodb_log_archive=OFF.

log_sys.archived_checkpoint: Keep track of the earliest available
checkpoint, corresponding to log_sys.archived_lsn. This reflects
SET GLOBAL innodb_log_recovery_start (which is settable now), for
incremental backup.

fil_system.have_all_spaces: Whether all tablespace metadata is guaranteed
to be known. To speed up startup, InnoDB does not normally open
all tablespace files.

fil_space_t::create_lsn: Change to Atomic_relaxed and use this to
indicate tablespace creation LSN, in addition to indicate undo
tablespace rebuild LSN.

fil_space_t::backup_end: The first page number that is not being backed up
(by default 0, to indicate that no backup is in progress).

fil_space_t::BACKUP_BATCH_SIZE: The number of preceding pages that will be
covered by fil_space_t::backup_end. This is the unit of "page range locking"
during InnoDB backup.

fil_node_t::backup_name: A snapshot of the file name, sampled in
InnoDB_backup::init(). For backing up any files that were created before
the backup start checkpoint LSN, these names will be written to the backup.
On restore, the files will be renamed or deleted as needed, by
applying FILE_RENAME or FILE_DELETE records.

fil_node_t::rename(), fil_node_t::set_backup_name(),
fil_node_t::get_backup_name(), fil_node_t::~fil_node_t(),
fil_node_t::clear_backup_name(): Deal with fil_node_t::backup_name.

buf_page_t::write_fix_try(), buf_page_t::write_unfix_try(): Try to set
or unset a fake "write fix" on a page, to prevent concurrent flush()
during a backup batch. The atomic operations may run concurrently with
set_reinit() and set_freed(). The fake "write fix" does not prevent
any concurrent read or write of the page data in the buffer pool; it
only blocks writes to the underlying data file.

buf_page_t::flush(): Atomically test and set write fix, and
skip the operation if the fake "write fix" was set.

buf_page_t::set_freed(), buf_page_t::set_reinit(): Employ a
compare-and-exchange loop to accommodate for the "write fix".

innodb_backup_batch_wait(): Look up any pages that we are about to
back up. For any dirty pages, invoke buf_page_t::write_fix_try() to
try to set a fake "write fix" lock-free. If the page is currently
write-fixed between buf_page_t::flush() and
buf_page_t::write_complete(), acquire and release a page U-latch to
wait for the conflicting write to complete.

InnoDB_backup::backup_batch_start(),
InnoDB_backup::backup_batch_stop(): Adjust fil_space_t::backup_end and
fake "write fix" of dirty pages to protect the copying of a range of
pages from the underlying file.

InnoDB_backup::commit(): Enqueue the remaining log to be copied.

InnoDB_backup::checkpoint_complete(): If backup is running and
commit() has not been called, add each completed innodb_archive_log=ON
file to the queue. Else, skip or delete, as appropriate.

InnoDB_backup::archive_create(), innodb_backup_archive_create():
Hook for log_t::archive_create().

log_t::backup_start(): If we were running with innodb_log_archive=ON,
ensure that the latest file is a valid recovery starting point.
That is, wait for the latest log checkpoint to be within the file.

buf_flush_list_space(): Check for concurrent backup before writing each
page. This is inefficient, but this function may be invoked from multiple
threads concurrently, and it cannot be changed easily, especially for
fil_crypt_thread().

fil_ibd_create(): Set fil_space_t::create_lsn after the file has been
created.

dict_load_tablespaces(): Determine the size of each file if
upgrade==true. Backup depends on that.

buf_dblwr_t::begin(), buf_dblwr_t::end(), buf_dblwr_t::size():
Accessors to allow BACKUP SERVER to skip the contents of the
doublewrite buffer in the system tablespace. It is only useful for
crash recovery in case a data page had been incompletely written by
the time the server was killed. If the server is killed during a
backup, the backup will be incomplete and unusable anyway.
Furthermore, the page range locking makes page writes and backup
mutually exclusive.

Tested by: Matthias Leich
Tested by: Saahil Alam
Tested by: Rahul Raj
Co-authored by: Michael 'Monty' Widenius (mysys and ENGINE=Aria changes)
Co-authored by: Alessandro Vetere (some ENGINE=Aria changes)
Co-authored by: Kristian Nielsen (the binlog_storage_engine=innodb part)
Reviewed by: Thirunarayanan Balathandayuthapani (the ENGINE=InnoDB part)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

7 participants