mysql 每日备份脚本分享

 下面是我自己使用的mysql每日数据备份脚本。分享给有需要的同学: python脚本,Centos7系统(自带python2.7)

#! /usr/bin/python

import os
import re
import datetime

mysql_host = '192.168.9.92'   ## 数据库连接地址
databases = ['sms','xingqi2','wechat_mall','zhonghui_wechat']   ## 需要备份的数据库库名
db_user = 'root'    ## 连接用户名
db_passwd = 'root'    ## 连接密码
back_dir = '/usr/local/backup_mysql/mysqldata'     ## 备份数据文件存储路径
today = datetime.datetime.today().strftime('%Y%m%d')
for database in databases:
    command = '/usr/local/mysql/bin/mysqldump -h '+mysql_host+' -u'+db_user+' -p'+db_passwd+' '+database+' |gzip > '+back_dir+'/'+database+'_'+today+'.sql.gz'
    os.system(command)
dirlist = os.listdir(back_dir)
for dirname in dirlist:
    t1 = re.findall(r'_([\d]*?).sql',dirname)[0]
    time1 = datetime.datetime.strptime(t1,'%Y%m%d')
    time_dif = datetime.datetime.today()-time1
    times = time_dif.days
    if times >= 10:     ## 这里是只保存最近10天数据,根据实际情况自己调整
        os.remove(back_dir+'/'+dirname)


猜你喜欢

转载自blog.csdn.net/weixin_41004350/article/details/79287600
今日推荐