抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

使用docker部署应用有着各种优势,但其挂载卷volume若是都交由docker管理,备份时无法使用拷贝文件、粘贴到新服务器这种方式去备份还原。
因此本文阐述如何通过正确的姿势去备份和还原使用了docker volume的应用。

一、备份volume

1、编写备份脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for i in "$@"; do
echo "开始备份:"$i
# 创建
docker run -it -d --name container_back -v $i:/volume alpine >/dev/null 2>&1
# 打包压缩
docker exec -it container_back tar -cjf /image_volume_back.tar.bz2 -C /volume ./ >/dev/null 2>&1
# 打包成一个新的images
docker commit -p container_back container_back >/dev/null 2>&1
# 将images导出为文件
docker save -o $i.tar container_back >/dev/null 2>&1
docker stop container_back >/dev/null 2>&1
docker rm container_back >/dev/null 2>&1
echo "完成备份:"$i
done

2、查看待备份volumn

1
2
3
4
5
6
7
8
docker volume ls
## 结果如下
DRIVER VOLUME NAME
local community_db_community-minio-config
local community_db_community-minio-data
local community_db_community-mysql-data
local community_db_community-redis-data
local portainer_data

3、开始备份

拼接要备份volumn到命令后,以空格隔开

1
sh back.sh community_db_community-minio-config community_db_community-minio-data community_db_community-mysql-data community_db_community-redis-data

4、查看备份结果

备份成功会打成压缩包,查看压缩包大小,大于1KB以上一般就算备份成功

1
2
3
ls
## 结果如下
back.sh community_db_community-minio-config.tar community_db_community-minio-data.tar community_db_community-mysql-data.tar community_db_community-redis-data.tar

二、备份stack脚本

文中以community项目为例
登录ptr管理面板

community.yml

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
version: '3.7'
services:
fxcloud-register:
image: registry.cn-hangzhou.aliyuncs.com/fx-community/register:2.0.0
ports:
- 8848:8848
environment:
MYSQL_PASSWORD: zgfxrc.sql2020
networks:
- community-network
deploy:
update_config:
delay: 5s
order: start-first

fxcloud-gateway:
image: registry.cn-hangzhou.aliyuncs.com/fx-community/gateway:2.0.0
ports:
- 8849:8849
networks:
- community-network
environment:
MYSQL_PASSWORD: zgfxrc.sql2020
deploy:
update_config:
delay: 5s
order: start-first

fxcloud-auth:
image: registry.cn-hangzhou.aliyuncs.com/fx-community/auth:2.0.0
networks:
- community-network
environment:
MYSQL_PASSWORD: zgfxrc.sql2020
deploy:
update_config:
delay: 5s
order: start-first

fxcloud-upms:
image: registry.cn-hangzhou.aliyuncs.com/fx-community/upms:2.0.0
networks:
- community-network
environment:
MYSQL_PASSWORD: zgfxrc.sql2020
deploy:
mode: replicated
replicas: 1
update_config:
delay: 5s
order: start-first

fxcloud-community:
image: registry.cn-hangzhou.aliyuncs.com/fx-community/community:3.2.0
networks:
- community-network
environment:
MYSQL_PASSWORD: zgfxrc.sql2020
deploy:
mode: replicated
replicas: 1
update_config:
delay: 5s
order: start-first

fxcloud-visitor:
image: registry.cn-hangzhou.aliyuncs.com/fx-community/visitor:2.3.0
networks:
- community-network
environment:
MYSQL_PASSWORD: zgfxrc.sql2020
deploy:
update_config:
delay: 5s
order: start-first

networks:
community-network:
external: true

community_db.yml

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
version: '3'
services:
fxcloud-mysql:
image: mysql:8.0.17
ports:
- 23306:3306
volumes:
- community-mysql-data:/var/lib/mysql
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
environment:
- MYSQL_ROOT_PASSWORD=zgfxrc.cn
- TZ=Asia/Shanghai
networks:
- community-network
deploy:
placement:
constraints:
- node.role == manager

fxcloud-redis:
image: redis:5.0.5
#command: redis-server --port 6379 --requirepass zgfxrc.cn --appendonly yes
ports:
- 6379:6379
volumes:
- community-redis-data:/data
networks:
- community-network
deploy:
placement:
constraints:
- node.role == manager

fxcloud-minio:
image: huanghs/minio:2.2.0
command: server /data
ports:
- 9000:9000
volumes:
- community-minio-data:/data
- community-minio-config:/root/.minio
environment:
- MINIO_DOMAIN=fxcloud-minio
- MINIO_ACCESS_KEY=huanghaisong
- MINIO_SECRET_KEY=huanghaisong
networks:
- community-network
deploy:
placement:
constraints:
- node.role == manager

networks:
community-network:
external: true

volumes:
community-mysql-data:
community-redis-data:
community-minio-data:
community-minio-config:

三、备份前端文件及nginx的conf

使用宝塔的登录拷贝即可,未使用宝塔的如下

1、备份nginx conf文件

例如nginx安装在/etc/nginx目录下

1
2
3
4
cd /etc/nginx/conf.d/
ls
##
firm.conf park.conf

备份其中firm.confpark.conf文件

2、备份前端文件

查看firm.conf映射的root目录,备份即可
如park.conf中的root可能为root /root/workspace/community-ui/dist;,则备份该目录的所有文件

四、还原volume

1、编写还原脚本

在目录下创建reduct.sh文件并填入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
for i in "$@"; do
echo "开始还原:"$i
# 建立 Docker Volume
docker volume create $i

# 还原文件为 Image
docker load -i $i.tar

# 挂载 Docker Volume之后,透过命令将原先 Image 内的压缩档解压缩到 Docker Volume,完成之后自动删掉 Alpine 的 Container
docker run --rm -v $i:/volume container_back sh -c "rm -rf /volume/* /volume/..?* /volume/.[!.]* ; tar -C /volume/ -xjf /image_volume_back.tar.bz2 ;"
docker rmi container_back
echo "完成还原:"$i
done

2、将tar传到新服务器

3、执行还原指令

1
sh reduct.sh community_db_community-minio-config community_db_community-minio-data community_db_community-mysql-data community_db_community-redis-data

4、检查还原的voume大小是否与旧服务器的一致

1
2
3
4
5
6
7
8
9
10
docker system df -v
### 末尾会看到
Local Volumes space usage:

VOLUME NAME LINKS SIZE
community_db_community-minio-config 0 0B
community_db_community-minio-data 0 19.91MB
community_db_community-mysql-data 0 286.8MB
community_db_community-redis-data 0 13.64kB

五、还原stack服务

登录ptr,按照所备份的stack yml创建即可,需注意

  • yml中的镜像是否有权限
  • yml中的networks是否已创建,未创建则创建同名的overlay类型网络

六、还原前端文件及配置nginx

如何备份则如何还原即可

评论