有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准
https://blog.zysicyj.top
最后修改日期:2023 年 7 月 5 日
尽管 Docker Compose 安装会自动将 Space 数据保存在持久性 Docker 卷中,但仍建议定期备份数据。这样,您可以在出现意外问题时恢复 Space 安装。
哪些数据值得备份
Docker Compose 安装使用数据存储中涉及的四个服务。每个服务都在单独的 Docker 容器中运行:
PostgreSQL 数据库 – 存储 Space 用户数据;强烈建议您备份此数据库。了解更多信息
MinIO 对象存储 – 存储空间文件;我们强烈建议您备份此存储。了解更多信息
Redis – 存储空间缓存;无需备份。
Elasticsearch – 存储空间搜索索引;对于数据量有限的小公司来说,备份 Elasticsearch 可能不是必需的,因为重建索引应该不会花费太长时间。
warning
对象存储和数据库中的数据是链接在一起的,因此必须以正确的顺序备份它们,以保持所有内容的一致性。首先,您应该备份 PostgreSQL 数据库,然后备份 MinIO 对象存储。
备份和还原 PostgreSQL 数据库
备份 PostgreSQL 数据库
在主机上,运行:
docker exec -t $DB_CONTAINER_ID pg_dump -U $DB_USERNAME -d $DB_NAME > spacedb_dump.sql这里:
DB_CONTAINER_ID是 postgres 容器的 ID。您可以使用以下命令获取 ID。docker psDB_USERNAME是用于安装的文件的值。默认情况下,.services.postgres.environment.POSTGRES_USER``docker-compose.yml``spaceDB_NAME是用于安装的文件的值。默认情况下,.services.postgres.environment.POSTGRES_DB``docker-compose.yml``spacedb
此命令在 PostgreSQL 容器内运行命令行工具以执行数据库转储。转储文件将保存到当前目录。
pg_dump``spacedb_dump.sql
还原 PostgreSQL 数据库
如果 Space 实例正在运行,请停止该实例:
docker-compose -p space-on-premises down docker-compose -p space-on-premises rm -f由于 PostrgreSQL 容器也已停止,因此应使用以下命令重新启动它:
docker compose up postgres跑:
docker exec -i $DB_CONTAINER_ID psql -U $DB_USERNAME -d $DB_NAME < spacedb_dump.sql此命令将之前创建的转储导入到 PostgreSQL 容器中的空数据库。
spacedb_dump.sql启动 Space 实例:
自动备份和还原 PostgreSQL 数据库
您可以使用 Bash 脚本自动执行备份和还原过程。例如:
#!/bin/bash DB_CONTAINER_ID="$2" DB_USERNAME="$3" DB_NAME="$4" function show_usage() { echo "Usage:" echo "$0 backup [DB_CONTAINER_ID] [DB_USERNAME] [DB_NAME] : Back up Space DB as spacedb_dump.sql to PWD" echo "$0 restore [DB_CONTAINER_ID] [DB_USERNAME] [DB_NAME] : Take spacedb_dump.sql from PWD and restore Space DB" } if [ $# -ne 4 ]; then # incorrect number of arguments echo "Error: Not all arguments are specified" show_usage exit 1 fi case "$1" in backup) echo "Backing up..." docker exec -t $DB_CONTAINER_ID pg_dump -U $DB_USERNAME -d $DB_NAME > spacedb_dump.sql echo "Backup created" ;; restore) echo "Restoring..." docker exec -i $DB_CONTAINER_ID psql -U $DB_USERNAME -d $DB_NAME < spacedb_dump.sql echo "Restore completed" ;; *) # invalid argument echo "Invalid command" show_usage exit 1 ;; esac
Back up and restore MinIO storage
To back up and restore the MinIO object storage, you will need the MinIO Client. You can run it either on the host machine that runs Space, or on the host machine that runs the MinIO container (if you run it separately).
To back up the MinIO storage
Make sure you performed the backup of the PostgreSQL database.
Add the MinIO host machine to the MinIO Client configuration:
mc alias set <ALIAS> <URL> <ACCESSKEY> <SECRETKEY>For example, for the default installation (Space runs as ):
localhostmc alias set space-minio http://localhost:9000 space-access-key space-secret-keyMirror the data stored in the MinIO buckets to the backup directory:
mc mirror <ALIAS> <BACKUP/DIRECTORY>After the backup is complete, the backup directory must contain the following subdirectories: , , , , , and .
automation-dsl-local``automation-fileshare-local``automation-logs-local``packages-local``space-local``vcs-local
To restore the MinIO storage
If the restore process is performed on a separate host machine, make sure that the MinIO Client is installed and the MinIO host is added as an alias to the configuration (step 2 of the backup process).
Stop your Space instance if it is running:
docker-compose -p space-on-premises down docker-compose -p space-on-premises rm -fAs the MinIO container is also stopped, you should start it again with:
Create the MinIO buckets (the command will ignore the existing buckets):
mc mb --ignore-existing <ALIAS>/automation-dsl-local <ALIAS>/automation-fileshare-local <ALIAS>/automation-logs-local <ALIAS>/packages-local <ALIAS>/space-local <ALIAS>/vcs-localRestore the MinIO data:
mc mirror <BACKUP/DIRECTORY> <ALIAS>Start your Space instance:
To automate backup and restore of the MinIO storage
You can automate the process of backup and restore with a Bash script. For example:
#!/bin/bash # Bash script configuring MinIO host and mirror data # Display usage instructions function show_usage() { echo "Usage:" echo "$0 add-host <minio_alias> <minio_url> <minio_access_key> <minio_secret_key> : Configure MinIO host" echo "$0 backup <minio_alias> <backup_dir> : Mirror data from MinIO to backup directory" echo "$0 restore <backup_dir> <minio_alias> : Restore data from backup directory to MinIO" } # Check the number of arguments if [ $# -lt 2 ]; then echo "Error: Invalid number of arguments" show_usage exit 1 fi # Execute add-host, backup, and restore commands case "$1" in add-host) if [ $# -ne 5 ]; then echo "Error: Invalid number of arguments for 'configure-host' command" show_usage exit 1 fi alias="$2" url="$3" access_key="$4" secret_key="$5" mc alias set $alias $url $access_key $secret_key ;; backup) if [ $# -ne 3 ]; then echo "Error: Invalid number of arguments for 'backup' command" show_usage exit 1 fi backup_dir="$3" echo "Mirroring data from MinIO to backup directory..." mc mirror $alias $backup_dir ;; restore) if [ $# -ne 3 ]; then echo "Error: Invalid number of arguments for 'restore' command" show_usage exit 1 fi backup_dir="$2" alias="$3" echo "Restoring data from backup directory to MinIO..." mc mb --ignore-existing $alias/automation-dsl-local $alias/automation-fileshare-local $alias/automation-logs-local $alias/packages-local $alias/space-local $alias/vcs-local mc mirror $backup_dir $alias ;; *) echo "Error: Unknown command '$command'" show_usage exit 1 ;; esac


