WireDatabaseBackup class

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 WireDatabaseBackup 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.

Show $var?             Show args?        

Initialization

It’s not typically necessary to call these initialization methods unless doing manual initialization.

Advanced

NameReturnSummary 
$backup->getDatabase()
PDOGet current database connection, initiating the connection if not yet active 
$backup->restoreMerge(string $filename1, string $filename2, array $options)
boolRestore from 2 SQL files while resolving table differences (think of it as array_merge for a DB restore) 

Common

NameReturnSummary 
$backup->dropAllTables()
intDrop all tables from database 

API reference based on ProcessWire core version 3.0.244

Latest news

  • ProcessWire Weekly #559
    The 559th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 25 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK