When using the MySQL database, if you import data frequently. To export the entire database using MySQL Internal commands, it is often because a table fails, resulting in the entire Import and Export job failure. So I want to divide and conquer it. Use PHP to write a script. Each import/export operation has a higher chance of success.
Use:
Usage: PhP dbio. php IM/import/EX/export/TR/truncate DB: // User: password @ host/dat
Abase path [-N]
Im/import is import DB from SQL files which is exists;
EX/export is export dB to SQL files;
TR/truncate is empties all tables in this database; the parm 'path' will be
Ignored. Path: the directory of the SQL files;
[-N] is optional parameter just for create new database when parm IM/import is
Exists.
Personal use is good.
The Code is as follows (dbio. php)
- <? PHP
- /**
- * @ File dbio. php
- * Import/export/truncate tables from a database, one file per table
- * Import/im tb: // User: passwd @ localhost/database path [-N]
- * Export/ex TB: // User: passwd @ localhost/database path
- * Truncate/tr tb: // User: passwd @ localhost/database path
- *
- * Mit license
- *
- * $ ID $
- */
- /* VIM: Set tabstop = 4 shiftwidth = 4 expandtab :*/
- Define ('schema _ database', 'schema. SQL ');
- Function usage ($ order ){
- Echo "Usage: PhP $ order IM/import/EX/export/TR/truncate DB: // User: password @ host/database path [-N]/n ";
- Echo "im/import/T is import DB from SQL files which is exists;/N ";
- Echo "EX/export/T is export dB to SQL files;/N ";
- Echo "TR/truncate/T is empties all tables in this database; the parm 'path' will be ignored .";
- Echo "Path:/t the directory of the SQL files;/N ";
- Echo "[-N] is optional parameter just for create new database when parm IM/import is exists./N ";
- Die ();
- }
- Function checkparams ($ argv, $ argc ){
- If ($ argc <3 | $ argc> 5 ){
- Foreach ($ argv as $ ELEM ){
- If (! Assert ($ ELEM) return false;
- }
- Return false;
- }
- Return true;
- }
- Function actionimport ($ TB, $ user, $ passwd, $ host, $ database, $ path, $ new ){
- $ Dbconn = mysql_select_db ($ database );
- If (! $ Dbconn ){
- If ($ new = '-n '){
- Echo "now create database $ database./N ";
- Mysql_query ("create database $ Database ");
- $ Exec = "mysql-h $ host-U $ user-p $ passwd $ database <$ path/". schema_database;
- Passthru ($ Exec );
- } Elseif ($ new = ''){
- Echo "database do not exist !, You need Param '-n' at last./N ";
- Die ();
- }
- }
- $ Files = scandir ($ PATH );
- If (! $ Files ){
- Echo "Check Params $ path ";
- } Else {
- $ SQL _file = preg_grep ("/[a-zA-Z0-9] */. SQL $/I", $ files );
- Echo "now start importing </N ";
- Foreach ($ SQL _file as $ ELEM ){
- If ($ ELEM = schema_database) continue;
- $ Exec = "mysql-h $ host-U $ user-p $ passwd $ database <$ path/$ ELEM ";
- Dopassthru ($ Exec );
- }
- }
- }
- Function actiontruncate ($ database ){
- Echo "now emptying all tables exporting >>>>/N ";
- $ Result = mysql_list_tables ($ database );
- $ TBS = array ();
- For ($ I = 0; $ I <mysql_num_rows ($ result); $ I ++)
- $ TBS [] = mysql_tablename ($ result, $ I );
- Mysql_free_result ($ result );
- Foreach ($ TBS as $ ELEM ){
- $ Exec = "truncate $ ELEM ";
- $ Result = mysql_query ($ Exec) or die ($ exec. 'failed ');
- }
- Echo "truncate success .";
- }
- Function actionexport ($ TB, $ user, $ passwd, $ host, $ database, $ path, $ new ){
- Echo "now start exporting ---/N ";
- $ Result = mysql_list_tables ($ database );
- $ TBS = array ();
- For ($ I = 0; $ I <mysql_num_rows ($ result); $ I ++)
- $ TBS [] = mysql_tablename ($ result, $ I );
- Mysql_free_result ($ result );
- Foreach ($ TBS as $ ELEM ){
- $ Exec = "mysqldump-h $ host-U $ user-p $ passwd-Q-T $ database $ ELEM> $ path/$ ELEM. SQL ";
- Dopassthru ($ Exec );
- }
- $ Exec = "mysqldump-h $ host-U $ user-p $ passwd-Q-d $ database> $ path/". schema_database;
- Dopassthru ($ Exec );
- }
- Function dopassthru ($ Exec ){
- Echo $ exec;
- Passthru ($ exec, $ RET );
- If ($ RET! = 0 ){
- Echo ": Failed/N ";
- } Else {
- Echo ": OK/N ";
- }
- Return true;
- }
- // Main ()
- If (checkparams ($ argv, $ argc )){
- $ Action = $ argv [1];
- List ($ TB, $ tmp1, $ tmp2, $ user, $ passwd, $ host, $ database) = Split ('[: // @]', $ argv [2]);
- $ Path = $ argv [3];
- $ New = $ argv [4];
- If (! Mysql_connect ($ host, $ user, $ passwd) Die ("cocould not connect to MySQL ");
- Switch ($ action ){
- Case 'im ':
- Case 'import ':
- Actionimport ($ TB, $ user, $ passwd, $ host, $ database, $ path, $ new );
- Break;
- Case 'X ':
- Case 'Port ':
- Actionexport ($ TB, $ user, $ passwd, $ host, $ database, $ path, $ new );
- Break;
- Case 'tr ':
- Case 'truncate ':
- Actiontruncate ($ database );
- Break;
- Default:
- Die ("Action Param '$ action' is error !!! ");
- }
- } Else {
- Usage ($ argv [0]);
- }
- ?>