1、安装docker(不说了)、下载centos7镜像:
docker pull centos:7
2、运行 centos7 docker 容器,在容器中安装mydumer,安装相关的工具和crontab。
crontab的安装这样就不说了,有一些需要注意,docker 容器中要使用 systemctl start crond.service,要使用--privilege=true /usr/sbin/init 运行centos7容器。
3、安装python3运行环境。
yum install -y python3
4、安装好mydumer、crontab、python3后就可以编写python脚本了:
#!/usr/bin/env python # -*- coding:utf-8 -*- # @FileName :mysql_backup.py # @Time :2020/5/22 12:51 # @Author :Oscar import os import time import datetime import configparser import shutil # 创建备份类 class Backup(object): # 初始化函数 def __init__(self, conf_path='/opt/conf/backup.conf', file_path='/opt/file', log_file='/opt/log/backup.log'): # 配置文件目录 self.conf_path = conf_path # 备份目录 self.file_path = file_path # 日志目录 self.log_path = log_file # 读取配置 self.read_conf() # 执行数据库备份 def exec_backup(self): db = [item.strip() for item in self.__db] self.log('备份开始...') for item in db: self.backup_db(item) self.log('备份结束...') # 执行备份 def backup_db(self, db): self.log('删除%s历史备份开始...' % db) self.delete_history(db) self.log('删除%s历史备份完成...' % db) self.log("%s备份开始..." % db) dir_name = self.backup_dirname(db) command = 'mydumper -h %s -P %s -u %s -p %s -c -B %s -o %s' \ % (self.__host, self.__port, self.__user, self.__password, db, dir_name) os.popen(command) self.log('%s备份完成...' % db) # 删除该库N天之前的数据 def delete_history(self, db): parent_dir = os.path.join(self.file_path, db) dir_name = self.history_dirname(db) for item in os.listdir(parent_dir): item_dir = os.path.join(parent_dir, item) if item_dir < dir_name: shutil.rmtree(item_dir) # 获取备份目录名称 def backup_dirname(self, db): dir_name = 'backup_' time_str = time.strftime("%Y%m%dT%H%M%S", time.localtime(time.time())) dir_name = '%s%s' % (dir_name, time_str) dir_name = os.path.join(self.file_path, db, dir_name) if not os.path.exists(dir_name): os.makedirs(dir_name) return dir_name # 获取N天前的的目录名称用于比较大小 def history_dirname(self, db): dir_name = 'backup_' days_ago = datetime.datetime.now() - datetime.timedelta(days=self.__days) time_str = time.strftime("%Y%m%dT%H%M%S", time.localtime(time.mktime(days_ago.timetuple()))) dir_name = '%s%s' % (dir_name, time_str) dir_name = os.path.join(self.file_path, db, dir_name) return dir_name # 读取配置文件 def read_conf(self): conf = configparser.ConfigParser() conf.read(self.conf_path, encoding='utf-8') # 数据库主机 self.__host = conf.get('mysql', 'host') # 数据库端口 self.__port = conf.get('mysql', 'port') # 数据库用户名 self.__user = conf.get('mysql', 'user') # 数据库密码 self.__password = conf.get('mysql', 'password') # 要备份的数据库 self.__db = conf.get('mysql', 'db').split(',') # 数据保留时间 self.__days = int(conf.get('mysql', 'days')) # 记录日志文件 def log(self, content): time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) with open(self.log_path, 'a', encoding='utf8') as fp: fp.write(time_str + ' ' + content + '\n') if __name__ == "__main__": backup = Backup() backup.exec_backup() pass
5、python程序中使用的配置文件样式:
[mysql] host = localhost port = 3306 user = root password = 123456 db = strive,strive_test days = 7
6、编辑/etc/crontab:
30 22 * * * root /opt/pyshell/backup.py
这样就可以执行了。
7、将容器提交为docker镜像:
docker commit centos7 docker/mysqlbackup