# syncコマンド

{% hint style="success" %}
同期は一方向のみで、Keeperを信頼できる情報源 (読み取り専用) として使用し、外部のシークレットマネージャー内のリモートのキー・バリューペアのみを更新します。
{% endhint %}

## `sync`コマンド

**説明:** Keeperボルトからシークレットをインポートし、外部のシークレット管理システムと同期します。

`ksm sync --credentials <UID> --type [aws|azure|gcp|json] [--dry-run] [--preserve-missing] --map <KEY NOTATION>...`

{% hint style="info" %}
`ksm profile init <TOKEN>` で初期化したシークレットマネージャープロファイルが必要です。\
\
詳細については、[profileコマンドのページ](/keeperpam/jp/secrets-manager/secrets-manager-command-line-interface/profile-command.md)をご参照ください。
{% endhint %}

{% hint style="info" %}
**AWS同期の前提条件:** AWSへの同期 (`--type aws`) には `[aws]` エクストラが必要です。

`pip3 install keeper-secrets-manager-cli[aws]`

`--type json` オプションでは追加の依存関係は不要です。
{% endhint %}

パラメータ:

* `-t`, `--type` ターゲットのキー/値ストレージのタイプ。使用できるタイプは以下のとおりです。
  * `aws` — AWS Secrets Manager
  * `azure` — Azure Key Vault
  * `gcp` — GCP Secret Manager
  * `json` — 送信元と送信先の両方の値を含む、保留中の同期操作をすべて一覧表示します。
* `-m`, `--map <KEY NOTATION>` [表記法](/keeperpam/jp/secrets-manager/about/keeper-notation.md)のURIを使用して、送信先のキー名を値にマッピングします。
* `-c`, `--credentials <uid>` 送信先のキー/値ストレージにアクセスするための認証情報を格納したKeeperレコードのUID。指定したレコードは、Keeperシークレットマネージャーアプリケーションに共有されている必要があります。

オプションパラメータ:

* `-n`, `--dry-run` 変更を加えずにテストで実行します。
* `-p`, `--preserve-missing` 送信元の値が削除されても、送信先の値を保持します。

#### AWS固有のオプション <a href="#aws-specific-options" id="aws-specific-options"></a>

* `-r`, `--record <RECORD>` タイトルまたはUIDを指定して、個別のレコードを同期します。複数回指定できます。
* `-f`, `--folder <FOLDER>` 指定したフォルダ内のすべてのレコードを同期します (再帰処理は行いません)。
* `-fr`, `--folder-recursive <FOLDER>` 指定したフォルダおよびその配下のすべてのサブフォルダを再帰的に同期します。
* `-rj`, `--raw-json` 完全な生のJSONをKMSシークレットに保存します (`secret get <UID> --json` と同じ形式)。

詳しい例については、[AWS Secrets Managerとの同期](/keeperpam/jp/secrets-manager/integrations/aws-secrets-manager.md)をご参照ください。

### crontabを使用した自動化 <a href="#automation-with-crontab" id="automation-with-crontab"></a>

cronジョブを使用することで、シークレットの同期を自動化できます。

#### 例1: 毎日のシンプルな同期 <a href="#example-1-simple-daily-sync-one-liner" id="example-1-simple-daily-sync-one-liner"></a>

毎日午前2時に1回同期します。

```bash
# crontabを編集
crontab -e

# Productionフォルダを毎日2:00に同期する行を追加
0 2 * * * /usr/local/bin/ksm sync -t aws -c <CRED_UID> --folder-recursive "Production" >> /var/log/ksm-sync.log 2>&1
```

#### 例2: 複数同期を行う高度なスクリプト <a href="#example-2-complex-multi-sync-script" id="example-2-complex-multi-sync-script"></a>

異なるマッピングを使用して複数の同期処理を行う場合は、シェルスクリプトを作成します。

**`/home/user/scripts/ksm-sync-all.sh` を作成します。**

```bash
#!/usr/bin/env bash

# 設定
CRED_UID="<YOUR_AWS_CRED_UID>"

# 複数のカスタムマッピングを使用した同期
ksm sync -t aws -c "$CRED_UID" \
    --map "legacy-db-password" "keeper://<UID>/field/password" \
    --map "api_key" "keeper://<UID>/custom_field/api_key" \
    --map "db_host" "keeper://<UID>/custom_field/hostname" \
    >> /var/log/ksm-sync.log 2>&1

# マッピング数が多い場合は、複数の同期コマンドに分割します
ksm sync -t aws -c "$CRED_UID" \
    --map "db_password" "keeper://<UID>/field/password" \
    --map "db_login" "keeper://<UID>/custom_field/login" \
    >> /var/log/ksm-sync.log 2>&1
```

**スクリプトに実行権限を付与します。**

```bash
chmod +x /home/user/scripts/ksm-sync-all.sh
```

**6時間ごとに実行するようcrontabに追加します。**

```bash
# crontabを編集
crontab -e

# 6時間ごとに同期スクリプトを実行
0 */6 * * * /home/user/scripts/ksm-sync-all.sh
```

## 同期タイプ

外部プロバイダーを選び、統合の詳細は各リンク先のページをご参照ください。

{% content-ref url="/pages/34KiaAqCCWYeFK8n8tHW" %}
[AWSシークレットマネージャー同期](/keeperpam/jp/secrets-manager/integrations/aws-secrets-manager.md)
{% endcontent-ref %}

{% content-ref url="/pages/8LqKdN1gExiCxJhy1YW3" %}
[Azure Key Vaultとの同期](/keeperpam/jp/secrets-manager/integrations/azure-key-vault.md)
{% endcontent-ref %}

{% content-ref url="/pages/bGhfkhAMwUdjtvng2lhQ" %}
[Google Cloud Secret Managerとの同期](/keeperpam/jp/secrets-manager/integrations/gcp-secret-manager.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/secrets-manager/secrets-manager-command-line-interface/sync-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
