一、操作环境
1、python 3.7(个人感觉不是第2代就行)
2、tk pythom已经内置
3、pycharm 2020
4、SQLAlchemy 1.4.44
二、来个图提个神
编辑
三、上代码
import os
import tkinter
from tkinter import ttk, messagebox, filedialog
from framework.base.BaseConfig import BaseConfig
from framework.control.AdenButton import AdenButton
from framework.control.AdenCheckButton import AdenCheckButton
from framework.control.AdenEntry import AdenEntry
from framework.control.AdenLabel import AdenLabel
from framework.control.AdenMessageBox import AdenMessageBox
from framework.control.AdenTkPlugin import TkPlugin
from framework.pulgin.Tools import Tools
from operation.access.DBBackupAccess import DBBackupAccess
from operation.entity.DBBackupEntity import DBBackupEntity
class MySqlDBBackView():
dbAccess = DBBackupAccess()
action_type = "add"
def getFrame(self, root, tab):
self.note_book = tab
self.frame = tkinter.Frame(root)
frame_data_config = self.frame
frame_data_config.configure(background=TkPlugin.background())
frame_data_config.columnconfigure(1, weight=1) # 设置第2列自动适应Frame 需要sitcky布局配合
frame_data_config.columnconfigure(3, weight=1) # 设置第4列自动适应Frame
frame_data_config.rowconfigure(8, weight=1)
# 第1行
lable_sys_name = AdenLabel(frame_data_config, text="数据库类型:")
lable_sys_name.grid(row=0, column=0, sticky=tkinter.E)
self.com_database_type = ttk.Combobox(frame_data_config, state='readonly') # 邮件名称 通过自定义控件实现 读取值和获取值都可以通过控件获取
database_type = ('MySql', 'SQLSERVER')
self.com_database_type['value'] = database_type
self.com_database_type.grid(row=0, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6", columnspan=3)
self.com_database_type.current(0)
# 第2行1列
connection_string = AdenLabel(frame_data_config, text="数据库IP地址:", bg=TkPlugin.background())
connection_string.grid(row=1, column=0, sticky=tkinter.E)
self.txt_connection_ipaddress = AdenEntry(frame_data_config)
self.txt_connection_ipaddress.grid(row=1, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第2行2列
connection_name = AdenLabel(frame_data_config, text="数据库名称:", bg=TkPlugin.background())
connection_name.grid(row=1, column=2, sticky=tkinter.E)
self.txt_connection_name = AdenEntry(frame_data_config)
self.txt_connection_name.grid(row=1, column=3, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第3行1列
lable_smtp_user = AdenLabel(frame_data_config, text="用户名:", bg=TkPlugin.background())
lable_smtp_user.grid(row=2, column=0, sticky=tkinter.E)
self.txt_user_name = AdenEntry(frame_data_config)
self.txt_user_name.grid(row=2, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第3行2列
lable_smtp_pass = AdenLabel(frame_data_config, text="密码:", bg=TkPlugin.background())
lable_smtp_pass.grid(row=2, column=2, sticky=tkinter.E)
self.txt_pass_word = AdenEntry(frame_data_config)
self.txt_pass_word.grid(row=2, column=3, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第4行1
lable_dbback_path = AdenLabel(frame_data_config, text="端口号:", bg=TkPlugin.background())
lable_dbback_path.grid(row=3, column=0, sticky=tkinter.E)
self.txt_db_port = AdenEntry(frame_data_config)
self.txt_db_port.grid(row=3, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第4行2
lable_dbback_path = AdenLabel(frame_data_config, text="自动备份:", bg=TkPlugin.background())
lable_dbback_path.grid(row=3, column=2, sticky=tkinter.E)
self.ck_check_view = tkinter.IntVar()
self.check_view = AdenCheckButton(frame_data_config, variable=self.ck_check_view, text="选中是自动", fg="#0081FF")
self.check_view.grid(row=3, column=3, sticky=tkinter.W, padx="6", columnspan=2)
# 第5行第1列
lable_dbback_path = AdenLabel(frame_data_config, text="备份数量:", bg=TkPlugin.background())
lable_dbback_path.grid(row=4, column=0, sticky=tkinter.E)
self.txt_dbback_number = AdenEntry(frame_data_config)
self.txt_dbback_number.grid(row=4, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第5行第2列
lable_dbback_path = AdenLabel(frame_data_config, text="备份时间:", bg=TkPlugin.background())
lable_dbback_path.grid(row=4, column=2, sticky=tkinter.E)
self.txt_dbback_timer = AdenEntry(frame_data_config)
self.txt_dbback_timer.grid(row=4, column=3, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第6行第1列
lable_dbback_path = AdenLabel(frame_data_config, text="备份路径:", bg=TkPlugin.background())
lable_dbback_path.grid(row=5, column=0, sticky=tkinter.E)
self.txt_dbback_path = AdenEntry(frame_data_config)
self.txt_dbback_path.grid(row=5, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
# 第6行第2列
lable_dbback_path = AdenLabel(frame_data_config, text="目标邮件:", bg=TkPlugin.background())
lable_dbback_path.grid(row=5, column=2, sticky=tkinter.E)
self.txt_send_mail = AdenEntry(frame_data_config)
self.txt_send_mail.grid(row=5, column=3, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
btnbackup = AdenButton(frame_data_config, text="备份", width=8, bg="#FF1493", command=lambda: self.run_backup())
btnbackup.grid(row=9, column=2, sticky=tkinter.E, columnspan="2", padx="210")
btnSave = AdenButton(frame_data_config, text="保存", width=8, command=lambda: self.db_config_save())
btnSave.grid(row=9, column=2, sticky=tkinter.E, columnspan="2", padx="140")
btnTest = AdenButton(frame_data_config, text="测试链接", bg="#FFA500", width=8,
command=lambda: self.db_config_test())
btnTest.grid(row=9, column=2, sticky=tkinter.E, columnspan="2", padx="70")
btnDelete = AdenButton(frame_data_config, text="关闭窗体", bg="#D9534F", width=8, command=lambda: self.forget())
btnDelete.grid(row=9, column=3, sticky=tkinter.E)
self.initialize_database_config()
return self.frame
def initialize_database_config(self):
dict_result = self.dbAccess.query()
if dict_result["result"]:
entity = dict_result["datas"]
if entity is None:
self.action_type = "add"
self.txt_dbback_path.SetValue(BaseConfig().ROOT_PATH os.sep "DBBackup")
else:
self.action_type = entity.backup_id
self.com_database_type.set(entity.db_type)
self.txt_connection_ipaddress.SetValue(entity.db_url)
self.txt_connection_name.SetValue(entity.db_name)
self.txt_user_name.SetValue(entity.db_user)
self.txt_pass_word.SetValue(entity.db_pass)
self.txt_db_port.SetValue( entity.db_port)
self.txt_dbback_path.SetValue(entity.backup_path)
self.txt_dbback_timer.SetValue(entity.backup_cycle)
self.txt_dbback_number.SetValue(entity.backup_number)
if entity.backup_auto.lower() == "true":
self.ck_check_view.set(1)
else:
self.ck_check_view.set(0)
self.txt_send_mail.SetValue(entity.target_mail)
else:
messagebox.showinfo('提示信息', '数据加载失败' dict_result["datas"])
def run_backup(self):
db_type = self.com_database_type.get()
server = self.txt_connection_ipaddress.GetValue()
folder = self.txt_dbback_path.GetValue()
user = self.txt_user_name.GetValue()
pwd = self.txt_pass_word.GetValue()
port= self.txt_db_port.GetValue()
dbname= self.txt_connection_name.GetValue()
folder=folder os.sep dbname Tools.get_guid() ".sql"
str_sql = "mysqldump -h {0} -P {1} -u {2} -p{3} --databases {4} > {5}".format(server, port, user, pwd, dbname,
folder)
print(str_sql)
os.system(str_sql)
messagebox.showinfo('提示信息', '备份成功!!!')
def db_config_save(self):
entity = DBBackupEntity()
entity.db_name = self.txt_connection_name.GetValue()
entity.db_type = self.com_database_type.get()
entity.db_user = self.txt_user_name.GetValue()
entity.db_pass = self.txt_pass_word.GetValue()
entity.db_url = self.txt_connection_ipaddress.GetValue()
entity.db_port = self.txt_db_port.GetValue()
entity.backup_path = self.txt_dbback_path.GetValue()
entity.create_datetime = Tools.get_current_datetime()
entity.backup_cycle = self.txt_dbback_timer.GetValue()
entity.backup_number = self.txt_dbback_number.GetValue()
if self.ck_check_view.get() == 1:
entity.backup_auto = "true"
else:
entity.backup_auto = "false"
entity.target_mail = self.txt_send_mail.GetValue()
if self.action_type=="add":
entity.backup_id = Tools.get_guid()
dict_result = self.dbAccess.save(entity)
if dict_result["result"]:
messagebox.showinfo('提示信息', '保存成功!!!')
else:
messagebox.showinfo('提示信息', '保存失败,错误原因如下:' dict_result["datas"])
else:
entity.backup_id = self.action_type
dict_result = self.dbAccess.update(entity)
if dict_result["result"]:
messagebox.showinfo('提示信息', '保存成功!!!')
else:
messagebox.showinfo('提示信息', '保存失败,错误原因如下:' dict_result["datas"])
pass
def db_config_test(self):
"""
测试数据库链接
:return:
"""
db_type = self.com_database_type.get()
db_conn_ipaddress = self.txt_connection_ipaddress.GetValue()
db_conn_name = self.txt_connection_name.GetValue()
db_user_name = self.txt_user_name.GetValue()
db_pass_word = self.txt_pass_word.GetValue()
dict_result = self.dbAccess.test_connection(db_type, db_conn_ipaddress, db_conn_name, db_user_name, db_pass_word)
if dict_result["result"]:
AdenMessageBox.show('连接成功!!!')
else:
AdenMessageBox.show('数据库链接不上,错误原因如下:' dict_result["datas"])
pass
def forget(self):
self.note_book.forget(self.frame)
五、其实就是使用Mysql的备份语句备份的数据库
mysqldump -h 127.0.0.1 -P 3306 -u root -p123123 --databases yadinghao > D:FreedomDBBackyadinghaoE72E3A1C-9EB4-11ED-B855-709CD1DEFC28.sql
mysqldump 个命令需要将mysql的bin路径配置到环境变量里,我这里就不说了。网上一大堆......