public/wordpress/wp-content/plugins/backupbuddy/classes/restore.php
/* swapDatabases()
	 *
	 * ROLLBACK
	 * Swap out the recently imported database tables with temp prefix for the live database.
	 * Sets maintenance mode during the swap, although it should be very brief.
	 *
	 */
swapDatabases()
ROLLBACK
Swap out the recently imported database tables with temp prefix for the live database.
Sets maintenance mode during the swap, although it should be very brief.
swapDatabases () ROLLBACK Swap out the recently imported database tables with temp prefix for the live database . Sets maintenance mode during the swap although it should be very brief .
public function swapDatabases() {
		$this->_before( __FUNCTION__ );
		if ( ( 'rollback' != $this->_state['type'] ) && ( 'deploy' != $this->_state['type'] ) ) { 
			$this->_error( 'This restore type `' . $this->_state['type'] . '` does not support this operation.' );
			return false;
		}
		if ( false === $this->maintenanceOn() ) {
			$this->_error( 'Could not enable maintenance mode.' );
			return false;
		}
		global $wpdb;
		$newPrefix = 'bbnew-' . substr( $this->_state['serial'], 0, 4 ) . '_'; 
		$oldPrefix = 'bbold-' . substr( $this->_state['serial'], 0, 4 ) . '_'; 
		pb_backupbuddy::status( 'details', 'Checking for newly imported rollback tables with temp prefix `' . $newPrefix . '`.' );
		$sql = "SELECT table_name FROM information_schema.tables WHERE table_name LIKE '" . str_replace( '_', '\_', $newPrefix ) . "%' AND table_schema = DATABASE()";
		$results = $wpdb->get_results( $sql, ARRAY_A );
		pb_backupbuddy::status( 'details', 'Found ' . count( $results ) . ' matching tables.' );
		if ( 0 == count( $results ) ) {
			$this->_error( 'Error getting tables or none found. SQL Query: ' . htmlentities( $sql ) );
			return false;
		}
		pb_backupbuddy::status( 'details', 'Rename all existing tables with this temp prefix to prefix `' . $wpdb->prefix . '`.' );
		foreach( $results as $result ) {
			$newTableName = str_replace( $newPrefix, '', $wpdb->prefix . $result['table_name'] ); 
			$oldTableName = $oldPrefix . $newTableName; 
			$sql = "SHOW TABLES LIKE '" . backupbuddy_core::dbEscape( $newTableName ) . "';";
			pb_backupbuddy::status( 'details', 'SQL: `' . $sql . '`.' );
			$results = $wpdb->get_results( $sql, ARRAY_A );
			if ( count( $results ) > 0 ) { 
				pb_backupbuddy::status( 'details', 'Renaming table `' . $newTableName . '` (already exists in this site) to `' . $oldTableName . '`.' );
				if ( false === $wpdb->query( "RENAME TABLE `" . backupbuddy_core::dbEscape( $newTableName ) . "` TO `" . backupbuddy_core::dbEscape( $oldTableName ) . "`;" ) ) {
					$this->_error( 'Error #844389a: Unable to rename table `' . $newTableName . '` to `' . $oldTableName . '`. Details: `' . $wpdb->last_error . '`.' );
					return false;
				}
			} else {
				pb_backupbuddy::status( 'details', 'Table `' . $newTableName . '` did not already exist so NOT renaming to `' . $oldTableName . '`.' );
			}
			pb_backupbuddy::status( 'details', 'Renaming incoming table `' . $result['table_name'] . '` to `' . $newTableName . '`.' );
			if ( false === $wpdb->query( "RENAME TABLE `" . backupbuddy_core::dbEscape( $result['table_name'] ) . "` TO `" . backupbuddy_core::dbEscape( $newTableName ) . "`;" ) ) {
				$this->_error( 'Error #844389b: Unable to rename table `' . $result['table_name'] . '` to `' . $newTableName . '`. Details: `' . $wpdb->last_error . '`.' );
				return false;
			}
		} 
		$this->maintenanceOff();
		return true;
	}
