-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
85 lines (70 loc) · 2.51 KB
/
backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import subprocess
import mysql.connector
import TestRecordBase
# 备份数据库
def backup_database():
try:
# 使用mysqldump备份数据库
global host, user, password, database, backup_file
subprocess.run(['mysqldump', '-h', host, '-u', user, '-p' + password, database, '--result-file=' + backup_file])
print("Backup successful.")
except Exception as e:
print("Backup failed:", str(e))
#恢复数据库
def restore_database():
try:
global host, user, password, database, backup_file
# 连接到数据库
conn = mysql.connector.connect(host=host, user=user, password=password)
cursor = conn.cursor()
# 创建数据库
cursor.execute("CREATE DATABASE IF NOT EXISTS {}".format(database))
# 选择数据库
cursor.execute("USE {}".format(database))
# 恢复数据库
with open(backup_file, 'r', encoding='utf-8') as f:
sql_commands = f.read().split(';')
for sql_command in sql_commands:
cursor.execute(sql_command.strip())
if sql_command.strip().lower().startswith("select"):
cursor.fetchall() # 只在执行查询语句后清空结果集
conn.commit()
TestRecordBase.create_trigger()
#conn.close()
print("Restore successful.")
except Exception as e:
print("Restore failed:", str(e))
'''
def restore_database():
try:
global host, user, password, database, backup_file
# 连接到数据库
conn = mysql.connector.connect(host=host, user=user, password=password)
cursor = conn.cursor()
# 创建数据库
cursor.execute("CREATE DATABASE IF NOT EXISTS {}".format(database))
# 选择数据库
cursor.execute("USE {}".format(database))
# 恢复数据库
with open(backup_file, 'r', encoding='utf-8') as f:
sql_commands = f.read().split(';')
for sql_command in sql_commands:
if sql_command.strip(): # 确保不是空语句
cursor.execute(sql_command.strip())
conn.commit()
TestRecordBase.create_trigger()
conn.close()
print("Restore successful.")
except Exception as e:
print("Restore failed:", str(e))
'''
# 设置数据库连接参数
host = 'localhost'
user = 'root'
password = '1111'
database = 'facereco'
backup_file = './backup.sql'
# 备份数据库
#backup_database()
# 恢复数据库
#restore_database()