ProcessWire Database Backup and Restore
// Get an instance of WireDatabaseBackup
$backup = $database->backups();
This class intentionally does not have any external dependencies (other than PDO)so that it can be included by outside tools for restoring/exporting, with the main example of that being the ProcessWire installer.
The recommended way to access these backup methods is via the $database
API variable
method $database->backups()
, which returns a Wire
instance, however
you can also initialize the class manually if you prefer, like this:
// determine where backups will go (should NOT be web accessible)
$backupPath = $config->paths->assets . 'backups/';
// create a new WireDatabaseBackup instance
$backup = new WireDatabaseBackup($backupPath);
// Option 1: set the already-connected DB connection
$backup->setDatabase($this->database);
// Option 2: OR provide a Config object that contains the DB connection info
$backup->setDatabaseConfig($this->config);
Backup the database
$file = $backup->backup();
if($file) {
echo "Backed up to: $file";
} else {
echo "Backup failed: " . implode("<br>", $backup->errors());
}
Restore a database
$success = $backup->restore($file);
if($success) {
echo "Restored database from file: $file";
} else {
echo "Restore failed: " . implode("<br>", $backup->errors());
}
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon.
Actions
Name | Return | Summary | |
---|---|---|---|
$backup->backup() $backup->backup() $backup->backup(array $options = []) | string | Perform a database export/dump | |
$backup->restore() $backup->restore(string $filename) $backup->restore(string $filename, array $options = []) | true | Restore/import a MySQL database dump file |
Reporting
Name | Return | Summary | |
---|---|---|---|
$backup->error() $backup->error() $backup->error(string $str = '') | string | Add an error and return last error | |
$backup->errors() $backup->errors() $backup->errors(bool $reset = false) | array | Return all error messages that occurred | |
$backup->getAllTables() $backup->getAllTables() $backup->getAllTables(bool $count = false, bool $cache = true) | array | Get array of all table names | |
$backup->getFileInfo() $backup->getFileInfo(string $filename) $backup->getFileInfo(string $filename) | array | Get information about a backup file | |
$backup->getFiles() $backup->getFiles() $backup->getFiles(bool $getObjects = false) | array \SplFileInfo | Return array of all backup files | |
$backup->getPath() $backup->getPath() $backup->getPath() | string | Get path where database files are stored | |
$backup->notes() $backup->notes() $backup->notes(bool $reset = false) | array | Get all notes |
Initialization
It’s not typically necessary to call these initialization methods unless doing manual initialization.
Name | Return | Summary | |
---|---|---|---|
$backup->setDatabase() $backup->setDatabase($database) $backup->setDatabase($database) | (nothing) | Set the PDO database connection | |
$backup->setDatabaseConfig() $backup->setDatabaseConfig($config) $backup->setDatabaseConfig($config) | $this | Set the database configuration information | |
$backup->setPath() $backup->setPath(string $path) $backup->setPath(string $path) | $this | Set path where database files are stored |
Advanced
Name | Return | Summary | |
---|---|---|---|
$backup->getDatabase() $backup->getDatabase() $backup->getDatabase() | PDO | Get current database connection, initiating the connection if not yet active | |
$backup->restoreMerge() $backup->restoreMerge(string $filename1, string $filename2, array $options) $backup->restoreMerge(string $filename1, string $filename2, array $options) | bool | Restore from 2 SQL files while resolving table differences (think of it as array_merge for a DB restore) |
Common
Name | Return | Summary | |
---|---|---|---|
$backup->dropAllTables() $backup->dropAllTables() $backup->dropAllTables() | int | Drop all tables from database |
API reference based on ProcessWire core version 3.0.236