migrating_mariadb_between_hosts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

migrating_mariadb_between_hosts [2025/04/13 01:56] – created osomigrating_mariadb_between_hosts [2025/04/13 02:01] (current) – [📦 Notes on Volume Migration and Backup] oso
Line 88: Line 88:
 1. Find the volume's path on the source server: 1. Find the volume's path on the source server:
  
-    <code bash> +<code bash> 
-    docker inspect mariadb | grep Source +docker inspect mariadb | grep Source 
-    </code>+</code>
  
 2. Stop the container, then tar the volume contents: 2. Stop the container, then tar the volume contents:
  
-    <code bash> +<code bash> 
-    docker stop mariadb +docker stop mariadb 
-    sudo tar -czvf mariadb_data.tar.gz /var/lib/docker/volumes/your_volume_name/_data +sudo tar -czvf mariadb_data.tar.gz /var/lib/docker/volumes/your_volume_name/_data 
-    </code>+</code>
  
 3. Copy the `.tar.gz` to the new server: 3. Copy the `.tar.gz` to the new server:
  
-    <code bash> +<code bash> 
-    scp mariadb_data.tar.gz user@newserver:+scp mariadb_data.tar.gz user@newserver:
-    </code>+</code>
  
 4. Extract into a Docker volume path (on the new server): 4. Extract into a Docker volume path (on the new server):
  
-    <code bash> +<code bash> 
-    docker volume create mariadb_data +docker volume create mariadb_data 
-    sudo tar -xzvf mariadb_data.tar.gz -C /var/lib/docker/volumes/mariadb_data/_data +sudo tar -xzvf mariadb_data.tar.gz -C /var/lib/docker/volumes/mariadb_data/_data 
-    </code>+</code>
  
 5. Now `docker compose up -d` with the same `docker-compose.yml`, and your DB will be live. 5. Now `docker compose up -d` with the same `docker-compose.yml`, and your DB will be live.
Line 116: Line 116:
 --- ---
  
 +Example for copying the volume:
 +
 +<code bash>
 +#!/bin/bash
 +set -e
 +
 +VOLUME_ID="4042cf30bc39026f2b18ef4f2fcd2893470efba3637f5e18f1a762856debba0d"
 +VOLUME_PATH="/var/lib/docker/volumes/${VOLUME_ID}/_data"
 +ARCHIVE_NAME="mariadb_volume_backup.tar.gz"
 +DEST_HOST="[email protected]"
 +DEST_PATH="/tmp"
 +
 +echo "==> Stopping any container using the volume..."
 +docker ps -q --filter volume="$VOLUME_ID" | while read cid; do
 +    docker stop "$cid"
 +done
 +
 +echo "==> Creating archive from volume data..."
 +sudo tar -czf "$ARCHIVE_NAME" -C "$VOLUME_PATH" .
 +
 +echo "==> Copying archive to destination server..."
 +scp "$ARCHIVE_NAME" "$DEST_HOST:$DEST_PATH"
 +
 +echo "==> Done. You can now restore it on serv1 using the corresponding import steps."
 +</code>
 +
 +then restore in the destination server...
 +
 +<code bash>
 +#!/bin/bash
 +set -e
 +
 +VOLUME_ID="mariadb_restored_volume"
 +ARCHIVE_PATH="/tmp/mariadb_volume_backup.tar.gz"
 +VOLUME_PATH="/var/lib/docker/volumes/${VOLUME_ID}/_data"
 +
 +echo "==> Creating target volume..."
 +docker volume create "$VOLUME_ID"
 +
 +echo "==> Extracting data into new volume..."
 +sudo tar -xzf "$ARCHIVE_PATH" -C "$VOLUME_PATH"
 +
 +echo "==> Fixing permissions..."
 +sudo chown -R 999:999 "$VOLUME_PATH"  # 999 is default mysql UID/GID in official image
 +
 +echo "==> Starting MariaDB container..."
 +docker run -d \
 +  --name mariadb \
 +  -v "${VOLUME_ID}:/var/lib/mysql" \
 +  -e MYSQL_ROOT_PASSWORD=your_root_pass \
 +  -p 3306:3306 \
 +  mariadb:latest
 +
 +echo "==> Done! MariaDB should be up with old data."
 +</code>
 ===== ✅ Final Thoughts ===== ===== ✅ Final Thoughts =====
  
migrating_mariadb_between_hosts.1744509374.txt.gz · Last modified: 2025/04/13 01:56 by oso