From 2fa43371cde3b488c46bde0065ab5e1c57f27fc7 Mon Sep 17 00:00:00 2001
From: Anel Husakovic <anel@mariadb.org>
Date: Mon, 12 Feb 2024 15:03:56 +0100
Subject: [PATCH] Automation script for MySQL 8.0 data migration

---
 examples/migration-8.0/.env                   |  7 ++
 examples/migration-8.0/compose-mysql8.0.yml   | 94 +++++++++++++++++++
 .../mysql-dump-data-utf8mb4_unicode_ci.sql    | 50 ++++++++++
 .../dump-data/mysql-dump-data.sql             | 50 ++++++++++
 examples/migration-8.0/dump-mysql.sh          | 14 +++
 examples/migration-8.0/migrate-mariadb.sh     |  5 +
 examples/migration-8.0/mysql/mysql-data.sql   |  3 +
 7 files changed, 223 insertions(+)
 create mode 100644 examples/migration-8.0/.env
 create mode 100644 examples/migration-8.0/compose-mysql8.0.yml
 create mode 100644 examples/migration-8.0/dump-data/mysql-dump-data-utf8mb4_unicode_ci.sql
 create mode 100644 examples/migration-8.0/dump-data/mysql-dump-data.sql
 create mode 100755 examples/migration-8.0/dump-mysql.sh
 create mode 100644 examples/migration-8.0/migrate-mariadb.sh
 create mode 100644 examples/migration-8.0/mysql/mysql-data.sql

