# Database and media migration

## Migrate ReadyMage database to your local setup

1. Setup SSH access following these [instructions](/project-development/ssh-access-for-magento-cli-database-and-media.md).
2. Connect to SSH through your terminal.
3. Run the following commands to create a database dump zip file on the ReadyMage server: `mysqldump magento --single-transaction --no-tablespaces | zip dump.sql.gz -`
4. Exit SSH by running command `exit`
5. Navigate to the directory where you want to download the database dump on your local machine, for example, Desktop: `cd desktop`
6. Run commands `scp {SSH USER}:/home/magento/dump.sql.gz .`\
   Make sure to replace {SSH USER} part with your ReadyMage SSH username, for example,&#x20;

   `scp store-pwruv-shj-1611224333-user@prod.ssh.us.i.readymage.com:/home/magento/dump.sql.gz .`
7. Unarchive the dump locally.
8. Rename the unarchived file to `dump.sql`
9. Make sure that your local Magento application is running and run the following command to import database dump (./dump.sql should be replaced with the path your dump file)\
   `npm run import-db ./dump.sql`\
   If the import-db command doesn't exist, check known issues below.
10. Navigate to your project repository using terminal and run the following commands:\
    `npm run stop`\
    `npm run start`\
    `npm run cli`\
    `magento indexer:reindex`\
    `magento cache:clean`
11. Create a new admin user to be used on your local setup by running this command `magento admin:user:create`

### Know Issues

**Import-db not available**

If the import-db command is not available add the following line to the package.json file located in the root folder of the cloned repository:

```
"import-db": "magento-scripts import-db"
```

Your package.json file scripts variable should have the import-db add&#x20;

```
"scripts": {
        "import-db": "magento-scripts import-db",
        "start": "magento-scripts start",
        "stop": "magento-scripts stop",
        "cli": "magento-scripts cli",
        "logs": "magento-scripts logs",
        "link": "magento-scripts link",
        "status": "magento-scripts status",
        "exec": "magento-scripts exec"
    },
```

## Migrate local database to ReadyMage

1. Navigate to your project repository using terminal and run the following command: `npm run status`
2. Copy the name of the MySQL container, for example,  `readymage-mystore-pwruv-shj-1611224333-master_mysql`
3. Create database dump by running the following command: `docker exec {MYSQL CONTAINER NAME} mysqldump magento --single-transaction --no-tablespaces -u magento -pmagento --result-file=/tmp/dump.sql`

   Replace {MYSQL CONTAINER NAME} by the container name you copied in step 2.
4. Download the dump to your Desktop by running the following command:\
   `docker cp {MYSQL CONTAINER NAME}:/tmp/dump.sql ~/Desktop/dump.sql`
5. Before importing the database dump to your ReadyMage instance open the database dump and change all the mentions of  `utf8mb4_general_ci` to `utf8mb4_0900_ai_ci`
6. Setup SSH access following these [instructions](/application-management/ssh-access.md). You don't have to connect to it.
7. Move your database to the ReadyMage server by running the following command from your terminal:  `scp {LOCAL PATH TO YOUR DUMP} {SSH USER}:/home/magento/dump.sql`\
   Replace `{LOCAL PATH TO YOUR DUMP}` with the path to your database dump and `{SSH USER}` with your SSH username, for example,&#x20;

   `scp ~/Desktop/dump.sql store-pwruv-shj-1611224333-user@prod.ssh.us.i.readymage.com:/home/magento/dump.sql`
8. Drop the database `DROP DATABASE magento; CREATE DATABASE magento;`
9. Connect to SSH and run the following command: `mysql magento < dump.sql`
10. Connect to mysql by running `mysql` command in terminal.
11. Run the following commands (replace **{STORE URL}** with your instance store URL):\
    `use magento`

    `UPDATE core_config_data SET value="elasticsearch" where path="catalog/search/elasticsearch7_server_hostname";`&#x20;
12. `UPDATE core_config_data SET value="9200" where path="catalog/search/elasticsearch7_server_port";`

    `UPDATE core_config_data SET value="2" where path="system/full_page_cache/caching_application";`

    `UPDATE core_config_data SET value="http://{STORE URL}/" where path="web/unsecure/base_url";`

    `UPDATE core_config_data SET value="https://{STORE URL}/" where path="web/secure/base_url";`

    `UPDATE core_config_data SET value="1" where path="web/secure/use_in_frontend";`

    `UPDATE core_config_data SET value="1" where path="web/secure/use_in_adminhtml";`\
    `UPDATE core_config_data SET value="{STORE URL}" where path="web/cookie/cookie_domain";`

    `exit`<br>
13. Access SSH and run the following commands:\
    `magento indexer:reindex`\
    `magento cache:clean`

### Known Issues

* **Error: Catalog Search index exception: Could not ping search engine: No alive nodes found in your cluster**\
  Update elasticsearch port to 9200 in your instance database core config table. Follow instructions [here](<https://devdocs.magento.com/guides/v2.4/config-guide/elasticsearch/configure-magento.html >).

* **Error: ERROR 1227 (42000) at line XX: Access denied; you need (at least one of) the SUPER or SET\_USER\_ID privilege(s) for this operation** \
  Please check the reported line. If `DEFINER` is specified, please remove it.

  <div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p>To remove it from the entire SQL dump file (<code>DATA-DUMP.sql</code>), you can use the following <code>sed</code> command:<br><code>sed 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' DATA-DUMP.sql > DATA-DUMP--cleared.sql</code></p></div>

## Migrate ReadyMage media to your local setup

1. Setup SSH access following these [instructions](/application-management/ssh-access.md).
2. Run command to download Media Content to your desktop (change the \~/Desktop/media path to your path where you want to dump the media):  `rsync -azh --partial --append {SSH USER}:/home/magento/media/ ~/media`\
   Replace `{SSH USER}` with your SSH username, for example, \
   `rsync -azP store-pwruv-shj-1611224333-user@prod.ssh.us.i.readymage.com:/home/magento/media/ ./Desktop/`
3. Move downloaded media content to your repository `pub/media` folder.
4. Navigate to the repository in the terminal and run the following commands:\
   `npm run cli`\
   `magento indexer:reindex`\
   `magento cache:clean`

## Migrate local media to ReadyMage

1. Setup SSH access following these [instructions](/application-management/ssh-access.md).
2. Navigate to your repository `pub` folder in terminal and run the command: `rsync -azP media/ {SSH USER}:/home/magento/media/`

   Replace `{SSH USER}` with your SSH username, for example, \
   `rsync -azh --partial --append --chmod=Du=rwx,Dg=rwx,Do=rx,Fu=rw,Fg=rw,Fo=r media/ store-pwruv-shj-1611224333-user@prod.ssh.us.i.readymage.com:/home/magento/media/`
3. Connect to SSH and run the following commands:\
   `magento indexer:reindex`\
   `magento cache:clean`
4. Restart Varnish from `Infrastructure Management > Manage Services`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.readymage.com/project-development/database-and-media-migration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
