码迷,mamicode.com
首页 > 数据库 > 详细

python编写Mysql自动备份脚本

时间:2016-04-04 18:06:25      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
# filename: mysql_dump.py
 
 
import os
import time
import string
‘‘‘ defined variable ‘‘‘
databases=‘--database kbss_kcas reportdb koacif sequence infocenter‘
sql_user=‘root‘
sql_pwd=‘123456‘
‘‘‘ Defining the remote backup variables ‘‘‘
jv_test01_dir="/opt/mysql"
 
‘‘‘ Create the backup file directory ‘‘‘
 
mkdir_dir="/opt/"+time.strftime(‘%Y%m‘)+"/"
if not os.path.exists(mkdir_dir):
        os.mkdir(mkdir_dir)
        print ‘Successfully created directory‘, mkdir_dir
 
‘‘‘ Start backup of database to the specified directory ‘‘‘
 
database_name = ‘databak31‘
os.chdir(mkdir_dir)
today_sql=mkdir_dir+database_name+‘_‘+time.strftime(‘%Y%m%d‘)+‘.sql‘
sql_comm="mysqldump -u %s -p‘%s‘ -R --events --quick --single-transaction %s > %s"%(sql_user,sql_pwd,databases,today_sql)
 
if os.system(sql_comm) ==  0:
        print database_name,‘is backup successfully!‘
else:
        print database_name,‘is backup Failed!!‘
time.sleep( 3)
scp_comm="cp %s %s "% (today_sql,jv_test01_dir)  #这里我使用了一个挂在盘,所以直接拷贝到挂在盘就可以做备份文件二次保护啦!
if os.system(scp_comm) ==  0:
        print today_sql,‘This file backup to jv_test01 success!‘
else:
        print today_sql,‘This file backup to jv_test01  Failed!!‘

 

python编写Mysql自动备份脚本

标签:

原文地址:http://www.cnblogs.com/TaleG/p/5352333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!