Skip to main content

数据传输

¥Data transfer

strapi transfer 命令将数据从一个 Strapi 实例流式传输到另一个 Strapi 实例。transfer 命令使用严格的模式匹配,这意味着你的两个 Strapi 实例需要是彼此的精确副本(除了所包含的数据之外)。默认 transfer 命令传输你的内容(实体和关系)、文件(资源)、项目配置和模式。该命令允许你传输数据:

¥The strapi transfer command streams your data from one Strapi instance to another Strapi instance. The transfer command uses strict schema matching, meaning your two Strapi instances need to be exact copies of each other except for the contained data. The default transfer command transfers your content (entities and relations), files (assets), project configuration, and schemas. The command allows you to transfer data:

  • 从本地 Strapi 实例到远程 Strapi 实例

    ¥from a local Strapi instance to a remote Strapi instance

  • 从远程 Strapi 实例到本地 Strapi 实例

    ¥from a remote Strapi instance to a local Strapi instance

提醒
  • 如果你在目标实例中使用 SQLite 数据库,则在 transfer 操作运行时其他数据库连接将被阻止。

    ¥If you are using an SQLite database in the destination instance other database connections will be blocked while the transfer operation is running.

  • 管理员用户和 API 令牌不会转移。

    ¥Admin users and API tokens are not transferred.

  • 如果你的项目中使用了 websockets 或 Socket.io,传输命令将会失败。你需要暂时禁用 websockets 或 Socket.io,或者确保你的 websocket 服务器运行在与 Strapi 服务器不同的端口上,或者运行在 Strapi 内的特定路由上才能使用传输命令。

    ¥If you use websockets or Socket.io in your projects, the transfer command will fail. You will need to temporarily disable websockets or Socket.io or ensure that your websocket server is running on a different port than the Strapi server, or a on a specific route within Strapi to use the transfer command.

CLI 命令包含以下参数:

¥The CLI command consists of the following arguments:

