数据传输
🌐 Data transfer
strapi transfer 命令是 数据管理功能 的一部分,用于将数据从一个 Strapi 实例流式传输到另一个 Strapi 实例。transfer 命令使用严格的模式匹配,这意味着你的两个 Strapi 实例需要完全相同,唯一的区别是包含的数据。默认的 transfer 命令会传输你的内容(实体和关系)、文件(资源)、项目配置以及模式。该命令允许你传输数据:
🌐 The strapi transfer command is part of the Data Management feature and 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 实例
- 从远程 Strapi 实例到本地 Strapi 实例
以下文档详细说明了可用于自定义数据传输的选项。传输命令及所有可用选项均通过 Strapi CLI 运行。
🌐 The following documentation details the available options to customize your data transfer. The transfer command and all of the available options are run using the Strapi CLI.
- 如果你在目标实例中使用 SQLite 数据库,当
transfer操作正在运行时,其他数据库连接将被阻塞。 - 管理员用户和 API 令牌不会转移。
- 如果你在项目中使用 websockets 或 Socket.io,transfer 命令将会失败。你需要暂时禁用 websockets 或 Socket.io,或者确保你的 websocket 服务器运行在与 Strapi 服务器不同的端口,或者在 Strapi 内的特定路由上运行,以使用 transfer 命令。
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 | 自动对所有提示回答“是”,包括可能具有破坏性的请求,并以非交互方式运行。 || --exclude | 使用逗号分隔的数据类型排除数据。可用的类型有:content、files 和 config。 || --only | 仅包含这些数据。可用的类型有:content、files 和 config。 || --throttle | 在传输期间在“块”之间注入人工延迟的时间(毫秒)。 || --verbose | 启用详细日志。 |
必须提供 --to 或 --from。
🌐 Either --to or --from is required.
当使用 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
盐转移令牌应在管理员面板配置文件中定义。
🌐 A salt transfer token should be defined in the admin panel configuration file.
strapi transfer 命令需要目标实例发放的传输令牌。要在管理面板中生成传输令牌,请按照 用户指南 中的说明操作。
🌐 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:
- Push data to remote
- Pull data from remote
- 启动目标实例的 Strapi 服务器。
- 在新的终端窗口中,导航到源实例的根目录。
- 运行以下最小命令以启动传输,确保
destinationURL是管理面板的完整 URL(即 URL 包含/admin部分):
- yarn
- npm
yarn strapi transfer --to destinationURL
npm run strapi transfer -- --to destinationURL
- 当出现提示时添加转移令牌。
- 在 CLI 提示中回答 是 或 否:"该传输将删除所有远程 Strapi 资源及其数据库。你确定要继续吗?"
- 启动源实例的 Strapi 服务器。
- 在新的终端窗口中,导航到目标实例的根目录。
- 运行以下最小命令以启动传输,确保
remoteURL是管理面板的完整 URL(即 URL 包含/admin部分):
- yarn
- npm
yarn strapi transfer --from remoteURL
npm run strapi transfer -- --from remoteURL
- 当出现提示时添加转移令牌。
- 在 CLI 提示中回答 是 或 否:"该传输将删除所有本地 Strapi 资源及其数据库。你确定要继续吗?"
绕过所有 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
- npm
yarn strapi transfer --to https://example.com/admin --to-token my-transfer-token --force
npm run strapi transfer -- --to https://example.com/admin --to-token my-transfer-token --force
在传输过程中仅包含指定的数据类型
🌐 Include only specified data types during transfer
默认的 strapi transfer 命令会传输你的内容(实体和关系)、文件(资源)、项目配置和模式。--only 选项允许你仅通过传入用逗号分隔且类型之间无空格的字符串来传输列出的项目。可用的值为 content、files 和 config。模式始终会被传输,因为 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
- npm
yarn strapi transfer --to https://example.com/admin --only files
npm run 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
- npm
yarn strapi transfer --to https://example.com/admin --exclude files
npm run 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 命令并不用于在两个本地实例之间传输数据。export 和 import 命令是为此目的设计的。不过,你可能希望在测试实例上本地测试 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
-
使用安装命令创建一个新的 Strapi 项目:
npx create-strapi-app@latest <project-name> --quickstart -
在项目中至少创建1种内容类型。如果你需要关于创建第一个内容类型的说明,请参阅快速入门指南。
Caution在此步骤中不要向你的项目添加任何数据。
-
将项目提交到 git 存储库:
git init
git add .
git commit -m "first commit" -
克隆项目仓库:
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
- 返回第一个 Strapi 实例并将数据添加到内容类型。
- 在第一个实例上停止服务器。
创建转账令牌
🌐 Create a transfer token
- 导航到第二个 Strapi 实例,并在根目录运行
build和start命令:
- yarn
- npm
yarn build && yarn start
npm run build && npm run start
- 注册一个管理员用户。
- 创建并复制传输令牌。
- 让服务器保持运行。
传输你的数据
🌐 Transfer your data
- 返回第一个 Strapi 实例。
- 在终端中运行
strapi transfer命令:
- yarn
- npm
yarn strapi transfer --to http://localhost:1337/admin
npm run strapi transfer -- --to http://localhost:1337/admin
- 出现提示时,应用转移令牌。
- 传输完成后,你可以返回到第二个 Strapi 实例并看到内容已成功传输。
在某些情况下,你可能会收到针对 localhost 的连接被拒绝错误。尝试将地址更改为 http://127.0.0.1:1337/admin。