# Restoring a Backup

Restoring backups is essential for recovery, environment duplication, or rollback scenarios. Elestio supports restoring backups both through its built-in dashboard and via command-line tools like <span class="s3">mysql</span> and <span class="s3">mysqldump</span>. You can also restore from inside Docker Compose environments. This guide provides detailed steps for full and partial restores using each method and explains how to address common errors that occur during restoration.

## **Restoring from a Backup via Terminal**

This method is used when you’ve created a `<span class="s3">.sql</span>` dump file using <span class="s3">mysqldump</span>. You can restore it using the <span class="s3">mysql</span> command-line client. This approach is useful for restoring backups to new environments, during version upgrades, or testing data locally.

#### **Create the target database if it does not exist**

If the database you’re restoring into doesn’t already exist, you must create it first:

```
mysql -u <username> -p -h <host> -P <port> -e "CREATE DATABASE <database_name>;"
```

You’ll be prompted to enter the password after running the command.

#### **Run MySQL to import the backup**

This command restores the full contents of the <span class="s2">.sql</span> file into the specified database:

```
mysql -u <username> -p -h <host> -P <port> <database_name> < <backup_file>.sql
```

You’ll again be prompted for the password. This command restores everything from the dump file, including schema and data.

## **Restoring via Docker Compose**

If your MySQL service is deployed using Docker Compose, you can restore the database inside the container environment. This is useful when MySQL runs in an isolated Docker setup, and you want to handle all backup and restore processes inside that environment.

#### **Copy the backup into the container**

Use <span class="s2">docker cp</span> to move the <span class="s2">.sql</span> file from your host machine to the MySQL container:

```
docker cp ./manual_backup.sql $(docker-compose ps -q mysql):/tmp/manual_backup.sql
```

#### **Run the restore inside the container**

Use the <span class="s2">mysql</span> CLI tool from within the container to restore the file:

```
docker-compose exec mysql \
  bash -c "mysql -u \$MYSQL_USER -p\"\$MYSQL_PASSWORD\" \$MYSQL_DATABASE < /tmp/manual_backup.sql"
```

Make sure your environment variables in the Docker Compose file (<span class="s1">MYSQL\_USER</span>, <span class="s1">MYSQL\_PASSWORD</span>, <span class="s1">MYSQL\_DATABASE</span>) match the values used here.

## **Partial Restores**

MySQL supports partial restores when the dump file is created with selective options in <span class="s1">mysqldump</span>. For example, you can restore just a specific table or only schema definitions.

#### **Restore a specific table**

If you created a dump for a specific table using <span class="s1">mysqldump -t</span>, you can restore it independently:

```
mysql -u <username> -p -h <host> -P <port> <database_name> < <table_dump_file>.sql
```

#### **Restore schema only (no data)**

To restore only the schema (no table contents), ensure that your dump file was created using:

```
mysqldump -u <username> -p -h <host> -P <port> --no-data <database_name> > schema_only.sql
```

Then restore it like this:

```
mysql -u <username> -p -h <host> -P <port> <database_name> < schema_only.sql
```

Partial restores work best when the original backup was generated with the appropriate level of granularity.

## **Common Errors &amp; How to Fix Them**

Errors during restore are often caused by permission issues, incorrect formats, or existing conflicting objects. Understanding the error messages and their causes will help you recover faster and avoid data loss.

**1. Access denied for user**

```
ERROR 1045 (28000): Access denied for user 'user'@'host'
```

Ensure you are using the correct username/password and that the user has privileges to access the target database.

**2. Table already exists**

```
ERROR 1050 (42S01): Table 'my_table' already exists
```

Either drop the target database before restoring:

```
mysql -u <username> -p -h <host> -P <port> -e "DROP DATABASE <database_name>;"
mysql -u <username> -p -h <host> -P <port> -e "CREATE DATABASE <database_name>;"
```

Or manually drop the conflicting tables before restore.

**3. ERROR 1064 (Syntax Error)**

```
ERROR 1064 (42000): You have an error in your SQL syntax...
```

Check if you’re trying to import a binary or incorrectly formatted file. Ensure you’re using <span class="s1">.sql</span> text dump files with the <span class="s1">mysql</span> command and not raw <span class="s1">.ibd</span> or <span class="s1">.frm</span> files.

**4. ERROR 1049 (Unknown Database)**

```
ERROR 1049 (42000): Unknown database 'mydatabase'
```

The specified database doesn’t exist. Create it manually before restoring.

```
mysql -u <username> -p -h <host> -P <port> -e "CREATE DATABASE mydatabase;"
```