import os import MySQLdb user = 'root' passwd = 'TOPSECRET' database = 'mydb' db = MySQLdb.connect(user=user, passwd=passwd, db=database) c = db.cursor() c.execute("show tables where tables_in_" + database + " like 'hello%'") result = c.fetchall() tables = ' '.join(row[0] for row in result) sql = 'mysqldump -c -u %s -p%s %s --tables %s > custom.sql' % (user, passwd, database, tables) os.system(sql) c.close()