MySQL Bulk import of multiple time SQL

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.