Created
December 16, 2012 09:05
-
-
Save futoase/4305764 to your computer and use it in GitHub Desktop.
This script is process the mysql dump.
Your useful for backup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding:utf-8 -*- | |
| import os | |
| import shlex | |
| import json | |
| from subprocess import call | |
| import pymysql | |
| DUMP_TEMPLATE = 'mysqldump -h {host} -u {user} --password="{passwd}" {db} {table} > dumps/{table}.sql' | |
| TAR_TEMPLATE = 'tar -cvJf dumps.tar.xz dumps' | |
| def dump(): | |
| ''' | |
| This script processing the mysql dump. | |
| setting.json format: | |
| { | |
| "host": "Mysql server host name.", | |
| "user": "Mysql server user name.", | |
| "passwd": "Mysql server user password.", | |
| "db": "Name the target database." | |
| } | |
| ''' | |
| # Create directory of mysqldump. | |
| if not os.path.exists('./dumps'): | |
| os.mkdir('./dumps') | |
| setting = json.loads(open('./setting.json').read()) | |
| con = pymysql.connect(**setting) | |
| cur = con.cursor() | |
| cur.execute('SHOW TABLES') | |
| for row in cur.fetchall(): | |
| args = shlex.split( | |
| DUMP_TEMPLATE.format( | |
| host = setting["host"], | |
| user = setting["user"], | |
| passwd = setting["passwd"], | |
| db = setting["db"], | |
| table = row[0] | |
| )) | |
| print(' '.join(args)) | |
| call(' '.join(args), shell=True) | |
| cur.close() | |
| con.close() | |
| call(TAR_TEMPLATE, shell=True) | |
| if __name__ == '__main__': | |
| dump() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment