页面加载中
博客快捷键
按住 Shift 键查看可用快捷键
ShiftK
开启/关闭快捷键功能
ShiftA
打开/关闭中控台
ShiftD
深色/浅色显示模式
ShiftS
站内搜索
ShiftR
随机访问
ShiftH
返回首页
ShiftL
友链页面
ShiftP
关于本站
ShiftI
原版/本站右键菜单
松开 Shift 键或点击外部区域关闭
互动
最近评论
暂无评论
标签
寻找感兴趣的领域
暂无标签
    0
    文章
    0
    标签
    8
    分类
    10
    评论
    128
    功能
    深色模式
    标签
    JavaScript12TypeScript8React15Next.js6Vue10Node.js7CSS5前端20
    互动
    最近评论
    暂无评论
    标签
    寻找感兴趣的领域
    暂无标签
      0
      文章
      0
      标签
      8
      分类
      10
      评论
      128
      功能
      深色模式
      标签
      JavaScript12TypeScript8React15Next.js6Vue10Node.js7CSS5前端20
      未知歌曲
      未播放
      ♪ 暂无歌词 ♪
      随便逛逛
      博客分类
      文章标签
      复制地址
      深色模式
      AnHeYuAnHeYu
      Search⌘K
      博客
        暂无其他文档

        3备份和回复

        本文介绍了 Docker Compose 安装 Space 时的数据备份与恢复方法。重点需备份 PostgreSQL 数据库(用户数据)和 MinIO 对象存储(文件),两者需按顺序备份以保证一致性。文章提供了手动备份/恢复 PostgreSQL 的具体命令,以及自动化备份的建议,同时指出 Redis 和 Elasticsearch 通常无需备份。

        November 21, 20237 分钟 阅读4 次阅读

        有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准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 ps

          • DB_USERNAME是用于安装的文件的值。默认情况下,.services.postgres.environment.POSTGRES_USER``docker-compose.yml``space

          • DB_NAME是用于安装的文件的值。默认情况下,.services.postgres.environment.POSTGRES_DB``docker-compose.yml``spacedb

          此命令在 PostgreSQL 容器内运行命令行工具以执行数据库转储。转储文件将保存到当前目录。pg_dump``spacedb_dump.sql

        还原 PostgreSQL 数据库

        1. 如果 Space 实例正在运行,请停止该实例:

          docker-compose -p space-on-premises down
          docker-compose -p space-on-premises rm -f
          
        2. 由于 PostrgreSQL 容器也已停止,因此应使用以下命令重新启动它:

          docker compose up postgres
          
        3. 跑:

          docker exec -i $DB_CONTAINER_ID psql -U $DB_USERNAME -d $DB_NAME < spacedb_dump.sql
          

          此命令将之前创建的转储导入到 PostgreSQL 容器中的空数据库。spacedb_dump.sql

        4. 启动 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

        1. Make sure you performed the backup of the PostgreSQL database.

        2. 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 ): localhost

          mc alias set space-minio http://localhost:9000 space-access-key space-secret-key
          
        3. Mirror 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

        1. 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).

        2. Stop your Space instance if it is running:

          docker-compose -p space-on-premises down
          docker-compose -p space-on-premises rm -f
          
        3. As the MinIO container is also stopped, you should start it again with:

        4. 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-local
          
        5. Restore the MinIO data:

          mc mirror <BACKUP/DIRECTORY> <ALIAS>
          
        6. 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
          
        最后更新于 November 21, 2023
        On this page
        暂无目录