Created
September 2, 2016 07:07
-
-
Save YavorK/e4cbf73aadb2e6bc5366edac827d70c7 to your computer and use it in GitHub Desktop.
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 | |
Artisan::command('db:port-original', function () { | |
//Laravel Remote is AWESOEM // https://laravelcollective.com/docs/5.3/ssh#sftp-downloads ... | |
//make sure process does not get killed | |
set_time_limit(0); | |
//define some values | |
$archiveName = env('ORIGINAL_SERVER_DB_DUMP_ARCHIVE_NAME'); | |
$archiveFilePath = base_path('storage' . DIRECTORY_SEPARATOR . $archiveName); | |
$dumpFilePath = base_path('storage' . DIRECTORY_SEPARATOR . 'do.dump'); | |
$remoteArchivePath = env('ORIGINAL_SERVER_DB_DUMP_ARCHIVE_DIR') . $archiveName; | |
//dump and archive the db | |
$this->info("Dumping and archiving remote DB..."); | |
SSH::into('original_server')->run([ | |
'mysqldump -u ' . env('ORIGINAL_SERVER_MYSQL_USERNAME') | |
. ' -p' . env('ORIGINAL_SERVER_MYSQL_PASSWORD') . ' ' | |
. env('ORIGINAL_SERVER_MYSQL_DATABASE') | |
. ' | gzip -c | cat > ' . $remoteArchivePath | |
]); | |
//download db to local | |
$this->info('Downloading dump file archive...'); | |
SSH::into('original_server')->get($remoteArchivePath, $archiveFilePath); | |
//unzip | |
$this->info('Unzipping the archive...'); | |
exec('gunzip < ' . $archiveFilePath . ' > ' . $dumpFilePath); | |
//add which db to use | |
$this->info('Adding USE statement to dump...'); | |
exec('sed -i \'1 i\ USE `'.env('LOCAL_SERVER_MYSQL_DATABASE').'`;\' ' . $dumpFilePath); | |
//import it | |
$this->info('Importing to the local DB...'); | |
exec('mysql -u '.env('LOCAL_SERVER_MYSQL_USERNAME').' -p'.env('LOCAL_SERVER_MYSQL_PASSWORD').' < ' . $dumpFilePath); | |
//delete remote dump files | |
$this->info("Remove remote archive... "); | |
SSH::into('original_server')->run([ | |
'rm -f ' . $remoteArchivePath | |
]); | |
//delete local dump files | |
$this->info("Remove local files... "); | |
@unlink($dumpFilePath); | |
@unlink($archiveFilePath); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment