Skip to main content

数据导出

🌐 Data export

Page summary:

strapi export 命令将数据从 Strapi 实例导出为一个加密和压缩的归档文件,其中包含实体、关系、资源、模式和配置。可以使用 --no-encrypt--no-compress--only--exclude 等选项自定义导出。

🌐 The strapi export command exports data from a Strapi instance as an encrypted and compressed archive containing entities, relations, assets, schemas, and configuration. Customize exports using options like --no-encrypt, --no-compress, --only, and --exclude.

4.6.0This feature requires Strapi version 4.6.0 or later.

strapi export 命令是 数据管理功能 的一部分,用于从本地 Strapi 实例导出数据。默认情况下,strapi export 命令会将数据导出为加密和压缩的 tar.gz.enc 文件,其中包括:

🌐 The strapi export command is part of the Data Management feature and used to export data from a local Strapi instance. By default, the strapi export command exports data as an encrypted and compressed tar.gz.enc file which includes:

  • 项目配置,
  • 实体:你所有的内容,
  • 链接:你实体之间的关系,
  • 资源:存储在上传文件夹中的文件,
  • schemas,
  • metadata.json 文件。

以下文档详细说明了自定义数据导出的可用选项。导出命令及所有可用选项都是通过 Strapi CLI 运行的。

🌐 The following documentation details the available options to customize your data export. The export command and all of the available options are run using the Strapi CLI.

Caution
  • 不会导出管理员用户和 API 令牌。

了解导出的档案

🌐 Understand the exported archive

导出的 .tar 存档包含一个扁平结构的编号 JSON lines 文件,这些文件按每个导出资源的文件夹排列:

export_202401011230.tar
├── metadata.json
├── configuration/
│ └── configuration_00001.jsonl
├── entities/
│ └── entities_00001.jsonl
├── links/
│ └── links_00001.jsonl
└── schemas/
└── schemas_00001.jsonl

每个文件夹(metadata.json 除外)包含一个或多个按顺序命名的 .jsonl 文件(例如,entities_00001.jsonl)。这些 .jsonl 文件中的每一行都表示一条记录,使得该归档便于逐行处理或转换为如 CSV 之类的格式。要直接检查这些文件,请导出时不要使用压缩或加密:

🌐 Each folder (except for metadata.json) contains one or more .jsonl files named sequentially (e.g., entities_00001.jsonl). Each line in these .jsonl files represents a single record, making the archive convenient for line‑by‑line processing or conversion to formats such as CSV. To inspect these files directly, export without compression or encryption:

terminal
yarn strapi export --no-encrypt --no-compress -f my-export

上面的命令会在项目根目录中创建 my-export.tar。解压归档文件(tar -xf my-export.tar)后,打开任意 .jsonl 文件以查看单个记录。

🌐 The command above creates my-export.tar in the project root. After extracting the archive (tar -xf my-export.tar), open any .jsonl file to view individual records.

Note

大型数据集会自动拆分到多个 .jsonl 文件中。maxSizeJsonl 提供者选项控制每个文件的最大大小。

🌐 Large datasets are automatically split across multiple .jsonl files. The maxSizeJsonl provider option controls the maximum size of each file.

导出到目录

🌐 Export to a directory

5.42.0This feature requires Strapi version 5.42.0 or later.

默认情况下,strapi export 会生成一个 .tar 存档。传递 --format dir 可以将导出写入为非压缩目录。该目录采用与存档相同的布局(metadata.jsonschemas/entities/links/configuration/assets/),使导出的数据无需先解压存档即可读取和比较差异。

🌐 By default, strapi export produces a .tar archive. Pass --format dir to write the export as an unpacked directory instead. The directory uses the same layout as the archive (metadata.json, schemas/, entities/, links/, configuration/, assets/), making the exported data readable and diffable without extracting an archive first.

目录导出不支持加密或压缩。你必须明确传递 --no-encrypt

🌐 Directory exports do not support encryption or compression. You must pass --no-encrypt explicitly:

terminal
yarn strapi export --format dir --no-encrypt -f my-export

该命令在项目根目录中创建一个包含所有导出数据的 my-export 目录。

🌐 The command creates a my-export directory in the project root containing all exported data.

Tip

