python导出wordpress数据库

为啥写个这玩意,还不是因为我懒吗!

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 根据WordPress的配置文件导出数据库

import os
import sys
import time

db_name     = ''
db_user     = ''
db_password = ''
db_host     = ''

if len(sys.argv) != 2:
	print('请输入WordPress目录')
else:
	path = sys.argv[1]
	if os.path.exists(path) == True:
		with open(path + '/wp-config.php') as f:
			lines = f.readlines()
			for line in lines:
				if line.find('DB_NAME') != -1:
					db_name = line.split("'")[3]
				if line.find('DB_USER') != -1:
					db_user = line.split("'")[3]
				if line.find('DB_PASSWORD') != -1:
					db_password = line.split("'")[3]
				if line.find('DB_HOST') != -1:
					db_host = line.split("'")[3]
		if len(db_user) > 0 and len(db_name) and len(db_password) > 0 and len(db_host) > 0:
			filename = db_name + '-' + time.strftime('%Y%m%d%H%M%S', time.localtime()) + '.sql'
			print('开始导出数据库 ' + db_name + ' ...')
			cmd = "mysqldump -u%s -p%s -h'%s' %s > %s" % (db_user, db_password, db_host, db_name ,filename)
			status = os.system(cmd)
			if status != 0:
				print('导出失败')
			else:
				print('成功导出文件:' + filename)
				
	else:
		print('目录错误')