Last active
May 24, 2017 10:03
-
-
Save spyesx/597cbe2db54b3e0efb63e2c59b9369a6 to your computer and use it in GitHub Desktop.
Create multiple SQL DB for different environments based on a local one. Useful for Wordpress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Folder tree : | |
./wordpress_migrate_db.php | |
./local | |
./dev | |
./preprod | |
./prod | |
In a terminal : php wordpress_migrate_db.php | |
*/ | |
$database = 'db_name'; | |
$user = 'sql_user'; | |
$pass = 'sql_pass'; | |
$file = 'tempfile.sql'; | |
$date = date('c'); | |
$env = array( | |
'local' => 'project.tld', | |
'dev' => 'dev.project.tld', | |
'preprod' => 'preprod.project.tld', | |
'prod' => 'prod.project.tld' | |
); | |
exec('mysqldump '.$database.' -u'.$user.' -p'.$pass.' > '.$file.''); | |
$originSQL = file_get_contents($file); | |
$newFilename = $date.'_'.$file; | |
foreach ($env as $name => $url) | |
{ | |
$envFile = './'.$name.'/'.$newFilename; | |
$fo = fopen($envFile, 'w') or die('Cannot open file: '.$envFile); | |
$data = $originSQL; | |
$data = str_replace($env['local'], $url, $data); | |
fwrite($fo, $data); | |
} | |
unlink($file); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment