| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
TODO
|
| 5 |
|
| 6 |
Dump each table's schema as a file (table_name_schema)
|
| 7 |
Dump each table's data as a file (table_name_data)
|
| 8 |
Have flag to dump as single file, or restore from single file
|
| 9 |
|
| 10 |
Have a config setting to store sequences seperately by user
|
| 11 |
Grab sequences as last line of file?
|
| 12 |
Check if table auto increments next highest
|
| 13 |
|
| 14 |
Merge by copying schema and data files for non-merge content
|
| 15 |
Merge content as normal
|
| 16 |
Restore&Dump only modified/merged tables
|
| 17 |
check diff, and flag to be restored and dumped
|
| 18 |
(Double check if this is still nessisary if data files are seperate)
|
| 19 |
|
| 20 |
Have script to complete merge process after conflict (merge.php finish)
|
| 21 |
**/
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Get the database connection settings from settings.php file
|
| 25 |
*/
|
| 26 |
function dbscripts_db_connect() {
|
| 27 |
require('config.inc');
|
| 28 |
require("$settings_path/settings.php");
|
| 29 |
|
| 30 |
preg_match('/'.$dbtype.':\/\/([^:]+):([^@]+)@([^\/]+)\/(.+)/', $db_url, $db_settings);
|
| 31 |
|
| 32 |
if (empty($db_settings)) {
|
| 33 |
return FALSE;
|
| 34 |
} else {
|
| 35 |
|
| 36 |
$dbuser = $db_settings[1];
|
| 37 |
$dbpassword = $db_settings[2];
|
| 38 |
$dbhost = $db_settings[3];
|
| 39 |
$dbname = $db_settings[4];
|
| 40 |
|
| 41 |
return "-u $dbuser -p$dbpassword -h $dbhost $dbname";
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Get options for dump file and filtering
|
| 48 |
*
|
| 49 |
* @param $argv
|
| 50 |
* Pass in the $_SERVER['argv'] variable to return options set when running
|
| 51 |
* the script.
|
| 52 |
*/
|
| 53 |
function dbscripts_get_options($argv) {
|
| 54 |
$use_default = FALSE;
|
| 55 |
if(isset($argv[1])) {
|
| 56 |
if(in_array($argv[1],array('min','none','full'))) {
|
| 57 |
$use_default = TRUE;
|
| 58 |
}
|
| 59 |
} else {
|
| 60 |
$use_default = TRUE;
|
| 61 |
}
|
| 62 |
|
| 63 |
if ($use_default) {
|
| 64 |
$options['file'] = 'development';
|
| 65 |
$filter_options = isset($argv[1]) ? $argv[1] : '';
|
| 66 |
} else {
|
| 67 |
$options['file'] = $argv[1];
|
| 68 |
$filter_options = isset($argv[2]) ? $argv[2] : '';
|
| 69 |
}
|
| 70 |
|
| 71 |
switch ($filter_options) {
|
| 72 |
case 'min':
|
| 73 |
$options['filter'] = 'min';
|
| 74 |
break;
|
| 75 |
|
| 76 |
case 'none':
|
| 77 |
$options['filter'] = 'none';
|
| 78 |
break;
|
| 79 |
|
| 80 |
default:
|
| 81 |
$options['filter'] = 'full';
|
| 82 |
break;
|
| 83 |
}
|
| 84 |
|
| 85 |
$options['sequences'] = FALSE;
|
| 86 |
if (in_array('sequences',$argv)) {
|
| 87 |
$options['sequences'] = TRUE;
|
| 88 |
}
|
| 89 |
|
| 90 |
return $options;
|
| 91 |
}
|
| 92 |
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Return help for each script
|
| 96 |
*
|
| 97 |
* @param $script
|
| 98 |
* Available options are 'dump', 'restore', 'erase' and 'merge'.
|
| 99 |
*/
|
| 100 |
function dbscripts_help($script) {
|
| 101 |
require('config.inc');
|
| 102 |
|
| 103 |
switch ($script) {
|
| 104 |
|
| 105 |
// Dump help
|
| 106 |
case 'dump':
|
| 107 |
$help = <<<EOF
|
| 108 |
|
| 109 |
NAME
|
| 110 |
Dump - database dump script
|
| 111 |
|
| 112 |
SYNOPSIS
|
| 113 |
dump.php [dump-file] [filter-option]
|
| 114 |
|
| 115 |
EXAMPLE USAGE
|
| 116 |
development: dump.php
|
| 117 |
production: dump.php production min
|
| 118 |
last merge: dump.php last-merge
|
| 119 |
|
| 120 |
DESCRIPTION
|
| 121 |
Dump the database from MySQL to a given file. The dump is 'diffable'
|
| 122 |
allowing it to be under version control. Filtering options are performed
|
| 123 |
to avoid dumping data that is not nessisary to be stored from development
|
| 124 |
and production environments.
|
| 125 |
|
| 126 |
OPTIONS
|
| 127 |
dump-file
|
| 128 |
File the database should be dumped to. Default options are
|
| 129 |
'development', 'production' and 'last-merge'. Other filename patterns
|
| 130 |
may be used if you wish to dump the database to an alternat location.
|
| 131 |
Relative to: {$file_path}
|
| 132 |
Defaults to: {$file_path}/development.sql
|
| 133 |
|
| 134 |
filter-option
|
| 135 |
The level of filtering that should be performed during the dump.
|
| 136 |
full
|
| 137 |
Removes the full option of data from the dump. Most useful for
|
| 138 |
removing user, sessions and cache data. Recomended to be used
|
| 139 |
when dumping a development database
|
| 140 |
min
|
| 141 |
Removes only a select minimum of data from the dump. Most
|
| 142 |
useful for removing sessions and cache data. Recommended to be
|
| 143 |
used when dumping a production database.
|
| 144 |
none
|
| 145 |
NO filtering is performed whatsoever. The entire database is
|
| 146 |
dumped to the given file.
|
| 147 |
Defaults to: full
|
| 148 |
|
| 149 |
FILES
|
| 150 |
{$script_path}/config.inc
|
| 151 |
Configuration settings for customizing these scripts for your
|
| 152 |
environment.
|
| 153 |
|
| 154 |
AUTHOR
|
| 155 |
Kathleen Murtagh <kathleen@ceardach.com>
|
| 156 |
http://drupal.org/user/79082
|
| 157 |
|
| 158 |
SEE ALSO
|
| 159 |
dbscripts documentation
|
| 160 |
http://drupal.org/node/232151
|
| 161 |
dump documentation
|
| 162 |
http://drupal.org/node/232153
|
| 163 |
\n
|
| 164 |
EOF;
|
| 165 |
break;
|
| 166 |
|
| 167 |
// Restore help
|
| 168 |
case 'restore':
|
| 169 |
$help = <<<EOF
|
| 170 |
|
| 171 |
NAME
|
| 172 |
Restore - database restore script
|
| 173 |
|
| 174 |
SYNOPSIS
|
| 175 |
restore.php [file] [filter-option] [sequences]
|
| 176 |
|
| 177 |
EXAMPLE USAGE
|
| 178 |
development: restore.php
|
| 179 |
production: restore.php production min
|
| 180 |
last merge: restore.php last-merge
|
| 181 |
|
| 182 |
DESCRIPTION
|
| 183 |
Restores the database from a given file. Filtering options are performed
|
| 184 |
to prevent certain data from being lost in the MySQL database in
|
| 185 |
development and production environments. Also performs the 'erase' script
|
| 186 |
to ensure that any tables removed in development are removed when performing
|
| 187 |
a restore.
|
| 188 |
|
| 189 |
OPTIONS
|
| 190 |
file
|
| 191 |
The file the database should be restored from. Default options are
|
| 192 |
'development', 'production' and 'last-merge'. Other filename patterns
|
| 193 |
may be used if you wish to restore the database from an alternate file.
|
| 194 |
Relative to: {$file_path}
|
| 195 |
Defaults to: {$file_path}/development.sql
|
| 196 |
|
| 197 |
filter-option
|
| 198 |
The level of filtering that should be performed during the restore to
|
| 199 |
allow some data to be preserved in the MySQL database.
|
| 200 |
full
|
| 201 |
Preserves the full option of data currently residing within the
|
| 202 |
MySQL database. Most useful for preserving user, sessions and
|
| 203 |
cache data. Recommended to use this when restoring within a
|
| 204 |
development environment.
|
| 205 |
min
|
| 206 |
Preserves only a select minimum of data currently residing
|
| 207 |
within the MySQL database. Most useful for preserving sessions
|
| 208 |
and cache data. Recommended to use this when restoring within a
|
| 209 |
production environment.
|
| 210 |
none
|
| 211 |
NO filtering is performed whatsoever. The entire database is
|
| 212 |
erased from MySQL and restored with only the data from the given
|
| 213 |
file.
|
| 214 |
Defaults to: full
|
| 215 |
|
| 216 |
sequences
|
| 217 |
Set this option if sequences should be preserved within the MySQL
|
| 218 |
database. This is a special case scenario that is only used during the
|
| 219 |
merge script.
|
| 220 |
|
| 221 |
FILES
|
| 222 |
{$script_path}/config.inc
|
| 223 |
Configuration settings for customizing these scripts for your
|
| 224 |
environment.
|
| 225 |
|
| 226 |
AUTHOR
|
| 227 |
Kathleen Murtagh <kathleen@ceardach.com>
|
| 228 |
http://drupal.org/user/79082
|
| 229 |
|
| 230 |
SEE ALSO
|
| 231 |
dbscripts documentation
|
| 232 |
http://drupal.org/node/232151
|
| 233 |
restore and erase documentation
|
| 234 |
http://drupal.org/node/232154
|
| 235 |
\n
|
| 236 |
EOF;
|
| 237 |
break;
|
| 238 |
|
| 239 |
// Erase help
|
| 240 |
case 'erase':
|
| 241 |
$help = <<<EOF
|
| 242 |
|
| 243 |
NAME
|
| 244 |
Erase - database erase script
|
| 245 |
|
| 246 |
SYNOPSIS
|
| 247 |
erase.php [filter-option] [sequences]
|
| 248 |
|
| 249 |
EXAMPLE USAGE
|
| 250 |
development: erase.php
|
| 251 |
production: erase.php min
|
| 252 |
|
| 253 |
DESCRIPTION
|
| 254 |
Erases the database within the MySQL database. Filtering options are
|
| 255 |
performed to prevent erasing data that should be kept.
|
| 256 |
|
| 257 |
OPTIONS
|
| 258 |
filter-option
|
| 259 |
The level of filtering that shold be performed during the erasure.
|
| 260 |
full
|
| 261 |
Preserves the full option of data within the MySQL database.
|
| 262 |
Most useful for keeping user, sessions and cache data.
|
| 263 |
Recommended to be used when erasing within a development
|
| 264 |
environment.
|
| 265 |
min
|
| 266 |
Preserves only the select minimum of data within the MySQL
|
| 267 |
database. Most useful for preserving sessions data so you do
|
| 268 |
not get logged out. Recommended to be used when erasing within
|
| 269 |
a production environment.
|
| 270 |
none
|
| 271 |
NO filtering is performed whatsoever. The entire database will
|
| 272 |
be erased.
|
| 273 |
Defaults to: full
|
| 274 |
|
| 275 |
sequences
|
| 276 |
Set this option if sequences should be preserved within the MySQL
|
| 277 |
database. This is a spcial case scenario that is only used during the
|
| 278 |
merge script.
|
| 279 |
|
| 280 |
FILES
|
| 281 |
{$script_path}/config.inc
|
| 282 |
Configuration settings for customizing these scripts for your
|
| 283 |
environment.
|
| 284 |
|
| 285 |
AUTHOR
|
| 286 |
Kathleen Murtagh <kathleen@ceardach.com>
|
| 287 |
http://drupal.org/user/79082
|
| 288 |
|
| 289 |
SEE ALSO
|
| 290 |
dbscripts documentation
|
| 291 |
http://drupal.org/node/232151
|
| 292 |
merge documentation
|
| 293 |
http://drupal.org/node/232154
|
| 294 |
\n
|
| 295 |
EOF;
|
| 296 |
break;
|
| 297 |
|
| 298 |
case 'merge':
|
| 299 |
$help = <<<EOF
|
| 300 |
|
| 301 |
NAME
|
| 302 |
Merge - database merge script
|
| 303 |
|
| 304 |
SYNOPSIS
|
| 305 |
merge.php [dev-db] [last-merge-db] [prod-db]
|
| 306 |
|
| 307 |
EXAMPLE USAGE
|
| 308 |
default: merge.php
|
| 309 |
|
| 310 |
DESCRIPTION
|
| 311 |
Merge a development and production database together.
|
| 312 |
|
| 313 |
OPTIONS
|
| 314 |
dev-db
|
| 315 |
The location of the database file used for development. Only configuration
|
| 316 |
and content changes will be preserved from this file. All user data
|
| 317 |
will be lost.
|
| 318 |
Relative to: {$file_path}
|
| 319 |
Defaults to: {$file_path}/development.sql
|
| 320 |
|
| 321 |
last-merge-db
|
| 322 |
The location of the database file that was used to represent the state
|
| 323 |
the last time both production development were merged. This is
|
| 324 |
important to be able to track the difference between an addition and a
|
| 325 |
subtraction.
|
| 326 |
Relative to: {$file_path}
|
| 327 |
Defaults to: {$file_path}/last-merge.sql
|
| 328 |
|
| 329 |
prod-db
|
| 330 |
The location of the database file used for production. Only content and
|
| 331 |
user data will be preserved from this file. All other data will be
|
| 332 |
lost.
|
| 333 |
Relative to: {$file_path}
|
| 334 |
defaults to: {$file_path}/production.sql
|
| 335 |
|
| 336 |
FILES
|
| 337 |
{$script_path}/config.inc
|
| 338 |
Configuration settings for customizing these scripts for your
|
| 339 |
environment.
|
| 340 |
|
| 341 |
AUTHOR
|
| 342 |
Kathleen Murtagh <kathleen@ceardach.com>
|
| 343 |
http://drupal.org/user/79082
|
| 344 |
|
| 345 |
SEE ALSO
|
| 346 |
dbscripts documentation
|
| 347 |
http://drupal.org/node/232151
|
| 348 |
merge documentation
|
| 349 |
http://drupal.org/node/232155
|
| 350 |
\n
|
| 351 |
EOF;
|
| 352 |
break;
|
| 353 |
|
| 354 |
default:
|
| 355 |
$help = 'That is not a valid script.'."\n";
|
| 356 |
break;
|
| 357 |
}
|
| 358 |
|
| 359 |
return $help;
|
| 360 |
}
|
| 361 |
|
| 362 |
|
| 363 |
/**
|
| 364 |
* Dump the database
|
| 365 |
*
|
| 366 |
* @param $file
|
| 367 |
* File the database should be dumped to. Defaults to 'development'.
|
| 368 |
* Alternative options that are supported by default are 'production' and
|
| 369 |
* 'last-merge'. Other filename patterns may be used if you wish to dump the
|
| 370 |
* the database to an alternate location.
|
| 371 |
* @param $filter_option
|
| 372 |
* Choose what level of filtering should be performed during the dump. Filter
|
| 373 |
* levels are configured in config.inc
|
| 374 |
* 'full' - Default option. Removes the full option of data from the dump.
|
| 375 |
* Most useful for removing user, sessions and cache data. Use this when
|
| 376 |
* dumping a development database.
|
| 377 |
* 'min' - Removes only a select minimum of data from the dump. Most useful
|
| 378 |
* for removing sessions and cache data. Use this when dumping a
|
| 379 |
* production database.
|
| 380 |
* 'none' - NO filtering is performed whatsoever. The entire database is
|
| 381 |
* dumped to the given file.
|
| 382 |
*/
|
| 383 |
function dbscripts_dump($file = 'development', $filter_option = 'full') {
|
| 384 |
require('config.inc');
|
| 385 |
|
| 386 |
$db_connection_settings = dbscripts_db_connect();
|
| 387 |
if (!$db_connection_settings) {
|
| 388 |
return "\nImproper database connection settings.\n\n";
|
| 389 |
exit;
|
| 390 |
}
|
| 391 |
|
| 392 |
// Set the file to dump to
|
| 393 |
$dump_file = $file.'.sql';
|
| 394 |
|
| 395 |
// Create the dump location if it doesn't exist
|
| 396 |
if(!file_exists($file_path)) {
|
| 397 |
exec("mkdir $file_path");
|
| 398 |
}
|
| 399 |
|
| 400 |
// Set the filtering options and message to be presented to the user
|
| 401 |
switch($filter_option){
|
| 402 |
case 'none':
|
| 403 |
$tables_trashed = array();
|
| 404 |
$tables_preserved = array();
|
| 405 |
$message = "Performed a full database dump to $dump_file.";
|
| 406 |
break;
|
| 407 |
|
| 408 |
case 'min':
|
| 409 |
$tables_trashed = $tables_filtered;
|
| 410 |
$tables_preserved = $tables_filtered_l1;
|
| 411 |
$message = "Dumped the database to $file with minimal filtering.";
|
| 412 |
break;
|
| 413 |
|
| 414 |
default:
|
| 415 |
$tables_trashed = $tables_filtered;
|
| 416 |
$tables_preserved = array_merge($tables_filtered_l1,$tables_filtered_l2);
|
| 417 |
$message = "Dumped the database to $file with full filtering options.";
|
| 418 |
break;
|
| 419 |
}
|
| 420 |
|
| 421 |
$filter = '';
|
| 422 |
foreach($tables_trashed as $filtered_data) {
|
| 423 |
$filter .= "|grep -v 'INSERT INTO .".$filtered_data.".' ";
|
| 424 |
}
|
| 425 |
foreach($tables_preserved as $preserved_data) {
|
| 426 |
$filter .= "|grep -v 'DROP TABLE IF EXISTS .".$preserved_data.".;' |sed 's/CREATE TABLE .".$preserved_data."./CREATE TABLE IF NOT EXISTS `".$preserved_data."`/g' |grep -v 'INSERT INTO .".$preserved_data.".' ";
|
| 427 |
}
|
| 428 |
|
| 429 |
// These are all the special dump options that make the database diffable
|
| 430 |
$dump_options = "--skip-opt --add-drop-table --add-locks --create-options --quick --lock-tables --set-charset --disable-keys --order-by-primary --comments=FALSE --default-character-set=utf8 --character-sets-dir=$charsets --hex-blob";
|
| 431 |
|
| 432 |
// and finally, lets dump the database to a temporary file
|
| 433 |
exec("$mysqldump $dump_options $db_connection_settings $filter > $file_path/temp");
|
| 434 |
|
| 435 |
// check that the file actually has data
|
| 436 |
if (file_get_contents("$file_path/temp") == '') {
|
| 437 |
$message = "Empty data returned. There may be a problem connecting to the database.";
|
| 438 |
exec("rm $file_path/temp");
|
| 439 |
} else {
|
| 440 |
exec("mv $file_path/temp $file_path/$dump_file");
|
| 441 |
}
|
| 442 |
|
| 443 |
// let the user know what happened
|
| 444 |
return "\n$message\n\n";
|
| 445 |
}
|
| 446 |
|
| 447 |
|
| 448 |
/*
|
| 449 |
* Erase the database
|
| 450 |
*
|
| 451 |
* @param $filter_option
|
| 452 |
* Choose what level of filtering should be performed while erasing the
|
| 453 |
* database. Filter levels are configured in config.inc. Any options set to
|
| 454 |
* be filtered will be subsequently PRESERVED in the database, and not erased.
|
| 455 |
* 'full' - Default option. Preserves the full option of data from the dump.
|
| 456 |
* Most useful for keeping user, sessions and cache data. Use this when
|
| 457 |
* erasing a development database.
|
| 458 |
* 'min' - Preserves only a select minimum of data from the dump. Most useful
|
| 459 |
* for keeping sessions data so you don't get logged out. Use this when
|
| 460 |
* erasing a production database.
|
| 461 |
* 'none' - NO filtering is performed whatsoever. The entire database will be
|
| 462 |
* erased.
|
| 463 |
* @param $sequences
|
| 464 |
* Set if sequences should be preserved or not. This is a special case
|
| 465 |
* scenario that is only used during the merge function.
|
| 466 |
*/
|
| 467 |
function dbscripts_erase($filter_option = 'full', $sequences = FALSE) {
|
| 468 |
require('config.inc');
|
| 469 |
|
| 470 |
$db_connection_settings = dbscripts_db_connect();
|
| 471 |
if (!$db_connection_settings) {
|
| 472 |
return "\nImproper database connection settings.\n\n";
|
| 473 |
exit;
|
| 474 |
}
|
| 475 |
|
| 476 |
// Set the filtering options and message to be presented to the user
|
| 477 |
switch ($filter_option) {
|
| 478 |
case 'min':
|
| 479 |
$tables_preserved = $tables_filtered_l1;
|
| 480 |
$message = "Erased the database, except for minimal preserved tables.";
|
| 481 |
break;
|
| 482 |
|
| 483 |
case 'none';
|
| 484 |
$tables_preserved = array();
|
| 485 |
$message = "Erased the entire database.";
|
| 486 |
break;
|
| 487 |
|
| 488 |
default:
|
| 489 |
$tables_preserved = array_merge($tables_filtered_l1,$tables_filtered_l2);
|
| 490 |
$message = "Erased the database, except for preserved tables.";
|
| 491 |
break;
|
| 492 |
}
|
| 493 |
|
| 494 |
$filter = '';
|
| 495 |
if ($sequences) {
|
| 496 |
$tables_preserved[] = 'sequences';
|
| 497 |
}
|
| 498 |
foreach($tables_preserved as $preserved_data) {
|
| 499 |
$filter .= "|grep -v 'DROP TABLE IF EXISTS .$preserved_data.;'";
|
| 500 |
}
|
| 501 |
|
| 502 |
// Dump options to make it easy to erase the database
|
| 503 |
$dump_options = "--add-drop-table --no-data";
|
| 504 |
|
| 505 |
// We'll dump the database, perform some changes to it, then pipe it back into
|
| 506 |
// MySQL so it will drop the given tables
|
| 507 |
exec("$mysqldump $dump_options $db_connection_settings | grep 'DROP TABLE' $filter | $mysql $db_connection_settings");
|
| 508 |
|
| 509 |
return "\n$message\n\n";
|
| 510 |
}
|
| 511 |
|
| 512 |
|
| 513 |
/**
|
| 514 |
* Restore the database
|
| 515 |
*
|
| 516 |
* @param $file
|
| 517 |
* File the database should be restored from. Defaults to 'development'.
|
| 518 |
* Alternative options that are supported by default are 'production' and
|
| 519 |
* 'last-merge'. Other filename patterns may be used if you wish to restore
|
| 520 |
* the database from an alternate location.
|
| 521 |
* @param $filter_option
|
| 522 |
* Choose what level of filtering should be performed during the restoration.
|
| 523 |
* Filter levels are configured in config.inc
|
| 524 |
* 'full' - Default option. Preserves the full option of data currently
|
| 525 |
* residing within MySQL. Most useful for preserving user, sessions and
|
| 526 |
* cache data. Use this when restoring within a development environment.
|
| 527 |
* 'min' - Preserves only a select minimum of data currently residing within
|
| 528 |
* MySQL. Most useful for preserving sessions and cache data. Use this
|
| 529 |
* when restoring within a production environment.
|
| 530 |
* 'none' - NO filtering is performed whatsoever. The entire database is
|
| 531 |
* erased from MySQL and restored with the data from the given file.
|
| 532 |
* @param $sequences
|
| 533 |
* Set if sequences should be preserved or not. This is a special case
|
| 534 |
* scenario that is only used during the merge function.
|
| 535 |
*/
|
| 536 |
function dbscripts_restore($file = 'development', $filter_option = 'full', $sequences = FALSE) {
|
| 537 |
require('config.inc');
|
| 538 |
|
| 539 |
$db_connection_settings = dbscripts_db_connect();
|
| 540 |
if (!$db_connection_settings) {
|
| 541 |
return "\nImproper database connection settings.\n\n";
|
| 542 |
exit;
|
| 543 |
}
|
| 544 |
|
| 545 |
// Set the file to restore from
|
| 546 |
$dump_file = $file.'.sql';
|
| 547 |
|
| 548 |
// Check that the requested file exists
|
| 549 |
if (!file_exists("$file_path/$dump_file")) {
|
| 550 |
return "\nThe file does not exist: $file_path/$dump_file\n\n";
|
| 551 |
}
|
| 552 |
|
| 553 |
// If sequences need to be restored, we have to create a temp file
|
| 554 |
$sequences_message = '';
|
| 555 |
if ($sequences) {
|
| 556 |
// Create a temp file with sequences striped out
|
| 557 |
exec("grep -v 'DROP TABLE IF EXISTS .sequences.;' $file_path/$dump_file |sed 's/CREATE TABLE .sequences./CREATE TABLE IF NOT EXISTS `sequences`/g' |grep -v 'INSERT INTO .sequences. VALUES' > $file_path/tmp/database.tmp");
|
| 558 |
$dump_file = '/tmp/database.tmp'; // reset dump_file to the temp version
|
| 559 |
$sequences_message = 'including sequences';
|
| 560 |
}
|
| 561 |
|
| 562 |
// Erase the database so any tables that were removed stay removed
|
| 563 |
dbscripts_erase($filter_option,$sequences);
|
| 564 |
|
| 565 |
// Set the message
|
| 566 |
switch($filter_option){
|
| 567 |
case 'none':
|
| 568 |
$message = "Restored the full database $sequences_message";
|
| 569 |
break;
|
| 570 |
|
| 571 |
case 'min':
|
| 572 |
$message = "Restored the database perserving minimal tables $sequences_message";
|
| 573 |
break;
|
| 574 |
|
| 575 |
default:
|
| 576 |
$message = "Restored the database preserving the full option of tables $sequences_message";
|
| 577 |
break;
|
| 578 |
}
|
| 579 |
|
| 580 |
// Restore the database and let the user know what happened
|
| 581 |
exec("$mysql $db_connection_settings < $file_path/$dump_file");
|
| 582 |
|
| 583 |
// Erase the temp file if it was created
|
| 584 |
if ($sequences) {
|
| 585 |
exec("rm $file_path/tmp/database.tmp");
|
| 586 |
}
|
| 587 |
|
| 588 |
return "\n$message\n\n";
|
| 589 |
}
|
| 590 |
|
| 591 |
|
| 592 |
/**
|
| 593 |
* Merge databases
|
| 594 |
*
|
| 595 |
* You really only need to set the following parameters for expert use.
|
| 596 |
* Otherwise this function can be run with no options.
|
| 597 |
*
|
| 598 |
* @param $dev_db
|
| 599 |
* Location of the database file that was used for 'development'. Only
|
| 600 |
* configuration and content changes will be preserved. All user data will
|
| 601 |
* be lost.
|
| 602 |
* @param $lastmerge_db
|
| 603 |
* Location of the database file that was used to represent the state the
|
| 604 |
* last time both production and development were merged. This is important
|
| 605 |
* to be able to track the difference between an addition and a subtraction.
|
| 606 |
* @param $prod_db
|
| 607 |
* Location of the database file that is used as 'production'. Only content
|
| 608 |
* and user data will be perserved. All other data will be lost.
|
| 609 |
*/
|
| 610 |
function dbscripts_merge($dev_db = 'development', $lastmerge_db = 'last-merge', $prod_db = 'production') {
|
| 611 |
require('config.inc');
|
| 612 |
|
| 613 |
// If empty data gets sent through, reset it
|
| 614 |
if (!$dev_db) {
|
| 615 |
$dev_db = 'development';
|
| 616 |
}
|
| 617 |
if (!$lastmerge_db) {
|
| 618 |
$lastmerge_db = 'last-merge';
|
| 619 |
}
|
| 620 |
if (!$prod_db) {
|
| 621 |
$prod_db = 'production';
|
| 622 |
}
|
| 623 |
|
| 624 |
// Ensure all the files exist
|
| 625 |
$missing_db = '';
|
| 626 |
if (!file_exists("$file_path/$dev_db.sql")) {
|
| 627 |
$missing_db .= $dev_db.'.sql ';
|
| 628 |
}
|
| 629 |
if (!file_exists("$file_path/$lastmerge_db.sql")) {
|
| 630 |
$missing_db .= $lastmerge_db.'.sql ';
|
| 631 |
}
|
| 632 |
if (!file_exists("$file_path/$prod_db.sql")) {
|
| 633 |
$missing_db .= $prod_db.'.sql ';
|
| 634 |
}
|
| 635 |
|
| 636 |
// Cancel merge if all files do not exist
|
| 637 |
if ($missing_db) {
|
| 638 |
return "\nMissing database files for merge: $missing_db\n\n";
|
| 639 |
}
|
| 640 |
|
| 641 |
// Ensure tmp folder exists
|
| 642 |
if (!file_exists($file_path.'/tmp')) {
|
| 643 |
exec("mkdir $file_path/tmp");
|
| 644 |
}
|
| 645 |
|
| 646 |
// After a conflict, the temp files would still exist
|
| 647 |
if(file_exists("$file_path/tmp/data_merged.sql")){
|
| 648 |
exec("rm $file_path/tmp/*");
|
| 649 |
}
|
| 650 |
|
| 651 |
// Adding status indicators, because when it takes 15 minutes, it's nice to
|
| 652 |
// know where you're at in the process.
|
| 653 |
print "\nRestoring production database...";
|
| 654 |
|
| 655 |
// To store production sequences, the full original production version of the
|
| 656 |
// database will be loaded into MySQL.
|
| 657 |
dbscripts_restore($prod_db,'min');
|
| 658 |
|
| 659 |
print " Done.";
|
| 660 |
|
| 661 |
//Set up ability to strip data from tables
|
| 662 |
$strip_merge = '';
|
| 663 |
foreach($tables_merge as $table){
|
| 664 |
$strip_content .= "|grep -v 'INSERT INTO .".$table.". VALUES' ";
|
| 665 |
}
|
| 666 |
$strip_override = '';
|
| 667 |
foreach($tables_override as $table){
|
| 668 |
$strip_users .= "|grep -v 'INSERT INTO .".$table.". VALUES' ";
|
| 669 |
}
|
| 670 |
|
| 671 |
// Set up ability to strip SQL comments
|
| 672 |
$strip_comment = '';
|
| 673 |
foreach($sql_comment_unset as $comment){
|
| 674 |
$strip_comment .= "|grep -v '".$comment."' ";
|
| 675 |
}
|
| 676 |
|
| 677 |
// To ensure that the newly merged databases get imported into the database
|
| 678 |
// correctly, tables that were modified to not be dropped in development,
|
| 679 |
// need to be reversed
|
| 680 |
$reverse_filter = '';
|
| 681 |
$tables_preserved = array_merge($tables_filtered_l1,$tables_filtered_l2);
|
| 682 |
foreach($tables_preserved as $table) {
|
| 683 |
$reverse_filter .= "|sed 's/CREATE TABLE IF NOT EXISTS .".$table.". (/DROP TABLE IF EXISTS `".$table."`; CREATE TABLE `".$table."` (/g' ";
|
| 684 |
}
|
| 685 |
|
| 686 |
print "\n\nPreparing temporary files...";
|
| 687 |
|
| 688 |
// Take development database and strip it of content and user data to
|
| 689 |
// create a skeleton to work from (skeleton.sql)
|
| 690 |
exec("cat $file_path/$dev_db.sql $strip_merge $strip_override $strip_comment $reverse_filter > $file_path/tmp/skeleton.sql");
|
| 691 |
print "."; // ghetto status indicators
|
| 692 |
|
| 693 |
// copy skeleton to where we'll be building the newly merged database
|
| 694 |
exec("cp $file_path/tmp/skeleton.sql $file_path/tmp/merged_database.sql");
|
| 695 |
print ".";
|
| 696 |
|
| 697 |
//Add production user data to start building the newly merged database
|
| 698 |
foreach($tables_override as $table){
|
| 699 |
exec("grep 'INSERT INTO .".$table.". VALUES' $file_path/$prod_db.sql >> $file_path/tmp/data_user.sql");
|
| 700 |
print ".";
|
| 701 |
}
|
| 702 |
exec("cat $file_path/tmp/data_user.sql >> $file_path/tmp/merged_database.sql");
|
| 703 |
print ".";
|
| 704 |
|
| 705 |
// Grab data from production, development and merged databases
|
| 706 |
// Strip auto increment table sequences to avoid conflicts
|
| 707 |
if(file_exists("$file_path/tmp/data_dev.sql")){ exec("rm $file_path/tmp/data_dev.sql"); }
|
| 708 |
if(file_exists("$file_path/tmp/data_prod.sql")){ exec("rm $file_path/tmp/data_prod.sql"); }
|
| 709 |
if(file_exists("$file_path/tmp/data_lastmerge.sql")){ exec("rm $file_path/tmp/data_lastmerge.sql"); }
|
| 710 |
|
| 711 |
foreach($tables_merge as $table){
|
| 712 |
exec("grep 'INSERT INTO .".$table.". VALUES' $file_path/$dev_db.sql $strip_increment >> $file_path/tmp/data_dev.sql");
|
| 713 |
print ".";
|
| 714 |
exec("grep 'INSERT INTO .".$table.". VALUES' $file_path/$prod_db.sql $strip_increment >> $file_path/tmp/data_prod.sql");
|
| 715 |
print ".";
|
| 716 |
exec("grep 'INSERT INTO .".$table.". VALUES' $file_path/$lastmerge_db.sql $strip_increment >> $file_path/tmp/data_lastmerge.sql");
|
| 717 |
print ".";
|
| 718 |
}
|
| 719 |
|
| 720 |
// Create a new merged version of data (this step takes awhile)
|
| 721 |
// NOTE: requires GNU diff3
|
| 722 |
print " Done. \n\nPerforming merge of data...";
|
| 723 |
exec("diff3 -E --merge $file_path/tmp/data_dev.sql $file_path/tmp/data_lastmerge.sql $file_path/tmp/data_prod.sql > $file_path/tmp/data_merged.sql");
|
| 724 |
|
| 725 |
// Check to see if the data merge had conflicts. Otherwise, continue.
|
| 726 |
$conflicts = exec("grep '^<<<<<<<' $file_path/tmp/data_merged.sql");
|
| 727 |
if($conflicts){
|
| 728 |
|
| 729 |
$message = "\n\nWARNING: The database has conflicts!\n\n";
|
| 730 |
|
| 731 |
$message .= "Please check $file_path/tmp/data_merged.sql for what conflicted\n";
|
| 732 |
$message .= "(hint: search for <<<<<<<), then:\n";
|
| 733 |
$message .= " * Resolve the conflicts manually\n";
|
| 734 |
$message .= " * Save $file_path/tmp/data_merged.sql\n\n";
|
| 735 |
|
| 736 |
$message .= "NOTE: If you want to default all conflicts to production values,\n
|
| 737 |
run this command instead of resolving manually:\n\n";
|
| 738 |
|
| 739 |
$message .= "diff3 -e --merge $file_path/tmp/data_dev.sql $file_path/tmp/data_lastmerge.sql $file_path/tmp/data_prod.sql > $file_path/tmp/data_merged.sql\n\n";
|
| 740 |
|
| 741 |
$message .= "After resolving conflicts, run the following commands\n
|
| 742 |
(just copy and paste):\n\n";
|
| 743 |
|
| 744 |
$message .= "cat $file_path/tmp/data_merged.sql >> $file_path/tmp/merged_database.sql\n";
|
| 745 |
$message .= "cp $file_path/tmp/merged_database.sql $file_path/$prod_db.sql\n";
|
| 746 |
$message .= "$script_path/restore.php production min sequences\n";
|
| 747 |
$message .= "$script_path/dump.php production min\n";
|
| 748 |
$message .= "cp $file_path/tmp/merged_database.sql $file_path/$dev_db.sql\n";
|
| 749 |
$message .= "$script_path/restore.php $dev_db\n";
|
| 750 |
$message .= "$script_path/dump.php $dev_db\n";
|
| 751 |
$message .= "cp $file_path/$dev_db.sql $file_path/$lastmerge_db.sql\n";
|
| 752 |
$message .= "$script_path/restore.php production min\n";
|
| 753 |
|
| 754 |
} else {
|
| 755 |
|
| 756 |
print " Successful! \n\nPreparing final files...";
|
| 757 |
|
| 758 |
// Append the merged data to the merged version we're building
|
| 759 |
exec("cat $file_path/tmp/data_merged.sql >> $file_path/tmp/merged_database.sql");
|
| 760 |
print ".";
|
| 761 |
|
| 762 |
// Apply the merged data to the databases
|
| 763 |
exec("cp $file_path/tmp/merged_database.sql $file_path/$dev_db.sql");
|
| 764 |
print ".";
|
| 765 |
exec("cp $file_path/tmp/merged_database.sql $file_path/$prod_db.sql");
|
| 766 |
print ".";
|
| 767 |
|
| 768 |
|
| 769 |
// Restore and dump databases so they are formated correctly with correct
|
| 770 |
// sequences for their version and prepared to be committed into svn.
|
| 771 |
//
|
| 772 |
// Production is restored first to align the database with the sequences currently
|
| 773 |
// in MySQL (which is the production version), then restored again after
|
| 774 |
// development to leave the MySQL status ready to be testing for the live version
|
| 775 |
dbscripts_restore($prod_db, 'min', 'sequences');
|
| 776 |
print ".";
|
| 777 |
dbscripts_dump($prod_db, 'min');
|
| 778 |
print ".";
|
| 779 |
dbscripts_restore($dev_db);
|
| 780 |
print ".";
|
| 781 |
dbscripts_dump($dev_db);
|
| 782 |
print ".";
|
| 783 |
exec("cp $file_path/$dev_db.sql $file_path/$lastmerge_db.sql"); // Merged database is a copy of development
|
| 784 |
print ".";
|
| 785 |
dbscripts_restore($prod_db, 'min');
|
| 786 |
print ".";
|
| 787 |
|
| 788 |
//Delete temp files
|
| 789 |
exec("rm $file_path/tmp/*");
|
| 790 |
print " Done.";
|
| 791 |
|
| 792 |
$message = "Merge completed successfully. Congrats! Pat yourself on the back.\n";
|
| 793 |
}
|
| 794 |
|
| 795 |
return "\n\n$message\n";
|
| 796 |
}
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|