Skip to content

Commit 0e9a162

Browse files
author
xujie
committed
feat: 备份和恢复
1 parent 6cf6a04 commit 0e9a162

File tree

7 files changed

+77
-8
lines changed

7 files changed

+77
-8
lines changed

.github/workflows/main.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ jobs:
3737
- name: Copy files to server
3838
run: |
3939
scp -P 2222 demo-app.tar root@xujie.i234.me:/data/docker-images/
40-
scp -P 2222 docker-compose.yml root@xujie.i234.me:/data/docker-images/
40+
scp -P 2222 docker-compose.yml root@xujie.i234.me:/data/projects/spring-boot-template/
41+
scp -P 2222 backup.sh restore.sh root@xujie.i234.me:/data/projects/spring-boot-template/ && ssh -p 2222 root@xujie.i234.me "chmod +x /data/projects/spring-boot-template/{backup,restore}.sh"
42+
4143
4244
- name: Load image and restart services
4345
run: |
44-
ssh -p 2222 root@xujie.i234.me << 'EOF'
45-
cd /data/docker-images/
46-
docker load -i demo-app.tar
47-
docker-compose down
48-
docker-compose up -d
46+
ssh -p 2222 root@xujie.i234.me << EOF
47+
cd /data/docker-images/
48+
docker load -i demo-app.tar
49+
cd /data/projects/spring-boot-template/
50+
docker-compose down
51+
docker-compose up -d
52+
docker image prune -f # 清理无用镜像
53+
rm -f vue3-template.tar # 新增清理tar文件
4954
EOF

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ build/
3434

3535
# GitHub Actions Keys
3636
github-actions-key
37-
github-actions-key.pub
37+
github-actions-key.pub
38+
39+
/mysql_backups/

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,10 @@ GET /recipes/list/search?name=面条&description=辣&ingredients=肉丝 青椒
177177
- [AuthController](src/main/java/com/example/demo/controller/AuthController.java)
178178

179179
## Actions
180-
- [main.yml](./.github/workflows/main.yml)
180+
- [main.yml](./.github/workflows/main.yml)
181+
182+
## 备份数据库
183+
- [backup.sh](./backup.sh)
184+
185+
## 恢复数据库
186+
- [restore.sh](./restore.sh)

backup.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# 定义变量
4+
CONTAINER_NAME=spring-boot-template-mysql # MySQL 容器名称
5+
BACKUP_DIR=./mysql_backups # 备份文件存储目录
6+
TIMESTAMP=$(date +"%F_%T") # 当前时间戳,格式为 YYYY-MM-DD_HH:MM:SS
7+
BACKUP_FILE=${BACKUP_DIR}/backup_${TIMESTAMP}.sql # 备份文件路径
8+
9+
# 创建备份目录(如果不存在)
10+
mkdir -p ${BACKUP_DIR}
11+
12+
# 执行备份
13+
# 使用 docker exec 在容器内执行 mysqldump 命令
14+
# --no-tablespaces 选项避免表空间权限问题
15+
# -u 指定用户名,--password 指定密码
16+
# 将输出重定向到备份文件
17+
docker exec -i ${CONTAINER_NAME} bash -c 'mysqldump --no-tablespaces -u myuser --password=secret mydatabase' > ${BACKUP_FILE}
18+
19+
# 删除超过30天的备份文件
20+
# find 命令查找备份目录中超过30天的 .sql 文件并删除
21+
find ${BACKUP_DIR} -type f -name "*.sql" -mtime +30 -exec rm {} \;
22+

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
services:
22
mysql:
33
image: 'mysql:latest'
4+
container_name: spring-boot-template-mysql
45
environment:
56
- 'MYSQL_DATABASE=mydatabase'
67
- 'MYSQL_PASSWORD=secret'

restore.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
# 首先给脚本执行权限:
4+
# chmod +x restore.sh
5+
6+
# 定义变量
7+
CONTAINER_NAME=spring-boot-template-mysql # MySQL 容器名称
8+
BACKUP_DIR=./mysql_backups # 备份文件存储目录
9+
10+
# 检查是否提供了备份文件名
11+
if [ -z "$1" ]; then
12+
echo "请指定要恢复的备份文件名"
13+
echo "用法: ./restore.sh <备份文件名>"
14+
exit 1
15+
fi
16+
17+
BACKUP_FILE=${BACKUP_DIR}/$1
18+
19+
# 检查备份文件是否存在
20+
if [ ! -f "$BACKUP_FILE" ]; then
21+
echo "错误:备份文件 $BACKUP_FILE 不存在"
22+
exit 1
23+
fi
24+
25+
# 执行恢复
26+
echo "正在恢复数据库..."
27+
docker exec -i ${CONTAINER_NAME} mysql -u myuser --password=secret mydatabase < ${BACKUP_FILE}
28+
29+
if [ $? -eq 0 ]; then
30+
echo "数据库恢复成功!"
31+
else
32+
echo "数据库恢复失败!"
33+
fi

workspace/spring-boot-template/.github/workflows/main.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)