LoginSignup
1
2

More than 3 years have passed since last update.

Contentfulでexport / import(バックアップ/リストア[復元])する

Last updated at Posted at 2021-01-16

Contentfulでexport/importする機会があったので、メモ。

前提条件

  • Contentfulのアカウントがあること(※ない場合は、「GitHubでログイン」で作りましょう)
  • node.jsがインストール済

手順

  1. トークン作成
  2. 設定ファイル作成
  3. Contentfulへコマンドログイン
  4. export
  5. import

1. トークン作成 (Personal Access Tokens)

Contentfulにブラウザログイン

GithubあたりでSSOして、ログインしてください。

スクリーンショット 2021-01-16 15.18.43.png

Settings - Api Keys

スクリーンショット 2021-02-06 16.44.09.jpg

Content management tokens

スクリーンショット 2021-02-06 16.45.26.jpg

Generate personal token

スクリーンショット 2021-02-06 16.47.04.jpg

適当な名前を入れて作成します

ここでは、「api keys」と入れて作成しました。
スクリーンショット 2021-01-16 15.22.05.jpg
この値をクリップボードに貼り付けてメモしておきます。

2.設定ファイル作成

ContentfulのSpace IDを取得

Settings - General settings で確認できます

スクリーンショット 2021-01-16 15.28.56.jpg

スクリーンショット 2021-01-16 15.30.10.jpg

設定ファイル作成

$ cat <<EOF > example-config.json
{
  "spaceId": "<< 確認したスペースIDを記載 >>",
  "managementToken": "<< 1.で取得したトークンを記載 >>",
  "contentFile": "contentful-export.json"
}
EOF

ここまでで確認した、スペースIDとトークンを作成したファイルに記載します。

3. Contentfulへコマンドログイン

$ npx contentful-cli login --management-token "1.で取得したトークンを記載"


Great! Your CMA token is now stored on your system. (Located at /Users/hirosue/.contentfulrc.json)
You can always run contentful logout to remove it.

4.export

$ npx contentful-cli space export --config example-config.json

  ✔ Initialize client (1s)
  ✔ Fetching data from space
    ✔ Connecting to space (1s)
    ✔ Fetching content types data (1s)
    ✔ Fetching tags data (1s)
    ✔ Fetching editor interfaces data (1s)
    ✔ Fetching content entries data (1s)
    ✔ Fetching assets data (1s)
    ✔ Fetching locales data (1s)
    ✔ Fetching webhooks data (1s)
    ✔ Fetching roles data (1s)
  ↓ Download assets [skipped]
  ✔ Write export log file
    ✔ Lookup directory to store the logs
    ✔ Create log directory
    ✔ Writing data to file
┌───────────────────────┐
│ Exported entities     │
├───────────────────┬───┤
│ Content Types     │ 2 │
├───────────────────┼───┤
│ Tags              │ 0 │
├───────────────────┼───┤
│ Editor Interfaces │ 2 │
├───────────────────┼───┤
│ Entries           │ 4 │
├───────────────────┼───┤
│ Assets            │ 4 │
├───────────────────┼───┤
│ Locales           │ 1 │
├───────────────────┼───┤
│ Webhooks          │ 1 │
├───────────────────┼───┤
│ Roles             │ 1 │
└───────────────────┴───┘
The export took a few seconds (6s)

Stored space data to json file at: /path/to/workspace/tmp/contentful-export.json
No errors or warnings occurred
The export was successful.
✨ Done

5. import

$  npx contentful-cli space import --config example-config.json

┌──────────────────────────────────────────────────┐
│ The following entities are going to be imported: │
├─────────────────────────────────┬────────────────┤
│ Content Types                   │ 2              │
├─────────────────────────────────┼────────────────┤
│ Tags                            │ 0              │
├─────────────────────────────────┼────────────────┤
│ Editor Interfaces               │ 2              │
├─────────────────────────────────┼────────────────┤
│ Entries                         │ 4              │
├─────────────────────────────────┼────────────────┤
│ Assets                          │ 4              │
├─────────────────────────────────┼────────────────┤
│ Locales                         │ 1              │
├─────────────────────────────────┼────────────────┤
│ Webhooks                        │ 1              │
└─────────────────────────────────┴────────────────┘
  ✔ Validating content-file
  ✔ Initialize client (1s)
  ✔ Checking if destination space already has any content and retrieving it (2s)
  ✔ Apply transformations to source data (1s)
  ✔ Push content to destination space
    ✔ Connecting to space (1s)
    ✔ Importing Locales (1s)
    ✔ Importing Content Types (4s)
    ✔ Publishing Content Types (2s)
    ↓ Importing Tags [skipped]
    ✔ Importing Editor Interfaces (2s)
    ✔ Importing Assets (26s)
    ✔ Publishing Assets (3s)
    ✔ Archiving Assets (1s)
    ✔ Importing Content Entries (4s)
    ✔ Publishing Content Entries (3s)
    ✔ Archiving Entries (0s)
    ✔ Creating Web Hooks (1s)
Finished importing all data
┌───────────────────────┐
│ Imported entities     │
├───────────────────┬───┤
│ Locales           │ 1 │
├───────────────────┼───┤
│ Content Types     │ 2 │
├───────────────────┼───┤
│ Editor Interfaces │ 2 │
├───────────────────┼───┤
│ Assets            │ 4 │
├───────────────────┼───┤
│ Published Assets  │ 4 │
├───────────────────┼───┤
│ Archived Assets   │ 0 │
├───────────────────┼───┤
│ Entries           │ 4 │
├───────────────────┼───┤
│ Published Entries │ 4 │
├───────────────────┼───┤
│ Archived Entries  │ 0 │
├───────────────────┼───┤
│ Webhooks          │ 1 │
└───────────────────┴───┘
The import took a few seconds (42s)


The following 0 errors and 1 warnings occurred:

15:50:99 - Rate limit error occurred. Waiting for 1561 ms before retrying...
The import was successful.

後処理(ログアウト)

$ npx contentful-cli logout

This will log you out by deleting the CMA token stored on your system.
? Do you want to log out now? Yes
Successfully logged you out.

その他

無料アカウントだとRate limitにたびたび引っかかります。

参考リンク

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2