选项描述
--to目标 Strapi 实例上 /admin 端点的完整 URL
(例如 --to https://my-beautiful-strapi-website/admin
‑‑to‑token从 Strapi 目标实例传输令牌。
--from要从中提取数据的远程 Strapi 实例的 /admin 端点的完整 URL(例如 --from https://my-beautiful-strapi-website/admin
‑‑from‑token从 Strapi 源实例传输令牌。
--force自动回答 "yes" 所有提示,包括潜在的破坏性请求,并以非交互方式运行。
--exclude使用逗号分隔的数据类型排除数据。可用的类型有:contentfilesconfig
--only仅包含这些数据。可用的类型有:contentfilesconfig
--throttle在传输期间在 "chunks" 之间注入人为延迟的时间(以毫秒为单位)。
提醒

需要 --to--from

¥Either --to or --from is required.

💡 提示
  • 数据传输由传输令牌授权,即 从管理面板管理。在管理面板中,你可以管理对令牌的基于角色的权限,包括 viewcreatereadregeneratedelete

    ¥Data transfers are authorized by transfer tokens, which are managed from the admin panel. From the admin panel, you can manage role-based permissions to tokens including view, create, read, regenerate and delete.

  • 将你的转移令牌存储到 环境变量 中以避免复制/粘贴可能会很方便。只需确保这些令牌不会被推送到公共存储库。

    ¥It might be convenient to store your transfer tokens into environment variables to avoid copying/pasting. Just ensure that these tokens are not pushed to public repositories.

⚠️ 警告

当使用 nginx 和将请求代理到本地主机的服务器时,可能会出现问题。为了防止这种情况,请通过更改 /etc/nginx/sites-available/yourdomain 中的配置文件来确保所有标头都正确转发,如下所示:

¥When using nginx and a server that proxies requests into a localhost, issues might occur. To prevent them, ensure all the headers are forwarded correctly by changing the configuration file in /etc/nginx/sites-available/yourdomain as follows:

server {
listen 80;
server_name <yourdomain>;
location / {
proxy_pass http://localhost:1337;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
include proxy_params;
}
}

生成转账令牌

¥Generate a transfer token

☑️ Prerequisites

盐转移令牌应在 管理面板配置 文件中定义。

¥A salt transfer token should be defined in the admin panel configuration file.

strapi transfer 命令需要目标实例颁发的传输令牌。要在管理面板中生成转移令牌,请使用 用户指南.txt 中的说明。

¥The strapi transfer command requires a transfer token issued by the destination instance. To generate a transfer token in the admin panel use the instructions in the User Guide.

设置并运行数据传输

¥Setup and run the data transfer

启动数据传输取决于你是要将数据推送到远程实例还是从远程拉取数据:

¥Initiating a data transfer depends on whether you want to push data to a remote instance or to pull data from the remote:

  1. 启动目标实例的 Strapi 服务器。

    ¥Start the Strapi server for the destination instance.

  2. 在新的终端窗口中,导航到源实例的根目录。

    ¥In a new terminal window, navigate to the root directory of the source instance.

  3. 运行以下最小命令来启动传输,确保 destinationURL 是管理面板的完整 URL(即 URL 包含 /admin 部分):

    ¥Run the following minimal command to initiate the transfer, ensuring destinationURL is the full URL to the admin panel (i.e., the URL includes the /admin part):

```bash
yarn strapi transfer --to destinationURL
```

  1. 当出现提示时添加转移令牌。

    ¥Add the transfer token when prompted to do so.

  2. 对 CLI 提示回答“是”或“否”:“此次传输将删除所有远程 Strapi 资源及其数据库。你确定要继续吗?”

    ¥Answer Yes or No to the CLI prompt: "The transfer will delete all of the remote Strapi assets and its database. Are you sure you want to proceed?"

绕过所有 transfer 命令行提示

¥Bypass all transfer command line prompts

使用 strapi transfer 命令时,需要确认传输将删除现有的数据库内容。--force 标志允许你绕过此提示。此选项对于以编程方式实现 strapi transfer 很有用。如果使用 --force 选项,则必须将 to-token 选项与转移令牌一起传递。

¥When using the strapi transfer command, you are required to confirm that the transfer will delete the existing database contents. The --force flag allows you to bypass this prompt. This option is useful for implementing strapi transfer programmatically. You must pass the to-token option with the transfer token if you use the --force option.

提醒

--force 选项会绕过所有有关内容删除的警告。

¥The --force option bypasses all warnings about content deletion.

示例:使用 --force 绕过 transfer 命令行提示

¥Example: bypass the transfer command line prompts with --force

yarn strapi transfer --to https://example.com/admin --to-token my-transfer-token --force

传输期间仅包含指定的数据类型

¥Include only specified data types during transfer

默认 strapi transfer 命令传输你的内容(实体和关系)、文件(资源)、项目配置和模式。--only 选项允许你通过传递逗号分隔的字符串(类型之间没有空格)来仅传输列出的项目。可用值为 contentfilesconfig。模式始终会传输,因为模式匹配用于 strapi transfer

¥The default strapi transfer command transfers your content (entities and relations), files (assets), project configuration, and schemas. The --only option allows you to transfer only the listed items by passing a comma-separated string with no spaces between the types. The available values are content, files, and config. Schemas are always transferred, as schema matching is used for strapi transfer.

示例:只传输文件

¥Example: only transfer files

yarn strapi transfer --to https://example.com/admin --only files

传输期间排除数据类型

¥Exclude data types during transfer

默认 strapi transfer 命令传输你的内容(实体和关系)、文件(资源)、项目配置和模式。--exclude 选项允许你通过以逗号分隔的字符串传递这些项目来排除内容、文件和项目配置,类型之间不带空格。你无法排除架构,因为架构匹配用于 strapi transfer

¥The default strapi transfer command transfers your content (entities and relations), files (assets), project configuration, and schemas. The --exclude option allows you to exclude content, files, and the project configuration by passing these items in a comma-separated string with no spaces between the types. You can't exclude the schemas, as schema matching is used for strapi transfer.

示例:从传输中排除文件

¥Example: exclude files from transfer

yarn strapi transfer --to https://example.com/admin --exclude files
⚠️ 警告

任何从传输中排除的类型都将在你的目标实例中删除。例如,如果你排除 config,则目标实例中的项目配置将被删除。

¥Any types excluded from the transfer will be deleted in your destination instance. For example, if you exclude config the project configuration in your destination instance will be deleted.

使用环境变量管理数据传输

¥Manage data transfer with environment variables

环境变量 STRAPI_DISABLE_REMOTE_DATA_TRANSFER 可用于禁用远程数据传输。除了管理面板中的 RBAC 权限 之外,它还可以帮助你保护 Strapi 应用的安全。要使用 STRAPI_DISABLE_REMOTE_DATA_TRANSFER,你可以将其添加到 .env 文件中或在 start 脚本前面添加。请参见以下示例:

¥The environment variable STRAPI_DISABLE_REMOTE_DATA_TRANSFER is available to disable remote data transfer. In addition to the RBAC permissions in the admin panel this can help you secure your Strapi application. To use STRAPI_DISABLE_REMOTE_DATA_TRANSFER you can add it to your .env file or preface the start script. See the following example:

STRAPI_DISABLE_REMOTE_DATA_TRANSFER=true yarn start

有关在 Strapi 中使用环境变量的更多详细信息,请参阅 环境配置文档

¥Additional details on using environment variables in Strapi are available in the Environment configurations documentation.

本地测试传输命令

¥Test the transfer command locally

transfer 命令不适用于在两个本地实例之间传输数据。exportimport 命令就是为此目的而设计的。但是,你可能希望在测试实例上本地测试 transfer,以便在将其与远程实例一起使用之前更好地了解其功能。以下文档提供了 transfer 流程的完整示例。

¥The transfer command is not intended for transferring data between two local instances. The export and import commands were designed for this purpose. However, you might want to test transfer locally on test instances to better understand the functionality before using it with a remote instance. The following documentation provides a fully-worked example of the transfer process.

创建并克隆新的 Strapi 项目

¥Create and clone a new Strapi project

  1. 使用安装命令创建一个新的 Strapi 项目:

    ¥Create a new Strapi project using the installation command:

    npx create-strapi-app@latest <project-name> --quickstart
  2. 在项目中创建至少 1 个内容类型。如果你需要有关创建第一个内容类型的说明,请参阅 快速入门指南

    ¥Create at least 1 content type in the project. See the Quick Start Guide if you need instructions on creating your first content type.

    Caution

    Do not add any data to your project at this step.

  3. 将项目提交到 git 存储库:

    ¥Commit the project to a git repository:

    git init
    git add .
    git commit -m "first commit"
  4. 克隆项目存储库:

    ¥Clone the project repository:

    cd .. # move to the parent directory
    git clone <path to created git repository>.git/ <new-instance-name>

将数据添加到第一个 Strapi 实例

¥Add data to the first Strapi instance

  1. 返回第一个 Strapi 实例并将数据添加到内容类型。

    ¥Return to the first Strapi instance and add data to the content type.

  2. 在第一个实例上停止服务器。

    ¥Stop the server on the first instance.

创建转账令牌

¥Create a transfer token

  1. 导航到第二个 Strapi 实例并在根目录中运行 buildstart 命令:

    ¥Navigate to the second Strapi instance and run the build and start commands in the root directory:

yarn build && yarn start
  1. 注册一个管理员用户。

    ¥Register an admin user.

  2. 创建并复制转移令牌

    ¥Create and copy a transfer token.

  3. 让服务器保持运行。

    ¥Leave the server running.

传输你的数据

¥Transfer your data

  1. 返回第一个 Strapi 实例。

    ¥Return the the first Strapi instance.

  2. 在终端中运行 strapi transfer 命令:

    ¥In the terminal run the strapi transfer command:

yarn strapi transfer --to http://localhost:1337/admin
  1. 出现提示时,应用转移令牌。

    ¥When prompted, apply the transfer token.

  2. 传输完成后,你可以返回到第二个 Strapi 实例并看到内容已成功传输。

    ¥When the transfer is complete you can return to the second Strapi instance and see that the content is successfully transferred.

💡 提示

在某些情况下,你可能会收到针对 localhost 的连接被拒绝错误。尝试将地址更改为 http://127.0.0.1:1337/admin

¥In some cases you might receive a connection refused error targeting localhost. Try changing the address to http://127.0.0.1:1337/admin.