# Migrating to a new server

{% hint style="danger" %}
Page under construction.

**Note:** If you migrate from a multi-db (Agency) setup, you have to migrate all databases.
{% endhint %}

To migrate your UXWizz dashboard to a new server, you have to:

1. [Migrate the MySQL database](#id-1.-migrate-the-mysql-database)
2. [Copy the UXWizz files](#id-2.-copy-the-uxwizz-folder)
3. [Update your domain to point to the new server ](#id-3.-point-your-domain-name-to-new-server)

{% hint style="info" %}
You can also use a database backup service like [SimpleBackups.io](https://simplebackups.com/?via=cristian). \
It's also useful and easy to use for regular, scheduled backups and alerting.
{% endhint %}

### 1. Migrate the MySQL database

{% hint style="warning" %}
Before migrating the database, it's a good idea to first [remove all unnecessary data](https://docs.uxwizz.com/installation/optimization-tips/auto-delete-old-data-cron-jobs#built-in-example-scripts) (e.g. old recordings, heatmaps, etc.) in order to reduce the database size as much as possible.
{% endhint %}

#### 1.1 Backup the current database

UXWizz provides a script to back up the current database. Simply run this in your UXWizz folder:

```bash
sh /server/cron/ust_backup_dh.sh
```

#### 1.2 Copy the .sql.gz file to the new server

We will use `scp` over `ssh` to copy the file directly to the new server. (you can also manually copy this file)

#### 1.2.1 Generate new SSH key

{% hint style="warning" %}
Skip if the new server uses password auth. This key is only used for the scp authentication.
{% endhint %}

On the original server, generate a new SSH key:

```bash
ssh-keygen -t rsa -b 4096
# View and CTRL+C copy the key
cat ~/.ssh/id_rsa.pub
```

On the new server, add the previously generated public key:

```bash
# Open the authorized keys file
nano ~/.ssh/authorized_keys

# Add a new line with the previous contents from id_rsa.pub
ssh-rsa AAAA_EXISTING_KEYS...
ssh-rsa AAAA_THE_NEW_KEY...
```

#### 1.2.2 Copy the backup file

Finally, start copying the backup file (replace the filename and the IP to the new server IP)

```bash
scp your_backup.sql.gz root@12.45.67.89:/tmp/ust.sql.gz
```

#### 1.3 Import the new database

On the new server, unzip the backup, create a new database (preferably with the same name as the old one) and import the sql file:

```bash
gunzip ust.sql.gz
mysql -u root -p -e "CREATE DATABASE new_database_name; USE new_database_name; SET autocommit=0 ; source ust.sql ; COMMIT;"
```

You might also have to create the same users as before. On the original server, you can see existing MySQL users and their permissions:

```bash
 apt-get install percona-toolkit
 pt-show-grants -u YOUR_MYSQL_USER -p YOUR_MYSQL_PASSWORD --database YOUR_UXWIZZ_DB
```

You can copy those grant commands and run them on the new MySQL instance to create the users and give grant privileges.

### 2. Copy the UXWizz folder

Assuming your installation folder is **/var/www/html**:

{% hint style="info" %}
Make sure the folder exists on the new server: `mkdir -p /var/www/html`
{% endhint %}

{% code title="On original server:" %}

```bash
cd /var/www/
scp -r html root@12.45.67.89:/var/www/
```

{% endcode %}

**Note:**  If you had cron jobs enabled, you should also copy the contents of `crontab -e`

### 3. Point your domain name to new server

Go to your domain name's DNS configuration and update the IP to point to the new server.

For any other server configuration (**HTTPS** certificate, MySQL optimized configuration) check out the [UXWizz new server installation guide](https://docs.uxwizz.com/installation/setup-uxwizz-server).
