python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python Mysql备份查询脚本

Python基于docker部署的Mysql备份查询脚本

作者:蜗牛其实也很努力

这篇文章主要来和大家分享Python基于docker部署的Mysql备份查询的脚本,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起了解下

前言

此环境是基于docker部署的mysql,docker部署mysql可以参考如下链接:

docker 部署服务案例

颜色块文件

root@bogon:~ 2024-04-18 16:34:23# cat DefaultColor.py
#########################################################################
#    File Name: DefaultColor.py
#    Author: eight
#    Mail: 18847097110@163.com 
#    Created Time: Thu 11 Apr 2024 10:25:31 PM CST
#########################################################################
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Color:
        END = '\033[0m'  # normal
        BOLD = '\033[1m'  # bold
        RED = '\033[1;91m'  # red
        GREEN = '\033[1;92m'  # green
        ORANGE = '\033[1;93m'  # orange
        BLUE = '\033[1;94m'  # blue
        PURPLE = '\033[1;95m'  # purple
        UNDERLINE = '\033[4m'  # underline
        CYAN = '\033[1;96m'  # cyan
        GREY = '\033[1;97m'  # gray
        BR = '\033[1;97;41m'  # background red
        BG = '\033[1;97;42m'  # background green
        BY = '\033[1;97;43m'  # background yellow

备份代码

 
#########################################################################
#    File Name: query_mysql.py
#    Author: eight
#    Mail: 18847097110@163.com 
#    Created Time: Thu 18 Apr 2024 11:12:26 AM CST
#########################################################################
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymysql
import configparser
import os
from datetime import datetime
import DefaultColor
import subprocess
 
 
# 创建备份目录
def mkdir():
        dir_path = os.getcwd()
        print("--------------------------------------------------------")
        print("当前脚本工作目录:", dir_path)
        mysql_backup_dir = "/opt/mysql_backup"
        print("--------------------------------------------------------")
        if not os.path.exists(mysql_backup_dir):
                os.makedirs(mysql_backup_dir)
        else:
                return f"数据库备份目录 '{mysql_backup_dir}' 已经存在,跳过创建"
        return "数据库备份目录:" + DefaultColor.Color.GREEN + mysql_backup_dir + DefaultColor.Color.END + " 创建成功"
 
 
 
# 备份数据库 
def backup(mysql_backup_dir):
        config = configparser.ConfigParser()
        config.read("config.ini")
        # 获取配置信息
        host = config.get("MYSQL", "MYSQL_HOST")
        username = config.get("MYSQL", "MYSQL_USER")
        password = config.get("MYSQL", "MYSQL_PASSWORD")
        port = config.get("MYSQL", "MYSQL_PORT")
        db = config.get("MYSQL", "MYSQL_DB")
        current_datetime = datetime.now()
        formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H-%M-%S")
        try:
                command = f'docker exec -it mysql-container /bin/bash -c "mysqldump -h {host} -u {username} -p{password} -P {port} -- {db} > {mysql_backup_dir}-{db}-{formatted_datetime}.sql"'
                subprocess.run(command, shell=True, check=True)
                
        except subprocess.CalledProcessError as e:
                print("Database backup failed:", e)
        return f"数据库:{db}备份完成: {mysql_backup_dir}-{db}-{formatted_datetime}.sql"
 
 
 
if __name__ == '__main__':
        backup_dir = mkdir()
        print(backup_dir)
        backup = backup("/opt/mysql_backup")
        print(backup)

此代码如果是本地环境测试的话,逻辑是判断当前本地有没有存储备份数据的目录,没有则创建。

再利用subprocess执行mysqldump命令,按照日期生成.sql文件

效果

 因为是docker环境,所以判断当前目录是否存在,是判断的宿主机上的,所以此代码不是很严谨的代码,如果是本地部署的话,把mysqldump命令前面的  docker exec -it mysql-container /bin/bash -c  删除掉即可。

这样备份的脚本就实现了脚本预期效果:将数据库备份到指定的目录下,以日期区别sql文件

全量代码(读取配置和备份和查询)

