#!/usr/bin/env python
Import OS
Import commands
Import time
Import Sys
def sqlorder (Sqldir):
sqlfile={}
For I in Os.popen (' ls%s '%sqldir). ReadLines ():
Sqlfile[i] = i.split ('-') [1]
Sqlfile1=[]
For I in Sorted (Sqlfile.items (), Key=lambda d:d[1]):
Sqlfile1.append (I[0])
Return Sqlfile1
def MysqlDump (databasename):
Datetime=time.strftime ("%y_%m_%d_%h_%m_%s", Time.localtime ())
Cmd= ' mysqldump-uroot-predhat%s >/test/%s_%s.sql '% (databasename,databasename,datetime)
Print cmd
K,v=commands.getstatusoutput (CMD)
#k, V=commands.getstatusoutput (' mysqldump-uroot-predhat%s >/test/%s_%s.sql '% (databasename,datetime))
If k = = 0:
Return ' msyqlbackupsucess '
Else
Return ' mysqlbackupfailed '
def importsql (Databasename,sqldir):
Test=sqlorder (Sqldir)
Done=mysqldump (DatabaseName)
if done = = ' mysqlbackupfailed ':
Return ' mysqlbakcupfailed '
For I in test:
#os. Popen (' Mysql-uroot-predhat%s </test/sql/%s '% (databasename,i))
Cmd= ' Mysql-uroot-predhat%s </test/sql/%s '% (Databasename,i.strip ())
Print cmd
K,v=commands.getstatusoutput (CMD)
#k, V=commands.getstatusoutput (' Mysql-uroot-predhat%s </test/sql/%s '% (databasename,i))
If k = = 0:
print '%s Import SQL Success '%i
Else
print '%s Import SQL failed '%i
Return ' importsqlfailed '
if __name__ = = "__main__":
DATABASENAME=SYS.ARGV[1]
SQLDIR=SYS.ARGV[2]
Importsql (Databasename,sqldir)
---------------------------------------------------------------------------------------------------------------
Note the file name format and the dependent MySQL command is good, file name format: Filesname-times.sql
This is a small script that writes a SQL bulk import based on multiple SQL file naming conventions,
For example, multiple filenames are as follows:
A-20161021.sql
Abc-20161020.sql
Abcd-20161027.sql
B-20161022.sql
C-20161023.sql
D-20161024.sql
The above script can be sorted according to the following date and imported into the corresponding database
Examples are as follows:
[email protected] test]# python sqlupdate2.py test/test/sql/
Mysqldump-uroot-predhat Test >/test/test_2010_12_12_14_16_26.sql
Mysql-uroot-predhat Test </test/sql/f-20161020.sql
F-20161020.sql
Import SQL Success
Mysql-uroot-predhat Test </test/sql/f-20161021.sql
F-20161021.sql
Import SQL Success
Mysql-uroot-predhat Test </test/sql/g-20161022.sql
G-20161022.sql
Import SQL Success
MySQL Bulk import of multiple time SQL