PHP Luminova: Backup and Export Module for Databases
Database manager class allows you to create a backup of your database and tables, and export them as a CSV or JSON format.
The Backup & Export Manager class allows you to create backups of your database, database table or export specific table columns as CSV or JSON files. MySQL backups are written under writeable/backups/. CSV and JSON are the formats accepted by the export API.
Class Definition
- Class namespace:
Luminova\Database\Manager - This class implements: Luminova\Interface\LazyObjectInterface
Methods
constructor
Initializes database manager class with an active database driver.
public function __construct(
DatabaseDriverInterface $db,
?string $table = null
)Parameters:
| Parameter | Type | Description |
|---|---|---|
$db | DatabaseDriverInterface | The database driver interface. |
$table | ?string | The name of the table to manage (default null). |
Example:
use Luminova\Database\Connection;
use Luminova\Database\Manager;
$db = (new Connection())->database();
$manager = new Manager($db, 'users');
$manager->backup('users', forTable: true);setTable
Sets the database table to backup or export.
public function setTable(string $table): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$table | string | The database table name. |
Return:
self - Return instance of manager class.
export
Accepts a table selection and download options for a CSV or JSON export.
public function export(string $as = 'csv', ?string $filename = null, array $columns = ['*']): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$as | string | Format to export (e.g, csv or json) (default 'csv'). |
$filename | ?string | The name of the file to download (default null). |
$columns | string[] | The columns to export (default: [*]all columns). |
Return:
bool - Return true on success else false.
Throws:
- Luminova\Exceptions\DatabaseException - If an invalid format is provided or if the export fails.
backup
Creates a backup of the current database or a specific table.
public function backup(?string $filename = null, bool $forTable = false): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$filename | ?string | The name of the backup file (default null). |
$forTable | bool | Whether to back up a specific table or the entire database (default: false). |
Return:
bool - Return true on success else false.
Throws:
- Luminova\Exceptions\DatabaseException - If the backup fails or the directory cannot be created.