#!/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!!‘