当你想将导出的数据提交到版本控制时,目录格式非常有用。JSON 和 JSONL 文件表现为常规的文本差异,使得在拉取请求中能够看到更改。

🌐 The directory format is useful when you want to commit exported data to version control. JSON and JSONL files appear as regular text diffs, making changes visible in pull requests.

命名导出文件

🌐 Name the export file

导出的数据包含在一个 .tar 文件中(或使用 --format dir 时为一个目录),该文件会自动使用 export_YYYYMMDDHHMMSS 格式命名。你可以通过使用 strapi export 命令的 --file-f 选项来选择性地命名导出的文件或目录。对于归档导出,请不要包含文件扩展名,因为根据提供的选项会自动设置扩展名。对于目录导出,则使用 -f 的值作为输出目录路径。

🌐 Exported data are contained in a .tar file (or a directory when using --format dir) that is automatically named using the format export_YYYYMMDDHHMMSS. You can optionally name the exported file or directory by passing the --file or -f option with the strapi export command. Do not include a file extension for archive exports, as one is set automatically depending on options provided. For directory exports, the value of -f is used as the output directory path.

示例:使用自定义文件名导出数据

🌐 Example: Export data with a custom filename

terminal
yarn strapi export --file my-strapi-export

配置数据加密

🌐 Configure data encryption

默认的 strapi export 命令使用 aes-128-ecb 加密来加密你的项目数据,并添加文件扩展名 .enc。要使用加密功能,你需要通过 -k--key 选项传入加密密钥,或者在提示时输入加密密钥。加密密钥是一个 string,没有最少字符数要求。

🌐 The default strapi export command encrypts your project data using aes-128-ecb encryption and adds the file extension .enc. To use encryption you need to pass an encryption key using the -k or --key option or enter an encryption key when prompted. The encryption key is a string with no minimum character count.

Encryption keys

为了保护项目中的敏感数据,建议使用强加密密钥。 OpenSSL 是用于生成加密密钥的资源。以下示例命令是在终端中生成加密密钥:

terminal
openssl rand -base64 48

要禁用加密,请在 strapi export 命令中传递 --no-encrypt 选项。

🌐 To disable encryption, pass the --no-encrypt option with the strapi export command.

示例:导出数据不加密

🌐 Example: Export data without encryption

terminal
yarn strapi export --no-encrypt

示例:使用加密 --key 选项导出数据

🌐 Example: Export data with the encryption --key option

terminal
yarn strapi export --key my-encryption-key

禁用数据压缩

🌐 Disable data compression

默认的 strapi export 命令使用 gzip 压缩压缩你的项目数据,并添加 .gz 文件扩展名。

🌐 The default strapi export command compresses your project data using gzip compression and adds the .gz file extension.

要禁用压缩,请使用 strapi export 命令传递 --no-compress 选项。

🌐 To disable compression, pass the --no-compress option with the strapi export command.

示例:导出数据不压缩

🌐 Example: Export data without compression

terminal
yarn strapi export --no-compress

仅导出所选类型的数据

🌐 Export only selected types of data

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

🌐 The default strapi export command exports your content (entities and relations), files (assets), project configuration, and schemas. The --only option allows you to export 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 exported, as schema matching is used for strapi import.

Note

诸如图片之类的媒体由文件(资源)和数据库中的实体组成。如果你使用 --only 标志导出 content,资源数据库记录仍然会被包括在内,并可能显示为损坏的链接。

🌐 Media such as images consist of the file (asset) and the entity in the database. If you use the --only flag to export content, the asset database records are still included, and could render as broken links.

示例:仅导出实体和关系

🌐 Example: Export only entities and relations


terminal
yarn strapi export --only content

从导出中排除项目

🌐 Exclude items from export

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

🌐 The default strapi export command exports 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 import.

Note

像图片这样的媒体由文件(资源)和数据库中的实体组成。如果使用 --exclude 标志来移除资源,数据库记录仍然会保留,并可能显示为断开的链接。

🌐 Media such as images consist of the file (asset) and the entity in the database. If you use the --exclude flag to remove assets, the database records are still included, and could render as broken links.

示例:导出数据,排除资源、实体和关系

🌐 Example: Export data excluding assets, entities, and relations

terminal
yarn strapi export --exclude files,content