# Manage Backups and Restores

Exoscale DBaaS are automatically backed up, encrypted and stored
securely in Object Storage.

The frequency and retention of backups is defined in the plan.
Refer to the [plan tables](https://www.exoscale.com/dbaas/) to review
the specifications of each plan. You can also check the specific plan
backup strategy with the command:

```bash
exo dbaas type show pg --backup-config startup-8
```

Output:

```bash
┼────────────────────────────┼──────┼
│ Backup interval (hours)    │ 24   │
│ Max backups                │ 3    │
│ Recovery mode              │ pitr │
│ Frequent backup interval   │ n/a  │
│ Frequent backup max age    │ n/a  │
│ Infrequent backup interval │ n/a  │
│ Infrequent backup max age  │ n/a  │
┼────────────────────────────┼──────┼
```

Some services like `pg` include PITR (point in time
recovery) which enables you to restore data at any
time between 2 backup intervals.

Exoscale DBaaS does not allow you to restore data over an
existing running service. Instead, you can create a new service from an existing
service specifying the time of recovery you need in order to restore data. This operation
is called a **fork**.

### Forks

Here is an example for `pg` that demonstrates how to create a fork of a service from a previous backup,
and how to perform a point-in-time recovery.

- Identify the available backups and periods of time available for a fork:

```bash
exo dbaas show test-pg -z de-fra-1 --backups
```

Output:

```bash
┼─────────────────────────────────────┼───────────────────────────────┼──────────┼
│                NAME                 │             DATE              │   SIZE   │
┼─────────────────────────────────────┼───────────────────────────────┼──────────┼
│ 2021-10-30_20-28_0.00000000.pghoard │ 2021-10-30 20:28:01 +0000 UTC │ 36341760 │
│ 2021-10-31_20-28_0.00000000.pghoard │ 2021-10-31 20:28:04 +0000 UTC │ 36382720 │
┼─────────────────────────────────────┼───────────────────────────────┼──────────┼
```

- Create a fork with a point-in-time recovery:

```bash
exo dbaas create pg startup-4 fork-test-pg --pg-recovery-backup-time 2021-10-30 23:35 --pg-fork-ftest-pg -z de-fra-1
```

The rest of the create options behave like the normal create command. You can specify options and service-specific options at launch time. Refer to the service specific documentation for additional details.

> [!NOTE]
> If you need to fork a service specifying another plan size than the original, 
> note that the storage should be sufficient if you launch the new service with 
> a smaller plan than the original service or fork.

### Backup Schedule

In the example above, we can see that the service is backed up each
day at 20:28.

This slot can be adjusted for most services, but the process is service
specific. Refer to the service specific documentation to change
the backup schedule of a service.

## Upgrades

Running upgrades will follow the same process as
restoring a service from a fork, except that we
will only create the service specifying the fork but
not the recovery backup time. The service will be created
using the latest major and minor supported release
of a service type.

## Termination Protection

Termination protection will ensure your service is not deleted
accidentally by running the `delete` command or any other destructive
operation.

```bash
exo dbaas update test-pg -z de-fra-1 --termination-protection=true
```

Output:


```bash
Updating Database Service "test-pg"...
┼───────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼
│   DATABASE SERVICE    │                                                                                                                                │
┼───────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼
│ Zone                  │ de-fra-1                                                                                                                       │
│ Name                  │ test-pg                                                                                                                        │
│ Type                  │ pg                                                                                                                             │
│ Plan                  │ hobbyist-1                                                                                                                     │
│ Disk Size             │ 8.0 GiB                                                                                                                        │
│ State                 │ running                                                                                                                        │
│ Creation Date         │ 2021-07-05 08:38:39 +0000 UTC                                                                                                  │
│ Update Date           │ 2021-10-21 08:30:54 +0000 UTC                                                                                                  │
│ Nodes                 │ 1                                                                                                                              │
│ Node CPUs             │ 2                                                                                                                              │
│ Node Memory           │ 2.0 GiB                                                                                                                        │
│ Termination Protected │ true                                                                                                                           │
│ Maintenance           │ sunday (10:40:38)                                                                                                              │
│ Backup Schedule       │ 12:52                                                                                                                          │
│ URI                   │ postgres://avnadmin:xxxxx@test-pg-exoscale-08b0165e-ef03-47ec-926f-f01163d557ed.aivencloud.com:21699/defaultdb?sslmode=require │
│ IP Filter             │ 0.0.0.0/0                                                                                                                      │
│ Components            │                                                                                                                                │
│                       │   pg          test-pg-exoscale-08b0165e-ef03-47ec-926f-f01163d557ed.aivencloud.com:21699   route:dynamic   usage:primary       │
│                       │   pgbouncer   test-pg-exoscale-08b0165e-ef03-47ec-926f-f01163d557ed.aivencloud.com:21700   route:dynamic   usage:primary       │
│                       │                                                                                                                                │
│ Users                 │ avnadmin (primary)                                                                                                             │
┼───────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼
```

It is therefore not possible to delete the service. If you decide to delete the service once termination protection is enabled:

```bash
exo dbaas -z de-fra-1 delete test-pg
```

Output:

```bash
[+] Are you sure you want to delete Database Service "test-pg"? [yN]: y
 ✔ Deleting Database Service "test-pg"... 0s
error: Delete "https://api-de-fra-1.exoscale.com/v2/dbaas-service/test-pg": invalid request: The service is protected against termination and shutdown. Remove termination protectio
n first.
```

Revert the flag and then delete the DB instance.
