数据库备份

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/python
#_*_encoding:utf-8_*_
#Author:gdlinjianying@qq.com

import os
import time

username = '用户名'
password = R'密码'

target_dir = '/opt/backup/'

today = target_dir + time.strftime('%Y%m%d')

now = time.strftime('%H%M%S')

if not os.path.exists(today):
	os.makedirs(today)
	print 'Successfully created directory', today

target = today + os.sep + now + '.sql'

mysqldump_command = 'mysqldump -u%s -p%s --all-databases > target' % (username, password)

if os.system(mysqldump_command) == 0:
	print 'Successful backup all databases to', target
else:
	print 'Backup failed'