python编写Mysql自动备份脚本
Posted Tale_G
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python编写Mysql自动备份脚本相关的知识,希望对你有一定的参考价值。
#!/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自动备份脚本的主要内容,如果未能解决你的问题,请参考以下文章