root@bogon:~ 2024-04-18 16:30:14# cat MysqlBackup.py 
#########################################################################
#    File Name: query_mysql.py
#    Author: eight
#    Mail: 18847097110@163.com 
#    Created Time: Thu 18 Apr 2024 11:12:26 AM CST
#########################################################################
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymysql
import configparser
import os
from datetime import datetime
import DefaultColor
import subprocess
 
# 读取mysql配置文件
def mysql_config():
        config = configparser.ConfigParser()
        config.read("config.ini")
 
        # 获取配置信息
        host = config.get("MYSQL", "MYSQL_HOST")
        username = config.get("MYSQL", "MYSQL_USER")
        password = config.get("MYSQL", "MYSQL_PASSWORD")
        port = config.get("MYSQL", "MYSQL_PORT")
        db = config.get("MYSQL", "MYSQL_DB")
        print("--------------------------------------------------------")
        return f"主机:{host}\t用户名:{username}\t密码:{password}\t端口:{port}\t数据库:{db}"
 
 
# 创建备份目录
def mkdir():
        dir_path = os.getcwd()
        print("--------------------------------------------------------")
        print("当前脚本工作目录:", dir_path)
        mysql_backup_dir = "/opt/mysql_backup"
        print("--------------------------------------------------------")
        if not os.path.exists(mysql_backup_dir):
                os.makedirs(mysql_backup_dir)
        else:
                return f"数据库备份目录 '{mysql_backup_dir}' 已经存在,跳过创建"
        return "数据库备份目录:" + DefaultColor.Color.GREEN + mysql_backup_dir + DefaultColor.Color.END + " 创建成功"
 
 
# 查询sql语句
##def select():
#       config = configparser.ConfigParser()
#       config.read("config.ini")
#                    
#       # 获取配置信息
#       host = config.get("MYSQL", "MYSQL_HOST")
#       username = config.get("MYSQL", "MYSQL_USER")
#       password = config.get("MYSQL", "MYSQL_PASSWORD")
#       port = config.get("MYSQL", "MYSQL_PORT")
#       db = config.get("MYSQL", "MYSQL_DB")
#       # 创建mysql连接信息
#       conn = pymysql.connect(host=host, user=username, password=password, port=int(port), db=db)
#       cur = conn.cursor()
#       # 执行查询语句
#       cur.execute("use docker;")
#       cur.execute("select * from DockerImages;")
#       print("查询到的数据是:")
#       # 接收全部的返回结果行
#       result = cur.fetchall()
#       while True:
#               resp = cur.fetchone()
#               if resp is None:
#                       break
#       # 关闭查询游标
#       cur.close()
#       # 提交
#       conn.commit()
#       # 关闭链接,释放计算机资源
#       conn.close()
#       return result
 
 
# 备份数据库 
def backup(mysql_backup_dir):
        config = configparser.ConfigParser()
        config.read("config.ini")
        # 获取配置信息
        host = config.get("MYSQL", "MYSQL_HOST")
        username = config.get("MYSQL", "MYSQL_USER")
        password = config.get("MYSQL", "MYSQL_PASSWORD")
        port = config.get("MYSQL", "MYSQL_PORT")
        db = config.get("MYSQL", "MYSQL_DB")
        current_datetime = datetime.now()
        formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H-%M-%S")
        try:
                command = f'docker exec -it mysql-container /bin/bash -c "mysqldump -h {host} -u {username} -p{password} -P {port} -- {db} > {mysql_backup_dir}-{db}-{formatted_datetime}.sql"'
                subprocess.run(command, shell=True, check=True)
                
        except subprocess.CalledProcessError as e:
                print("Database backup failed:", e)
        return f"数据库:{db}备份完成: {mysql_backup_dir}-{db}-{formatted_datetime}.sql"
 
 
 
if __name__ == '__main__':
        #mysql_config = mysql_config()
        #print(mysql_config)
        backup_dir = mkdir()
        print(backup_dir)
 
        #sector = select()
        # 循环打印所有数据,如果此代码是封装在函数内部,那么使用return的话,只会输出第一条数据,所以要在外部调用
        #for row in sector:
        #       print(row)
        backup = backup("/opt/mysql_backup")
        print(backup)