diff --git a/examples/migration-8.0/.env b/examples/migration-8.0/.env
new file mode 100644
index 0000000..67f58ab
--- /dev/null
+++ b/examples/migration-8.0/.env
@@ -0,0 +1,7 @@
+DB_ROOT_PASSWORD='secret'
+DB_USER='testuser'
+DB_PASSWORD='password'
+DB_DATABASE='testdb'
+MYSQL_name='mysql-container'
+MARIADB_DUMP_name='mariadb-container-dump'
+MARIADB_MIGRATED_name='mariadb-migrated-mysql8.0'
\ No newline at end of file
diff --git a/examples/migration-8.0/compose-mysql8.0.yml b/examples/migration-8.0/compose-mysql8.0.yml
new file mode 100644
index 0000000..fedd730
--- /dev/null
+++ b/examples/migration-8.0/compose-mysql8.0.yml
@@ -0,0 +1,94 @@
+version: "3"
+
+services:
+  mysql:
+    environment:
+      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
+      MYSQL_USER: ${DB_USER}
+      MYSQL_PASSWORD: ${DB_PASSWORD}
+      MYSQL_DATABASE: ${DB_DATABASE}
+    container_name: ${MYSQL_name}
+    image: mysql:8.3.0
+    healthcheck:
+      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
+      interval: 20s
+      timeout: 20s
+      retries: 2
+      start_period: 0s
+    volumes:
+      # Preload files for MySQL data
+      - ./mysql:/docker-entrypoint-initdb.d:z
+      # We have to save MySQL volume that will be used in upgrade
+      - dbdata:/var/lib/mysql
+    networks:
+      - backend
+
+# Sidecar for dumping files
+  mariadb-dump:
+    environment:
+      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
+      MARIADB_USER: ${DB_USER}
+      MARIADB_PASSWORD: ${DB_PASSWORD}
+    container_name: ${MARIADB_DUMP_name}
+    image: mariadb:lts
+    depends_on:
+     mysql:
+       condition: service_healthy
+    healthcheck:
+      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
+      start_period: 10s
+      interval: 20s
+      timeout: 20s
+      retries: 3
+    # command: >
+    #   bash -c "
+    #   echo 'MariaDB service started. Dump MySQL data ...'
+    #   mariadb-dump -h mysql-container -uroot -psecret testdb > /etc/dump/mysql-dump-data.sql"
+    # user: ${UID}:${EUID}
+    volumes:
+      - mysqldump:/etc/dump/
+     # This will not exit container, but command: will
+      - ./dump-mysql.sh:/docker-entrypoint-initdb.d/dump-mysql.sh
+    networks:
+      - backend
+    # entrypoint: ["/docker-entrypoint-initdb.d/dump-mysql.sh"]
+
+# We cannot share the same dump directory, we need to stop mariadb-dump
+  stopper:
+    image: docker:20.10
+    depends_on:
+     mariadb-dump:
+      condition: service_healthy
+    volumes:
+      - /var/run/docker.sock:/var/run/docker.sock
+    command: ["sh", "-c", "docker stop mariadb-container-dump"]
+
+# Sidecar for insert dump file
+  mariadb-migrated-from-mysql8:
+    environment:
+      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
+      MARIADB_USER: ${DB_USER}
+      MARIADB_PASSWORD: ${DB_PASSWORD}
+    container_name: ${MARIADB_MIGRATED_name}
+    image: mariadb:lts
+    depends_on:
+     mariadb-dump:
+       condition: service_completed_successfully
+    volumes:
+      - mysqldump:/etc/dump/
+      - ./migrate-mariadb.sh:/docker-entrypoint-initdb.d/migrate-mariadb.sh
+    networks:
+      - backend
+
+volumes:
+  dbdata: {}
+  # sudo chown -R 999:999 ${PWD}/dump-data # host
+  mysqldump:
+    driver: local
+    driver_opts:
+      type: none
+      device: "${PWD}/dump-data"
+      o: bind
+
+networks:
+  backend:
\ No newline at end of file
diff --git a/examples/migration-8.0/dump-data/mysql-dump-data-utf8mb4_unicode_ci.sql b/examples/migration-8.0/dump-data/mysql-dump-data-utf8mb4_unicode_ci.sql
new file mode 100644
index 0000000..1ada824
--- /dev/null
+++ b/examples/migration-8.0/dump-data/mysql-dump-data-utf8mb4_unicode_ci.sql
@@ -0,0 +1,50 @@
+-- MariaDB dump 10.19  Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64)
+--
+-- Host: mysql-container    Database: testdb
+-- ------------------------------------------------------
+-- Server version	8.3.0
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8mb4 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Table structure for table `countries`
+--
+
+DROP TABLE IF EXISTS `countries`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `countries` (
+  `name` char(20) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `countries`
+--
+
+LOCK TABLES `countries` WRITE;
+/*!40000 ALTER TABLE `countries` DISABLE KEYS */;
+INSERT INTO `countries` VALUES
+('Bosnia & Herzegovina');
+/*!40000 ALTER TABLE `countries` ENABLE KEYS */;
+UNLOCK TABLES;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2024-02-12 13:48:45
diff --git a/examples/migration-8.0/dump-data/mysql-dump-data.sql b/examples/migration-8.0/dump-data/mysql-dump-data.sql
new file mode 100644
index 0000000..497e056
--- /dev/null
+++ b/examples/migration-8.0/dump-data/mysql-dump-data.sql
@@ -0,0 +1,50 @@
+-- MariaDB dump 10.19  Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64)
+--
+-- Host: mysql-container    Database: testdb
+-- ------------------------------------------------------
+-- Server version	8.3.0
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8mb4 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Table structure for table `countries`
+--
+
+DROP TABLE IF EXISTS `countries`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `countries` (
+  `name` char(20) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `countries`
+--
+
+LOCK TABLES `countries` WRITE;
+/*!40000 ALTER TABLE `countries` DISABLE KEYS */;
+INSERT INTO `countries` VALUES
+('Bosnia & Herzegovina');
+/*!40000 ALTER TABLE `countries` ENABLE KEYS */;
+UNLOCK TABLES;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2024-02-12 13:48:45
diff --git a/examples/migration-8.0/dump-mysql.sh b/examples/migration-8.0/dump-mysql.sh
new file mode 100755
index 0000000..db2cff6
--- /dev/null
+++ b/examples/migration-8.0/dump-mysql.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+echo 'MariaDB service started. Dump MySQL data ...'
+# Run your commands and exit container
+whoami # mysql"
+# sh -c "chown -R mysql:mysql /etc/dump" # Operation permitted
+# sh -c "ls -la /etc/dump"
+sh -c "mariadb-dump -h mysql-container -uroot -psecret testdb > /etc/dump/mysql-dump-data.sql"
+sh -c "ls -la /etc/dump/"
+echo "List before"
+sh -c "cp /etc/dump/mysql-dump-data.sql /etc/dump/mysql-dump-data-utf8mb4_unicode_ci.sql"
+sh -c "ls -la /etc/dump/"
+echo "List after"
+sh -c "sed -i 's/utf8mb4_0900_ai_ci/utf8mb4_unicode_ci/g' /etc/dump/mysql-dump-data-utf8mb4_unicode_ci.sql"
\ No newline at end of file
diff --git a/examples/migration-8.0/migrate-mariadb.sh b/examples/migration-8.0/migrate-mariadb.sh
new file mode 100644
index 0000000..7ad79d5
--- /dev/null
+++ b/examples/migration-8.0/migrate-mariadb.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+echo "Show data in MariaDB"
+mariadb -uroot -psecret -e "create database testdb;"
+mariadb -uroot -psecret testdb < /etc/dump/mysql-dump-data-utf8mb4_unicode_ci.sql
+mariadb -uroot -psecret  -e "show databases; select * from countries;"
\ No newline at end of file
diff --git a/examples/migration-8.0/mysql/mysql-data.sql b/examples/migration-8.0/mysql/mysql-data.sql
new file mode 100644
index 0000000..c71d958
--- /dev/null
+++ b/examples/migration-8.0/mysql/mysql-data.sql
@@ -0,0 +1,3 @@
+DROP TABLE IF EXISTS countries;
+CREATE TABLE countries(name char(20));
+INSERT INTO countries values ("Bosnia & Herzegovina");
\ No newline at end of file
-- 
GitLab