# Restore files, email and databases

> Put a backup back on the server it came from, and know what each choice overwrites before you submit.

A restore writes a backup back onto the server it was taken from. This page covers **Files**, **Email** and **Database** plans. It cannot be undone. For a server that is gone, see [Restore to a new server](/docs/restores/new-server).

A restore writes the whole backup. To get back one file or folder, download it instead, unless the backup is encrypted. See [Download and share](/docs/restores/download-share).

## Prerequisites

- A Clokeep account with the Admin or Operator role. A Viewer or a Customer cannot restore. See [Users and roles](/docs/access/users-roles).
- The agent installed on the server. A restore waits as **Queued** until the agent picks it up.
- For files, a target folder the agent may write to. See [Allowed folders](#allowed-folders) below.

## Start a restore

<Steps>
<Step title="Select the backup">
Open the backup in **Browse Backups**. See [Browse a backup](/docs/restores/browse).
</Step>
<Step title="Open the form">
Click **Restore selected backup**. The **Restore backup to server** form opens. Its fields depend on the plan type, as described below. For a Plesk subscription, a cPanel account or one mailbox, see [Restore a Plesk or cPanel account](/docs/restores/hosting-account).

If the button is greyed out, hover over it to see why. For example, "The storage that held this backup was deleted.", "This backup was not found on storage at the last verification and cannot be restored.", or a text that starts with "This backup cannot be restored:".
</Step>
<Step title="Submit and follow the run">
Fill in the form and click **Submit**. A **Restore queued** message appears. Click **View run** in it.
</Step>
</Steps>

To start a restore from a script, see [Queue a restore through the API](/docs/developers/queue-restore).

## Files

A **Files** or **Email** plan's backup is restored as files into a folder you choose. So is a Plesk or cPanel backup whose account no longer exists, and a very old one.

- **Target path on server**: an absolute path, such as `/srv/restore`, without `.` or `..` parts.
- **If a file already exists**: what happens to a file that is already at the same place.

<Frame caption="The restore form of a Files backup.">
![The Restore backup to server form with Target path on server set to /srv/restore and If a file already exists set to Overwrite existing files](/images/docs/restore-files-form.png)
</Frame>

Each backup path is a folder of its own inside the target path, named after the last part of the path, or after its **Label**. A backup of `/var/www` restored to `/srv/restore` lands in `/srv/restore/www`. An **Email** plan's folders are named `mail-0`, `mail-1` and so on, one per mail path.

The three choices:

- **Overwrite existing files**: the file from the backup replaces the existing file, with the backup's permissions and modified time.
- **Skip existing files**: the existing file stays as it is.
- **Rename (keep both)**: the existing file stays, and the backup's copy is written beside it as `name.restored-1.ext`.

Files and folders in the target that are not in the backup stay, except as the Warning below says.

An incremental backup is restored from its whole chain. The agent first puts together each file's newest version up to the backup you picked, then writes that onto the target. Each choice above then applies to that newest version.

<Warning>
With **Overwrite existing files**, an incremental restore also deletes from the target path the files that were deleted on the server between the chain's full backup and the backup you picked. **Skip existing files** and **Rename (keep both)** never delete a file that is already there.
</Warning>

### Allowed folders

The agent writes files only below the folders listed under `restore` and `allowed_roots` in its settings file, `/etc/clokeep-agent.yaml`.

- Plesk: the subscription and mail folders, if Plesk was found when the agent enrolled.
- cPanel: the folders that hold the accounts' home folders, such as `/home`.
- Any other server: `/var/lib/clokeep-restore`. The agent creates this folder on the first restore into it. Restore there, then move the files where you need them, or allow other folders.

When the list is empty, the agent allows `/var/lib/clokeep-restore` only. Once you list folders, only those are allowed: add `/var/lib/clokeep-restore` to the list too if you still want it. Agents older than 3.2.0 refuse every file restore while the list is empty.

A refused path fails the run with "restore target … is not within an allowed root".

Files land one folder down. To restore `/var/www` in place, enter `/var` as **Target path on server**, and allow `/var` or a folder above it.

From agent 3.2.0, enrolling the agent again keeps the folders you added. It changes only the panel address and the agent's key in the file.

To allow other folders, open `/etc/clokeep-agent.yaml` and find `allowed_roots` under `restore:`. On a Plesk or cPanel server, the list already has folders: keep them and add yours below them. On any other server, the line reads `allowed_roots: []`: replace it with your folders, indented like the lines around it.

```yaml /etc/clokeep-agent.yaml
restore:
    allowed_roots:
        - /var/lib/clokeep-restore
        - /srv/restore
    default_mode: ""
```

Restart the agent with `sudo systemctl restart clokeep-agent` while no backup or restore runs: a restart ends it. Check it with `sudo systemctl status clokeep-agent`.

If the file is no longer valid YAML, for example with a second `restore:` line, the agent does not start. `sudo journalctl -u clokeep-agent` then shows a line that starts with `load config failed: parse config:`.

## A database

A **Database** plan's backup shows **Restore into database connection**. It lists the server's database connections, including the plan's own live one. The dump is imported into the database that the connection you pick names, which must already exist.

<Warning>
MySQL: each table in the backup is dropped and created again with the backup's rows. Tables that are not in the backup stay.

To keep the live database untouched, add a connection to an empty database on the server's **Database Connections** tab, and pick it here.
</Warning>

PostgreSQL: the dump deletes and replaces nothing. The import runs as one transaction and stops at the first error. If anything fails, for example because a table of the backup already exists, the run fails, nothing is imported, and the database stays as it was. The run's error ends with the message from `psql`. So restore a PostgreSQL backup into an empty database: add a connection to one on the server's **Database Connections** tab, and pick it here.

The server needs the `mysql` or `psql` client.

## One restore at a time, and stopping one

A server runs one restore at a time. While one is **Queued** or **Running**, a second shows **Restore already active**.

A restore stops like any run. See [Run and stop a backup](/docs/backups/run-stop). What it already wrote stays on the server and is not rolled back. A MySQL restore that stops midway can leave the database half imported: a table can be left empty or missing, because each one is dropped before the import refills it. A PostgreSQL restore that stops or fails is rolled back.

A failed restore raises a **Restore failed** alert. What it wrote before the failure stays, with the same risk for MySQL databases.

## Next steps

<CardGroup cols="2">
<Card title="Restore a Plesk or cPanel account" href="/docs/restores/hosting-account" icon="layers">
Restore a Plesk subscription, a cPanel account or one mailbox.
</Card>
<Card title="Download and share" href="/docs/restores/download-share" icon="book">
Get one file out of a backup without restoring it.
</Card>
</CardGroup>
