Table of Contents

Class BackupManager

Namespace
CL.MySQL2.Services
Assembly
CL.MySQL2.dll

Creates and manages schema backup files for MySQL tables and databases. Backups contain DDL (CREATE TABLE statements) and are stored as .sql files.

Files go to DataDirectory/backups unless the connection's BackupDirectory is set, in which case that directory is used for both writing and reading back.

public sealed class BackupManager
Inheritance
BackupManager
Inherited Members

Constructors

BackupManager(ConnectionManager, string, ILogger?, Func<string, MySqlDatabaseConfig?>?)

public BackupManager(ConnectionManager connectionManager, string dataDirectory, ILogger? logger = null, Func<string, MySqlDatabaseConfig?>? configLookup = null)

Parameters

connectionManager ConnectionManager

The connection manager to use for database access.

dataDirectory string

Base data directory; backups default to its backups subfolder.

logger ILogger

Optional logger.

configLookup Func<string, MySqlDatabaseConfig>

Optional delegate resolving per-connection config, so a configured BackupDirectory can override the default location.

Methods

BackupDatabaseSchemaAsync(string, CancellationToken)

Backs up the DDL for all tables in the current database.

public Task<Result<bool>> BackupDatabaseSchemaAsync(string connectionId = "Default", CancellationToken ct = default)

Parameters

connectionId string

The connection ID to use.

ct CancellationToken

Cancellation token.

Returns

Task<Result<bool>>

BackupTableSchemaAsync(string, string, CancellationToken)

Backs up the CREATE TABLE DDL for the specified table to a timestamped .sql file.

public Task<Result<bool>> BackupTableSchemaAsync(string tableName, string connectionId = "Default", CancellationToken ct = default)

Parameters

tableName string

The table to back up.

connectionId string

The connection ID to use.

ct CancellationToken

Cancellation token.

Returns

Task<Result<bool>>

CleanupOldBackupsAsync(int, string)

Removes backup files older than the specified number of days.

public Task<Result<int>> CleanupOldBackupsAsync(int olderThanDays = 30, string connectionId = "Default")

Parameters

olderThanDays int

Files older than this many days will be deleted.

connectionId string

The connection whose backup directory to clean.

Returns

Task<Result<int>>

The number of files deleted.

GetLatestBackupFile(string, string)

Returns the most recent schema backup file for a table, or null if none exists. Backups are named {table}_{yyyyMMdd_HHmmss}.sql in the backup directory.

public string? GetLatestBackupFile(string tableName, string connectionId = "Default")

Parameters

tableName string
connectionId string

Returns

string

RestoreTableSchemaAsync(string, string?, string, CancellationToken)

Restores a table's schema by replaying a backup .sql file: drops the table, then re-runs the captured CREATE TABLE. Destructive and operator-driven — the table's data is lost (only the DDL was ever backed up). When backupFile is null the latest backup is used.

public Task<Result<bool>> RestoreTableSchemaAsync(string tableName, string? backupFile = null, string connectionId = "Default", CancellationToken ct = default)

Parameters

tableName string

The table to restore.

backupFile string

Path to the backup file, or null to use the latest.

connectionId string

The connection ID to use.

ct CancellationToken

Cancellation token.

Returns

Task<Result<bool>>