> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# snapshot の管理

> このセクションでは、Python SDK を使ってアプリケーションおよびデータベースの snapshot を作成、取得、復元する方法を学びます。

[Client]: client

[Application]: client#application

snapshot は、特定の時点でのアプリケーションまたはデータベースのバックアップです。必要に応じて、アプリケーションやデータベースを以前の状態に復元できます。Square Cloud は自動の日次バックアップとともに、無制限の snapshot を提供します。

## アプリケーションの snapshot を作成する

`client.snapshot` は `Snapshot` オブジェクトを返します。

<Tabs>
  <Tab title="Using Client">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API_KEY')

    async def example():
        snapshot = await client.snapshot(app_id='application_id')
        
        print(snapshot.url)  # Download URL for the snapshot
        print(snapshot.key)  # Snapshot key identifier
    ```
  </Tab>

  <Tab title="Using Application">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API_KEY')

    async def example():
        app = await client.app('application_id')
        snapshot = await app.snapshot()
        
        print(snapshot.url)  # Download URL for the snapshot
        print(snapshot.key)  # Snapshot key identifier
    ```
  </Tab>
</Tabs>

## snapshot をダウンロードする

`Snapshot` オブジェクトには、snapshot を zip ファイルとしてダウンロードするメソッドが用意されています。

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    snapshot = await client.snapshot(app_id='application_id')
    
    # Download snapshot to current directory
    zip_file = await snapshot.download()
    
    # Download snapshot to specific directory
    zip_file = await snapshot.download(path='./backups')
```

## アプリケーションの snapshot 一覧を取得する

`client.all_app_snapshots` は `SnapshotInfo` オブジェクトのリストを返します。

<Tabs>
  <Tab title="Using Client">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API_KEY')

    async def example():
        snapshots = await client.all_app_snapshots(app_id='application_id')
        
        for snapshot in snapshots:
            print(f"Snapshot ID: {snapshot.id}")
            print(f"  Size: {snapshot.size}")
            print(f"  Created at: {snapshot.created_at}")
    ```
  </Tab>

  <Tab title="Using Application">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API_KEY')

    async def example():
        app = await client.app('application_id')
        snapshots = await app.all_snapshots()
        
        for snapshot in snapshots:
            print(f"Snapshot ID: {snapshot.id}")
            print(f"  Size: {snapshot.size}")
            print(f"  Created at: {snapshot.created_at}")
    ```
  </Tab>
</Tabs>

## snapshot を復元する

`client.restore_snapshot` は `Response` オブジェクトを返します。アプリケーションとデータベースの両方について snapshot を復元できます。

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    # Restore an application snapshot
    response = await client.restore_snapshot(
        application_type='app',
        app_id='application_id',
        snapshot_id='snapshot_id',
        version_id='version_id'
    )
    
    print(f"Snapshot restored: {response.status}")
    
    # Restore a database snapshot
    response = await client.restore_snapshot(
        application_type='database',
        app_id='database_id',
        snapshot_id='snapshot_id',
        version_id='version_id'
    )
    
    print(f"Database snapshot restored: {response.status}")
```

## snapshot のデータ構造

### Snapshot

`Snapshot` オブジェクトは、作成された snapshot を表します。

| Property | Type  | Description          |
| -------- | ----- | -------------------- |
| `url`    | `str` | snapshot のダウンロード URL |
| `key`    | `str` | snapshot の一意な識別子     |

### SnapshotInfo

`SnapshotInfo` オブジェクトは、snapshot のメタデータを表します。

| Property     | Type  | Description                    |
| ------------ | ----- | ------------------------------ |
| `id`         | `str` | snapshot の一意な識別子               |
| `size`       | `str` | snapshot のサイズ                  |
| `created_at` | `str` | snapshot の作成タイムスタンプ (ISO 8601) |

## ベストプラクティス

* **自動バックアップ**: Square Cloud は、アプリケーションとデータベースの日次 snapshot を自動的に作成します。
* **無制限の snapshot**: 追加費用なしで、必要な数だけ手動 snapshot を作成できます。
* **保持期間**: snapshot は 30 日間保持されます。
* **復元時間**: 復元処理は、すべてのデータが安定した状態になるまで約 60 秒かかります。
* **テスト**: snapshot を復元する前に、まず新しいアプリケーションまたはデータベースを作成して復元処理をテストすることを検討してください。

## snapshot の制限

* **プランごとの日次 snapshot 数**: 各プランは 1 日あたり `(RAM / 256) * 2` 個の snapshot を生成できます
  * 例: 2048MB の Hobby プランでは、1 日あたり 16 個の snapshot（月間 480 個）
* **保持**: すべての snapshot は直近 30 日間保持されます
* **費用**: snapshot に追加費用はかかりません