public function swapDatabases ( ) { $ this -> _before ( __FUNCTION__ ) ; if ( ( 'rollback' != $ this -> _state [ 'type' ] ) && ( 'deploy' != $ this -> _state [ 'type' ] ) ) { $ this -> _error ( 'This restore type `' . $ this -> _state [ 'type' ] . '` does not support this operation.' ) ; return false ; } if ( false === $ this -> maintenanceOn ( ) ) { $ this -> _error ( 'Could not enable maintenance mode.' ) ; return false ; } global $ wpdb ; $ newPrefix = 'bbnew-' . substr ( $ this -> _state [ 'serial' ] , 0 , 4 ) . '_' ; $ oldPrefix = 'bbold-' . substr ( $ this -> _state [ 'serial' ] , 0 , 4 ) . '_' ; pb_backupbuddy :: status ( 'details' , 'Checking for newly imported rollback tables with temp prefix `' . $ newPrefix . '`.' ) ; $ sql = " SELECT table_name FROM information_schema.tables WHERE table_name LIKE ' " . str_replace ( '_' , '\_' , $ newPrefix ) . " %' AND table_schema = DATABASE() " ; $ results = $ wpdb -> get_results ( $ sql , ARRAY_A ) ; pb_backupbuddy :: status ( 'details' , 'Found ' . count ( $ results ) . ' matching tables.' ) ; if ( 0 == count ( $ results ) ) { $ this -> _error ( 'Error getting tables or none found. SQL Query: ' . htmlentities ( $ sql ) ) ; return false ; } pb_backupbuddy :: status ( 'details' , 'Rename all existing tables with this temp prefix to prefix `' . $ wpdb -> prefix . '`.' ) ; foreach ( $ results as $ result ) { $ newTableName = str_replace ( $ newPrefix , '' , $ wpdb -> prefix . $ result [ 'table_name' ] ) ; $ oldTableName = $ oldPrefix . $ newTableName ; $ sql = " SHOW TABLES LIKE ' " . backupbuddy_core :: dbEscape ( $ newTableName ) . " '; " ; pb_backupbuddy :: status ( 'details' , 'SQL: `' . $ sql . '`.' ) ; $ results = $ wpdb -> get_results ( $ sql , ARRAY_A ) ; if ( count ( $ results ) > 0 ) { pb_backupbuddy :: status ( 'details' , 'Renaming table `' . $ newTableName . '` (already exists in this site) to `' . $ oldTableName . '`.' ) ; if ( false === $ wpdb -> query ( " RENAME TABLE ` " . backupbuddy_core :: dbEscape ( $ newTableName ) . " ` TO ` " . backupbuddy_core :: dbEscape ( $ oldTableName ) . " `; " ) ) { $ this -> _error ( 'Error #844389a: Unable to rename table `' . $ newTableName . '` to `' . $ oldTableName . '`. Details: `' . $ wpdb -> last_error . '`.' ) ; return false ; } } else { pb_backupbuddy :: status ( 'details' , 'Table `' . $ newTableName . '` did not already exist so NOT renaming to `' . $ oldTableName . '`.' ) ; } pb_backupbuddy :: status ( 'details' , 'Renaming incoming table `' . $ result [ 'table_name' ] . '` to `' . $ newTableName . '`.' ) ; if ( false === $ wpdb -> query ( " RENAME TABLE ` " . backupbuddy_core :: dbEscape ( $ result [ 'table_name' ] ) . " ` TO ` " . backupbuddy_core :: dbEscape ( $ newTableName ) . " `; " ) ) { $ this -> _error ( 'Error #844389b: Unable to rename table `' . $ result [ 'table_name' ] . '` to `' . $ newTableName . '`. Details: `' . $ wpdb -> last_error . '`.' ) ; return false ; } } $ this -> maintenanceOff ( ) ; return true ; }
swapDatabases()
 ROLLBACK
 Swap out the recently imported database tables with temp prefix for the live database.
swapDatabases () ROLLBACK Swap out the recently imported database tables with temp prefix for the live database .
// Restore mode used for restorying during deployment. // Turn on maintenance mode. // Calculate temporary table prefixes. // Temp prefix for holding the NEWly imported data. // Temp prefix for holding the OLD (currently live) data. // Get newly imported tables with the temp prefix. // Rename newly imported tables with temp prefix, renaming the existing live table first. // the target new table name we are importing. // the target name for the old table where we hold it in case it needs undoing. // Check if existing site already had this table. If so then we will need to rename it to a temp table name to allow for undoing. // This table existed in the existing site so it needs to be renamed. // Rename imported table to live prefix. // end foreach. // Turn off maintenance mode.