66use mikehaertl \shellcommand \Command ;
77use yii \console \Controller ;
88use yii \console \Exception ;
9+ use yii \helpers \Console ;
910use yii \helpers \FileHelper ;
1011
1112/**
@@ -229,28 +230,33 @@ public function actionDump()
229230 * @throws \yii\base\Exception
230231 */
231232 public function actionXDumpData (){
233+ $ fileName = $ this ->getFilePrefix ()."_data.sql " ;
232234 $ command = new Command ('mysqldump ' );
233235
234236 $ command ->addArg ('-h ' ,getenv ('DB_PORT_3306_TCP_ADDR ' ));
235237 $ command ->addArg ('-u ' ,getenv ('DB_ENV_MYSQL_USER ' ));
236238 $ command ->addArg ('--password= ' ,getenv ('DB_ENV_MYSQL_PASSWORD ' ));
237239 $ command ->addArg ('--no-create-info ' );
240+ $ this ->stdout ("Ignoring tables: " );
238241 foreach ($ this ->noDataTables as $ table ) {
239242 $ command ->addArg ('--ignore-table ' ,getenv ('DB_ENV_MYSQL_DATABASE ' ) . '. ' . $ table );
243+ $ this ->stdout ("$ table, " );
240244 }
245+ $ this ->stdout ("\n" );
241246 $ command ->addArg (getenv ('DB_ENV_MYSQL_DATABASE ' ));
242247
243248 $ command ->execute ();
244249
245250 $ dir = \Yii::getAlias ('@runtime/mysql ' );
246251 FileHelper::createDirectory ($ dir );
247- $ fileName = 'data.sql ' ;
248252
249253 $ dump = $ command ->getOutput ();
250254 $ dump = preg_replace ('/LOCK TABLES (.+) WRITE;/ ' ,'LOCK TABLES $1 WRITE; TRUNCATE TABLE $1; ' ,$ dump );
255+ $ file = $ dir .'/ ' .$ fileName ;
256+
257+ file_put_contents ($ file , $ dump );
251258
252- file_put_contents ($ dir .'/ ' .$ fileName , $ dump );
253- $ this ->stdout ('Done. ' );
259+ $ this ->stdout ("\nMySQL dump successfully written to ' $ file' \n" , Console::FG_GREEN );
254260 }
255261
256262 /**
@@ -306,14 +312,11 @@ public function actionXDump()
306312 // generate file
307313 $ dir = \Yii::getAlias ('@runtime/mysql ' );
308314 FileHelper::createDirectory ($ dir );
309- $ fileName = $ date . ' _ ' . getenv ( ' DB_ENV_MYSQL_DATABASE ' ) . '_ ' . $ fileNameSuffix . '.sql ' ;
315+ $ fileName = $ this -> getFilePrefix ( ) . '_ ' . $ fileNameSuffix . '.sql ' ;
310316 $ file = $ dir . '/ ' . $ fileName ;
311317 file_put_contents ($ file , $ dump );
312318
313- $ this ->stdout ("\n" );
314- $ this ->stdout ('MYSQL Dump successfully saved to ' );
315- $ this ->stdout ("\n" . $ file );
316- $ this ->stdout ("\n\n" );
319+ $ this ->stdout ("\nMySQL dump successfully written to ' $ file' \n" , Console::FG_GREEN );
317320 }
318321
319322 /**
@@ -333,4 +336,8 @@ private function execute($cmd)
333336 }
334337 }
335338
339+ private function getFilePrefix (){
340+ return \Yii::$ app ->id .'_ ' .YII_ENV .'_ ' .date ('U ' );
341+ }
342+
336343}
0 commit comments