root@bogon:~ 2024-04-18 16:30:18# 
root@bogon:~ 2024-04-18 16:30:32# 
root@bogon:~ 2024-04-18 16:30:32# cat MysqlBackup.py 
#########################################################################
#    File Name: query_mysql.py
#    Author: eight
#    Mail: 18847097110@163.com 
#    Created Time: Thu 18 Apr 2024 11:12:26 AM CST
#########################################################################
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymysql
import configparser
import os
from datetime import datetime
import DefaultColor
import subprocess
 
# 读取mysql配置文件
def mysql_config():
        config = configparser.ConfigParser()
        config.read("config.ini")
 
        # 获取配置信息
        host = config.get("MYSQL", "MYSQL_HOST")
        username = config.get("MYSQL", "MYSQL_USER")
        password = config.get("MYSQL", "MYSQL_PASSWORD")
        port = config.get("MYSQL", "MYSQL_PORT")
        db = config.get("MYSQL", "MYSQL_DB")
        print("--------------------------------------------------------")
        return f"主机:{host}\t用户名:{username}\t密码:{password}\t端口:{port}\t数据库:{db}"
 
 
# 创建备份目录
def mkdir():
        dir_path = os.getcwd()
        print("--------------------------------------------------------")
        print("当前脚本工作目录:", dir_path)
        mysql_backup_dir = "/opt/mysql_backup"
        print("--------------------------------------------------------")
        if not os.path.exists(mysql_backup_dir):
                os.makedirs(mysql_backup_dir)
        else:
                return f"数据库备份目录 '{mysql_backup_dir}' 已经存在,跳过创建"
        return "数据库备份目录:" + DefaultColor.Color.GREEN + mysql_backup_dir + DefaultColor.Color.END + " 创建成功"
 
 
# 查询sql语句
def select():
        config = configparser.ConfigParser()
        config.read("config.ini")
                     
        # 获取配置信息
        host = config.get("MYSQL", "MYSQL_HOST")
        username = config.get("MYSQL", "MYSQL_USER")
        password = config.get("MYSQL", "MYSQL_PASSWORD")
        port = config.get("MYSQL", "MYSQL_PORT")
        db = config.get("MYSQL", "MYSQL_DB")
        # 创建mysql连接信息
        conn = pymysql.connect(host=host, user=username, password=password, port=int(port), db=db)
        cur = conn.cursor()
        # 执行查询语句
        cur.execute("use docker;")
        cur.execute("select * from DockerImages;")
        print("查询到的数据是:")
        # 接收全部的返回结果行
        result = cur.fetchall()
        while True:
                resp = cur.fetchone()
                if resp is None:
                        break
        # 关闭查询游标
        cur.close()
        # 提交
        conn.commit()
        # 关闭链接,释放计算机资源
        conn.close()
        return result
 
 
# 备份数据库 
def backup(mysql_backup_dir):
        config = configparser.ConfigParser()
        config.read("config.ini")
        # 获取配置信息
        host = config.get("MYSQL", "MYSQL_HOST")
        username = config.get("MYSQL", "MYSQL_USER")
        password = config.get("MYSQL", "MYSQL_PASSWORD")
        port = config.get("MYSQL", "MYSQL_PORT")
        db = config.get("MYSQL", "MYSQL_DB")
        current_datetime = datetime.now()
        formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H-%M-%S")
        try:
                command = f'docker exec -it mysql-container /bin/bash -c "mysqldump -h {host} -u {username} -p{password} -P {port} -- {db} > {mysql_backup_dir}-{db}-{formatted_datetime}.sql"'
                subprocess.run(command, shell=True, check=True)
                
        except subprocess.CalledProcessError as e:
                print("Database backup failed:", e)
        return f"数据库:{db}备份完成: {mysql_backup_dir}-{db}-{formatted_datetime}.sql"
 
 
 
if __name__ == '__main__':
        mysql_config = mysql_config()
        print(mysql_config)
        backup_dir = mkdir()
        print(backup_dir)
 
        sector = select()
        #循环打印所有数据,如果此代码是封装在函数内部,那么使用return的话,只会输出第一条数据,所以要在外部调用
        for row in sector:
                print(row)
        backup = backup("/opt/mysql_backup")
        print(backup)

效果

到此这篇关于Python基于docker部署的Mysql备份查询脚本的文章就介绍到这了,更多相关Python Mysql备份查询脚本内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文