Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
customer-recall
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tools
customer-recall
Commits
9a6b11c4
Commit
9a6b11c4
authored
Sep 10, 2025
by
luoqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:数据库备份3
parent
a0f069dc
Pipeline
#3230
passed with stage
in 22 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
6 deletions
+29
-6
database/scripts/backup_database.py
+27
-6
docker-compose.yml
+2
-0
No files found.
database/scripts/backup_database.py
View file @
9a6b11c4
...
...
@@ -19,8 +19,9 @@ def run_command(command, description):
"""执行命令并处理错误"""
print
(
f
"🔄 {description}..."
)
try
:
# 使用bash -c来确保命令正确执行
result
=
subprocess
.
run
(
command
,
f
"bash -c '{command}'"
,
shell
=
True
,
check
=
True
,
capture_output
=
True
,
...
...
@@ -49,16 +50,36 @@ def backup_database():
timestamp
=
datetime
.
now
()
.
strftime
(
"
%
Y
%
m
%
d_
%
H
%
M
%
S"
)
backup_file
=
backup_dir
/
f
"auto_backup_{timestamp}.sql"
# 构建备份命令(容器内直接连接MySQL)
backup_command
=
f
"mysqldump -h mysql -u callback_user -pdev_password_123 callback_system > {backup_file}"
print
(
f
"📁 备份文件: {backup_file}"
)
print
(
f
"🔄 备份数据库到 {backup_file.name}..."
)
try
:
# 分步执行备份(使用已验证的方法)
# 1. 创建配置文件
config_cmd
=
"echo '[client]
\n
host=mysql
\n
user=callback_user
\n
password=dev_password_123
\n
ssl=0' > /tmp/backup.cnf"
subprocess
.
run
(
config_cmd
,
shell
=
True
,
check
=
True
)
# 2. 执行备份
backup_cmd
=
f
"mysqldump --defaults-file=/tmp/backup.cnf --single-transaction --routines --triggers callback_system > {backup_file}"
result
=
subprocess
.
run
(
backup_cmd
,
shell
=
True
,
capture_output
=
True
,
text
=
True
)
if
run_command
(
backup_command
,
f
"备份数据库到 {backup_file.name}"
):
# 3. 清理配置文件
subprocess
.
run
(
"rm -f /tmp/backup.cnf"
,
shell
=
True
)
# 检查备份文件大小
if
backup_file
.
exists
()
and
backup_file
.
stat
()
.
st_size
>
0
:
print
(
"✅ 备份数据库成功"
)
print
(
f
"📊 备份文件大小: {backup_file.stat().st_size} 字节"
)
print
(
"🎉 数据库备份完成"
)
return
True
else
:
print
(
"❌ 数据库备份失败"
)
print
(
f
"❌ 备份数据库失败"
)
if
result
.
stderr
:
print
(
f
"错误: {result.stderr}"
)
return
False
except
Exception
as
e
:
print
(
f
"❌ 备份过程中发生异常: {e}"
)
return
False
if
__name__
==
'__main__'
:
...
...
docker-compose.yml
View file @
9a6b11c4
...
...
@@ -49,6 +49,8 @@ services:
-
DB_PASSWORD=dev_password_123
-
DB_NAME=callback_system
-
DB_CHARSET=utf8mb4
volumes
:
-
./database/backups:/app/database/backups
depends_on
:
mysql
:
condition
:
service_healthy
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment