mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-08 18:49:09 +00:00
+1
-2
@@ -44,5 +44,4 @@ docker/nginx/ssl/*
|
|||||||
frontend/.env.development
|
frontend/.env.development
|
||||||
frontend/.env.production
|
frontend/.env.production
|
||||||
|
|
||||||
# 备份文件
|
|
||||||
backups/
|
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ check_permissions() {
|
|||||||
log "==========🔍 检查系统依赖...==========" "INFO"
|
log "==========🔍 检查系统依赖...==========" "INFO"
|
||||||
|
|
||||||
# 创建必要的持久化目录
|
# 创建必要的持久化目录
|
||||||
for dir in "${DOCKER_DIR}/mysql/data" "${DOCKER_DIR}/redis/data" "${WORK_DIR}/backups" ; do
|
for dir in "${DOCKER_DIR}/mysql/data" "${DOCKER_DIR}/redis/data" ; do
|
||||||
if [ ! -d "$dir" ]; then
|
if [ ! -d "$dir" ]; then
|
||||||
mkdir -p "$dir"
|
mkdir -p "$dir"
|
||||||
log "📁 创建目录: ${dir}" "INFO"
|
log "📁 创建目录: ${dir}" "INFO"
|
||||||
@@ -382,43 +382,6 @@ restart_containers() {
|
|||||||
docker compose ps
|
docker compose ps
|
||||||
}
|
}
|
||||||
|
|
||||||
# 备份数据库
|
|
||||||
backup_database() {
|
|
||||||
log "==========💾 备份数据库...==========" "INFO"
|
|
||||||
local backup_dir="${WORK_DIR}/backups"
|
|
||||||
local timestamp=$(date '+%Y%m%d_%H%M%S')
|
|
||||||
mkdir -p "${backup_dir}"
|
|
||||||
|
|
||||||
if docker compose ps mysql --format '{{.Status}}' 2>/dev/null | grep -q "healthy"; then
|
|
||||||
local db_user="${MYSQL_USER:-fastapiadmin}"
|
|
||||||
local db_password="${MYSQL_PASSWORD}"
|
|
||||||
local db_name="${MYSQL_DATABASE:-fastapiadmin}"
|
|
||||||
local backup_file="${backup_dir}/${db_name}_${timestamp}.sql.gz"
|
|
||||||
|
|
||||||
log "📦 备份数据库 ${db_name} 到 ${backup_file}" "INFO"
|
|
||||||
docker compose exec -T mysql mysqldump \
|
|
||||||
-u"${db_user}" -p"${db_password}" \
|
|
||||||
--single-transaction \
|
|
||||||
--routines \
|
|
||||||
--triggers \
|
|
||||||
--events \
|
|
||||||
"${db_name}" | gzip > "${backup_file}"
|
|
||||||
log "✅ 数据库备份完成: ${backup_file}" "SUCCESS"
|
|
||||||
|
|
||||||
# 保留最近 7 天备份,清理旧的
|
|
||||||
find "${backup_dir}" -name "*.sql.gz" -type f -mtime +7 -delete
|
|
||||||
log "🗑️ 已清理 7 天前的旧备份" "INFO"
|
|
||||||
else
|
|
||||||
log "⚠️ MySQL 未运行,跳过数据库备份" "WARN"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 备份 .env 文件
|
|
||||||
if [ -f "${ENV_FILE}" ]; then
|
|
||||||
cp "${ENV_FILE}" "${backup_dir}/.env.backup.${timestamp}"
|
|
||||||
log "✅ 环境变量配置已备份" "INFO"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# 清理旧镜像
|
# 清理旧镜像
|
||||||
clear_old_images() {
|
clear_old_images() {
|
||||||
log "==========🗑️ 清理旧镜像...==========" "INFO"
|
log "==========🗑️ 清理旧镜像...==========" "INFO"
|
||||||
@@ -530,11 +493,6 @@ main() {
|
|||||||
check_permissions
|
check_permissions
|
||||||
log "✅ ${DEPLOY_STATUS}完成" "INFO"
|
log "✅ ${DEPLOY_STATUS}完成" "INFO"
|
||||||
|
|
||||||
DEPLOY_STATUS="备份数据库"
|
|
||||||
log "==========💾 开始${DEPLOY_STATUS}...==========" "INFO"
|
|
||||||
backup_database
|
|
||||||
log "✅ ${DEPLOY_STATUS}完成" "INFO"
|
|
||||||
|
|
||||||
DEPLOY_STATUS="停止现有容器"
|
DEPLOY_STATUS="停止现有容器"
|
||||||
log "==========⏹️ 开始${DEPLOY_STATUS}...==========" "INFO"
|
log "==========⏹️ 开始${DEPLOY_STATUS}...==========" "INFO"
|
||||||
stop_containers
|
stop_containers
|
||||||
@@ -664,14 +622,13 @@ while [[ $# -gt 0 ]]; do
|
|||||||
echo " 1. 检查脚本权限"
|
echo " 1. 检查脚本权限"
|
||||||
echo " 2. 加载环境变量"
|
echo " 2. 加载环境变量"
|
||||||
echo " 3. 检查系统依赖"
|
echo " 3. 检查系统依赖"
|
||||||
echo " 4. 备份数据库"
|
echo " 4. 停止现有容器"
|
||||||
echo " 5. 停止现有容器"
|
echo " 5. 更新代码"
|
||||||
echo " 6. 更新代码"
|
echo " 6. 构建前端(默认跳过,使用已上传的静态文件)"
|
||||||
echo " 7. 构建前端(默认跳过,使用已上传的静态文件)"
|
echo " 7. 构建镜像"
|
||||||
echo " 8. 构建镜像"
|
echo " 8. 启动容器"
|
||||||
echo " 9. 启动容器"
|
echo " 9. 验证部署"
|
||||||
echo " 10. 验证部署"
|
echo " 10. 显示日志"
|
||||||
echo " 11. 显示日志"
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "日志查看命令:"
|
echo "日志查看命令:"
|
||||||
echo " 查看实时日志:docker compose logs -f [服务名]"
|
echo " 查看实时日志:docker compose logs -f [服务名]"
|
||||||
|
|||||||
+11
-47
@@ -86,14 +86,13 @@ docker/
|
|||||||
1. 检查脚本权限
|
1. 检查脚本权限
|
||||||
2. 加载环境变量
|
2. 加载环境变量
|
||||||
3. 检查系统依赖并创建必要目录
|
3. 检查系统依赖并创建必要目录
|
||||||
4. **备份数据库**(备份到 `backups/` 目录,保留最近 7 天)
|
4. 停止现有容器
|
||||||
5. 停止现有容器
|
5. 更新代码
|
||||||
6. 更新代码
|
6. 构建前端(可选,默认跳过)
|
||||||
7. 构建前端(可选,默认跳过)
|
7. 构建 Docker 镜像
|
||||||
8. 构建 Docker 镜像
|
8. 启动容器
|
||||||
9. 启动容器
|
9. 验证部署
|
||||||
10. 验证部署
|
10. 显示日志
|
||||||
11. 显示日志
|
|
||||||
|
|
||||||
### 2. 命令选项
|
### 2. 命令选项
|
||||||
|
|
||||||
@@ -173,39 +172,6 @@ docker compose logs -f [服务名]
|
|||||||
各容器的日志会被 Docker 自动轮转,限制为每个容器最多保留 3 个日志文件,每个最大 10MB。
|
各容器的日志会被 Docker 自动轮转,限制为每个容器最多保留 3 个日志文件,每个最大 10MB。
|
||||||
如有需要,可在 `docker-compose.yaml` 中调整 `logging` 配置。
|
如有需要,可在 `docker-compose.yaml` 中调整 `logging` 配置。
|
||||||
|
|
||||||
## 数据库备份与恢复
|
|
||||||
|
|
||||||
### 自动备份
|
|
||||||
|
|
||||||
执行完整部署流程时,脚本会自动在停服前备份数据库,备份文件保存在 `backups/` 目录:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
backups/
|
|
||||||
├── fastapiadmin_20240101_120000.sql.gz
|
|
||||||
├── fastapiadmin_20240102_120000.sql.gz
|
|
||||||
└── .env.backup.20240101_120000 # 环境变量备份
|
|
||||||
```
|
|
||||||
|
|
||||||
自动清理策略:保留最近 **7 天** 的备份。
|
|
||||||
|
|
||||||
### 手动备份
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd docker
|
|
||||||
docker compose exec mysql mysqldump \
|
|
||||||
-u fastapiadmin -p'your_password' \
|
|
||||||
--single-transaction --routines --triggers \
|
|
||||||
fastapiadmin | gzip > ../backups/manual_backup.sql.gz
|
|
||||||
```
|
|
||||||
|
|
||||||
### 恢复备份
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd docker
|
|
||||||
cat ../backups/fastapiadmin_20240101_120000.sql.gz | gunzip | \
|
|
||||||
docker compose exec -T mysql mysql -u fastapiadmin -p'your_password' fastapiadmin
|
|
||||||
```
|
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
|
||||||
### 1. 环境变量文件不存在
|
### 1. 环境变量文件不存在
|
||||||
@@ -257,8 +223,7 @@ sudo chmod -R 755 docker/mysql/data
|
|||||||
2. **保护 `.env` 文件**:本部署已将 `docker/.env` 加入 `.gitignore`,请勿手动提交
|
2. **保护 `.env` 文件**:本部署已将 `docker/.env` 加入 `.gitignore`,请勿手动提交
|
||||||
3. **使用正规 SSL 证书**:生产中不要使用自签名证书
|
3. **使用正规 SSL 证书**:生产中不要使用自签名证书
|
||||||
4. **限制端口访问**:通过防火墙只开放 80/443 端口
|
4. **限制端口访问**:通过防火墙只开放 80/443 端口
|
||||||
5. **定期备份**:建议将 `backups/` 目录定期同步到异地存储
|
5. **及时更新**:定期执行部署脚本获取最新版本
|
||||||
6. **及时更新**:定期执行部署脚本获取最新版本
|
|
||||||
|
|
||||||
## 版本更新
|
## 版本更新
|
||||||
|
|
||||||
@@ -269,7 +234,6 @@ sudo chmod -R 755 docker/mysql/data
|
|||||||
```
|
```
|
||||||
|
|
||||||
脚本会自动:
|
脚本会自动:
|
||||||
1. 备份当前数据库
|
1. 拉取最新代码
|
||||||
2. 拉取最新代码
|
2. 构建新镜像
|
||||||
3. 构建新镜像
|
3. 重启容器
|
||||||
4. 重启容器
|
|
||||||
|
|||||||
+3
-11
@@ -120,7 +120,7 @@ http {
|
|||||||
root /usr/share/nginx/html/docs/dist;
|
root /usr/share/nginx/html/docs/dist;
|
||||||
index index.html;
|
index index.html;
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
expires 1h;
|
expires 30d;
|
||||||
add_header Cache-Control "public, no-transform";
|
add_header Cache-Control "public, no-transform";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +129,7 @@ http {
|
|||||||
alias /usr/share/nginx/html/web/dist;
|
alias /usr/share/nginx/html/web/dist;
|
||||||
index index.html;
|
index index.html;
|
||||||
try_files $uri $uri/ /web/index.html;
|
try_files $uri $uri/ /web/index.html;
|
||||||
expires 1h;
|
expires 30d;
|
||||||
add_header Cache-Control "public, no-transform";
|
add_header Cache-Control "public, no-transform";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +138,7 @@ http {
|
|||||||
alias /usr/share/nginx/html/app/dist/build/h5;
|
alias /usr/share/nginx/html/app/dist/build/h5;
|
||||||
index index.html;
|
index index.html;
|
||||||
try_files $uri $uri/ /app/index.html;
|
try_files $uri $uri/ /app/index.html;
|
||||||
expires 1h;
|
expires 30d;
|
||||||
add_header Cache-Control "public, no-transform";
|
add_header Cache-Control "public, no-transform";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,14 +175,6 @@ http {
|
|||||||
proxy_cache off;
|
proxy_cache off;
|
||||||
}
|
}
|
||||||
|
|
||||||
# ==================== 静态资源缓存 ====================
|
|
||||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
|
||||||
expires 30d;
|
|
||||||
add_header Cache-Control "public, immutable, no-transform";
|
|
||||||
access_log off;
|
|
||||||
log_not_found off;
|
|
||||||
}
|
|
||||||
|
|
||||||
# ==================== Nginx 健康检查 ====================
|
# ==================== Nginx 健康检查 ====================
|
||||||
location /nginx-health {
|
location /nginx-health {
|
||||||
access_log off;
|
access_log off;
|
||||||
|
|||||||
Reference in New Issue
Block a user