数据传输
¥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 实例
¥from a local Strapi instance to a remote Strapi instance
-
从远程 Strapi 实例到本地 Strapi 实例
¥from a remote Strapi instance to a local Strapi instance
以下文档详细介绍了自定义数据传输的可用选项。传输命令和所有可用选项均使用 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操作运行时其他数据库连接将被阻止。¥If you are using an SQLite database in the destination instance other database connections will be blocked while the
transferoperation 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 | 使用逗号分隔的数据类型排除数据。可用的类型有:content、files 和 config。 |
--only | 仅包含这些数据。可用的类型有:content、files 和 config。 |
--throttle | 在传输期间在 "chunks" 之间注入人为延迟的时间(以毫秒为单位)。 |
--verbose | 启用详细日志。 |
需要 --to 或 --from。
¥Either --to or --from is required.
-
数据传输由传输令牌授权,即 从管理面板管理。在管理面板中,你可以管理对令牌的基于角色的权限,包括
view、create、read、regenerate和delete。¥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,regenerateanddelete. -
将你的转移令牌存储到 环境变量 中以避免复制/粘贴可能会很方便。只需确保这些令牌不会被推送到公共存储库。
¥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
盐转移令牌应在 管理面板配置 文件中定义。
¥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:
- Push data to remote
- Pull data from remote
-
启动目标实例的 Strapi 服务器。
¥Start the Strapi server for the destination instance.
-
在新的终端窗口中,导航到源实例的根目录。
¥In a new terminal window, navigate to the root directory of the source instance.
-
运行以下最小命令来启动传输,确保
destinationURL是管理面板的完整 URL(即 URL 包含/admin部分):¥Run the following minimal command to initiate the transfer, ensuring
destinationURLis the full URL to the admin panel (i.e., the URL includes the/adminpart):
- yarn
- npm
```bash
yarn strapi transfer --to destinationURL
```
```bash
npm run strapi transfer -- --to destinationURL
```