# Restore from Backup

You can restore a database from a previous backup either in-place or to a different cluster.

{% hint style="info" %}
**Note:** Choose the resources carefully for your new database cluster. The operation may fail if there is insufficient disk space or RAM. We recommend at least 4 GB of RAM for the new database, which can be scaled down after the restore operation.
{% endhint %}

## List all available backups

To restore from a backup, you need to provide its ID. You can request a list of all available backups.

### Endpoint

Use the following endpoint to get a list of all available backups for a PostgreSQL cluster:

`https://api.ionos.com/databases/postgresql/clusters/backups`

{% hint style="info" %}
**Note:** The sample UUID is `498ae72f-411f-11eb-9d07-046c59cc737e`.
{% endhint %}

### Request

```bash
curl --include \
    --user "clientname@ionos.com:Mb2.r5oHf-0t" \
    --header "Content-Type: application/json" \
```

### Response

```json
{
  "id": "backups",
  "type": "collection",
  "items": [
    {
      "type": "backup",
      "id": "dcd31531-3ac8-11eb-9feb-046c59cc737e",
      "properties": {
        "clusterId": "498ae72f-411f-11eb-9d07-046c59cc737e",
        "version": "15",
        "earliestRecoveryTargetTime": "2021-12-08T14:02:59Z"
      },
      "metadata": {...}
    },
    {
      "type": "backup",
      "id": "2b0cd7f8-5924-11ec-b621-da289d52ded8",
      "properties": {
        "clusterId": "2c12ad79-5cba-b2fc-b621-da289d52ded8",
        "version": "16",
        "earliestRecoveryTargetTime": "2021-12-09T14:02:59Z"
      },
      "metadata": {...}
    }
  ],
  "offset": 0,
  "limit": 2,
  "_links": {}
}
```

## List backups per cluster

You can also list backups belonging to a specific cluster. For this, you need a `clusterId`.

### Endpoint

Use the following endpoint to get a list backup per cluster:

`https://api.ionos.com/databases/postgresql/clusters/{clusterId}/backups`

{% hint style="info" %}
**Note:** The sample UUID is `498ae72f-411f-11eb-9d07-046c59cc737e`.
{% endhint %}

### Request

```bash
curl --include \
    --user "clientname@ionos.com:Mb2.r5oHf-0t" \
    --header "Content-Type: application/json" \
```

### Response

```json
{
  "type": "collection",
  "id": "498ae72f-411f-11eb-9d07-046c59cc737e/backups",
  "items": [
    {
      "type": "backup",
      "id": "dcd31531-3ac8-11eb-9feb-046c59cc737e",
      "metadata": {...},
      "properties": {
        "clusterId": "498ae72f-411f-11eb-9d07-046c59cc737e",
        "version": "15",
        "isActive": true,
        "earliestRecoveryTargetTime": "2020-12-08T20:13:49.000Z"
      }
    }
  ],
  "offset": 0,
  "limit": 1,
  "links": {}
}
```

## Restore from backup in-place

You can now trigger a restore of the chosen cluster. Your database will not be available during the restore operation.

The `recoveryTargetTime` is an ISO-8601 timestamp that specifies the point in time up to which data will be restored. It is non-inclusive, meaning the recovery will stop right before this timestamp.

You should choose a backup with the most recent `earliestRecoveryTargetTime`. However, this timestamp should be strictly less than the desirable `recoveryTargetTime`. For example suppose you have three backups with `earliestRecoveryTargetTime` from 1st, 2nd and 3rd of january 2022 at 0:00:00 espectively. If you want to restore to the `recoveryTargetTime` `2022-01-02T20:00:00Z`, you should use chose the backup from 2nd of january.

{% hint style="info" %}
**Note:** To restore a cluster in-place you can only use backups from that cluster. If that backup is from an older Postgres version (after a major version upgrade), only the data is applied. The database will continue running the updated version.
{% endhint %}

### Endpoint

Use the following endpoint to get restore from backup in-place: `https://api.ionos.com/databases/postgresql/clusters/{clusterId}/restore`

{% hint style="info" %}
**Note:** The sample UUID is `498ae72f-411f-11eb-9d07-046c59cc737e`.
{% endhint %}

### Request

```bash
curl --include \
    --request POST \
    --user "clientname@ionos.com:Mb2.r5oHf-0t" \
    --header "Content-Type: application/json" \
    --data-binary '{
      "backupId": "dcd31531-3ac8-11eb-9feb-046c59cc737e",
      "recoveryTargetTime": "2020-12-10T12:37:50.000Z"
    }' \
```

### Response

The API will respond with a `202 Accepted` status code if the request is successful.

## Restorie to a different target

You can also create a new cluster as a copy from a backup by adding the `fromBackup` field in your `POST` request. You can use any backup from any cluster as long as the target cluster has the same or a more recent version of PostgreSQL.

The field takes the same arguments as shown above, `backupId` and `recoveryTargetTime`.

{% hint style="info" %}
**Note:** A backup is a continuous process, so if you have any ongoing workload in your current cluster, do not expect the old data to appear instantly. If you wish to avoid a slight delay, you need to stop the workload prior to backing up.
{% endhint %}

If you want a new database to have all the data from the old one (clone database) use a backup with the most recent `earliestRecoveryTargetTime` and omit `recoveryTargetTime` from the `POST` request.

{% hint style="info" %}
**Note:** You can use the `POST` and `fromBackup` functionality to move a database to another region since the new database cluster doesn't need to be in the same region as the original one.
{% endhint %}

### Endpoint

Use the following endpoint to restore to a different target: `https://api.ionos.com/databases/postgresql/clusters`

### Request

```bash
curl --include \
    --request POST \
    --user "clientname@ionos.com:Mb2.r5oHf-0t" \
    --header "Content-Type: application/json" \
    --data-binary '{
      "metadata": {},
      "properties: {
        "postgresVersion": "16",
        "instances": 2,
        "cores": 4,
        "ram": 2048,
        "location": "DE/TXL",
        "storageSize": 20000,
        "storageType": "HDD",
        "displayName": "a good name for a database",
        "synchronizationMode": "ASYNCHRONOUS",
        "credentials": {
          "username": "dsertionos",
          "password": "knight-errant"
        },
        "connections": [
          {
            "datacenterId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "lanId": "x",
            "cidr": "x.x.x.x/24",
          }
        ],
        "fromBackup": {
          "backupId": "dcd31531-3ac8-11eb-9feb-046c59cc737e",
          "recoveryTargetTime": "2020-12-23T09:37:50.000Z"
        }
      }
    }' \
```
