python备份文件与mysql数据库的脚本

发布时间:2020-07-21编辑:脚本学堂
本文介绍一个python脚本,用来备份文件,及mysql数据库,有需要的朋友,作个参考吧。

用于备份文件与mysql/ target=_blank class=infotextkey>mysql数据库python/ target=_blank class=infotextkey>python脚本代码,如下:
 

复制代码 代码示例:

#!/usr/local/python
import os
import time
import string

source=['/var/data/xxx1/','/var/data/xxx2/']
target_dir='/backup/'
target=target_dir+time.strftime('%Y%m%d')
zip_comm='zip -r %s %s'%(target," ".join(source))

target_database=['DB_name1','DB_name2']
sql_user='root'
sql_pwd='xxx'

if os.system(zip_comm) == 0:
print 'file backup Success is:',target
#if python version is 3.x ,print('file backup Success is:',target)
else:
print 'file backup failed!'

#backup mysql data files
for database_name in target_database:
target_sql=target_dir+database_name+time.strftime('%Y%m%d')+'.sql'
sql_comm='/usr/local/mysql/bin/mysqldump -u %s -p%s %s > %s'%(sql_user,sql_pwd,database_name,target_sql)
if os.system(sql_comm) == 0:
print database_name,'is backup seccess!'
else:
print database_name,'is backup Failed!!'