数据导入
¥Data import
4.6.0This feature requires Strapi version 4.6.0 or later.strapi import 命令是 数据管理功能 的一部分,用于从文件导入数据。默认情况下,strapi import 命令从加密和压缩的 tar.gz.enc 文件导入数据,其中包括:
¥The strapi import command is part of the Data Management feature and used to import data from a file. By default, the strapi import command imports data from an encrypted and compressed tar.gz.enc file which includes:
-
项目配置,
¥the project configuration,
-
实体:你的所有内容,
¥entities: all of your content,
-
链接:你的实体之间的关系,
¥links: relations between your entities,
-
资源:文件存储在上传文件夹中,
¥assets: files stored in the uploads folder,
-
模式,
¥schemas,
-
metadata.json文件。¥the
metadata.jsonfile.
该存档遵循与 strapi export 生成的存档相同的结构:.tar 包含 configuration、entities、links 和 schemas 文件夹,其中包含按编号排列的 JSON 行 文件。压缩 (.gz) 和加密 (.enc) 会从文件扩展名中检测出来,因此也可以导入纯文本 .tar。
¥The archive follows the same structure as the one produced by strapi export: a .tar containing configuration, entities, links, and schemas folders filled with numbered JSON lines files. Compression (.gz) and encryption (.enc) are detected from the file extension, so a plain .tar can be imported as well.
以下文档详细介绍了自定义数据导入的可用选项。导入命令和所有可用选项均使用 Strapi CLI 运行。
¥The following documentation details the available options to customize your data import. The import command and all of the available options are run using the Strapi CLI.
-
strapi import在导入备份文件之前删除所有现有数据,包括数据库和上传目录。¥
strapi importdeletes all existing data, including the database and uploads directory, before importing the backup file. -
源架构和目标架构必须匹配才能成功使用
strapi import,这意味着所有内容类型必须相同。¥The source and target schemas must match to successfully use
strapi import, meaning all content types must be identical. -
恢复的数据不包括
Admin users表,这意味着恢复的实例中createdBy和updatedBy为空。¥Restored data does not include the
Admin userstable, which means thatcreatedByandupdatedByare empty in a restored instance.
了解导入存档
¥Understand the import archive
strapi import 期望存档具有与 strapi export 命令 生成的相同扁平结构:
¥strapi import expects an archive with the same flat structure produced by the strapi export command:
-
configuration/:项目配置文件¥
configuration/: project configuration files -
entities/:实体记录¥
entities/: entity records -
links/:实体间关系¥
links/: relations between entities -
schemas/:模式定义¥
schemas/: schema definitions -
metadata.json:导出元数据¥
metadata.json: metadata about the export
每个文件夹包含一个或多个 .jsonl 文件,其中每一行代表一条记录。该格式允许你在重新导入数据之前编辑或转换数据。
¥Each folder contains one or more .jsonl files where each line represents a single record. The format allows you to edit or transform data before re‑importing it.
要准备存档以供手动审核或修改:
¥To prepare an archive for manual review or modification:
- Yarn
- NPM
yarn strapi export --no-encrypt --no-compress -f my-export
tar -xf my-export.tar
npm run strapi export -- --no-encrypt --no-compress -f my-export
tar -xf my-export.tar
调整 .jsonl 文件后,重新创建存档 (tar -cf my-export.tar configuration entities links schemas metadata.json) 并使用 strapi import -f my-export.tar 导入。根据文件扩展名自动检测加密和压缩。
¥After adjusting the .jsonl files, re‑create the archive (tar -cf my-export.tar configuration entities links schemas metadata.json) and import it with strapi import -f my-export.tar. Encryption and compression are detected automatically based on the file extension.
指定导入文件
¥Specify the import file
要将数据导入 Strapi 实例,请在目标项目根目录中使用 strapi import 命令。使用 -f 或 --file 选项指定要导入的文件。文件名、扩展名和路径是必需的。如果文件已加密,系统会在导入开始之前提示你输入加密密钥。
¥To import data into a Strapi instance use the strapi import command in the destination project root directory. Specify the file to be imported using the -f or --file option. The filename, extension, and path are required. If the file is encrypted, you are prompted for the encryption key before the import starts.
示例:从 Strapi 项目根目录中的文件导入数据的最少命令
¥Example: Minimum command to import data from a file in the Strapi project root
- yarn
- npm
yarn strapi import -f /path/to/my/file/export_20221213105643.tar.gz.enc
npm run strapi import -- -f /path/to/my/file/export_20221213105643.tar.gz.enc
示例:导入纯 .tar 存档
¥Example: Import a plain .tar archive
- yarn
- npm
yarn strapi import -f /path/to/my/file/backup.tar
npm run strapi import -- -f /path/to/my/file/backup.tar
提供加密密钥
¥Provide an encryption key
如果要从加密文件导入数据,则可以使用 strapi import 命令通过 -k 或 --key 选项传递加密密钥。