数据导出
🌐 Data export
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.
- 不会导出管理员用户和 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:
- Yarn
- NPM
yarn strapi export --no-encrypt --no-compress -f my-export
npm run 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.
大型数据集会自动拆分到多个 .jsonl 文件中。maxSizeJsonl 提供者选项 控制每个文件的最大大小。
🌐 Large datasets are automatically split across multiple .jsonl files. The maxSizeJsonl provider option controls the maximum size of each file.
命名导出文件
🌐 Name the export file
导出的数据包含在一个 .tar 文件中,该文件会自动使用 export_YYYYMMDDHHMMSS 格式命名。你可以通过在 strapi export 命令中使用 --file 或 -f 选项来选择性地为导出的文件命名。不要包含文件扩展名,因为会根据提供的选项自动设置。
🌐 Exported data are contained in a .tar file that is automatically named using the format export_YYYYMMDDHHMMSS. You can optionally name the exported file by passing the --file or -f option with the strapi export command. Do not include a file extension as one will be set automatically depending on options provided.
示例:使用自定义文件名导出数据
🌐 Example: Export data with a custom filename
- yarn
- npm
yarn strapi export --file my-strapi-export
npm run 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.
为了保护项目中的敏感数据,建议使用强加密密钥。 OpenSSL 是用于生成加密密钥的资源。以下示例命令是在终端中生成加密密钥:
- Mac/Linux
- Windows
openssl rand -base64 48
node -p "require('crypto').randomBytes(48).toString('base64');"
要禁用加密,请在 strapi export 命令中传递 --no-encrypt 选项。
🌐 To disable encryption, pass the --no-encrypt option with the strapi export command.
示例:导出数据不加密
🌐 Example: Export data without encryption
- yarn
- npm
yarn strapi export --no-encrypt
npm run strapi export -- --no-encrypt
示例:使用加密 --key 选项导出数据
🌐 Example: Export data with the encryption --key option
- yarn
- npm
yarn strapi export --key my-encryption-key
npm run 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
- yarn
- npm
yarn strapi export --no-compress
npm run strapi export -- --no-compress
仅导出所选类型的数据
🌐 Export only selected types of data
默认的 strapi export 命令会导出你的内容(实体和关系)、文件(资源)、项目配置和模式。--only 选项允许你仅导出列出的项目,通过传递一个逗号分隔且类型之间无空格的字符串。可用的值为 content、files 和 config。模式始终会被导出,因为模式匹配用于 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.
诸如图片之类的 媒体由文件(资源)和数据库中的实体组成。如果你使用 --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
- yarn
- npm
yarn strapi export --only content
npm run 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.
像图片这样的媒体由文件(资源)和数据库中的实体组成。如果使用 --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.