migration.sh


Overview

migration.sh is a Bash script designed to facilitate backup and restore operations for RAGFlow's Docker volumes. It manages persistent data for critical services including MySQL, MinIO, Redis, and Elasticsearch by creating compressed archive backups or restoring data from these backups into Docker volumes.

The script ensures data integrity by verifying Docker availability, checking for running containers using the target volumes, and confirming destructive operations with the user. It provides clear user guidance, error handling, and detailed status messages throughout the process.


Functionality Summary


Detailed Explanation of Script Components

Global Variables

Variable

Description

DEFAULT_BACKUP_FOLDER

Default directory where backups are stored (backup)

VOLUMES

Array of Docker volume names to back up/restore: docker_mysql_data, docker_minio_data, docker_redis_data, docker_esdata01

BACKUP_FILES

Corresponding backup archive filenames: mysql_backup.tar.gz, minio_backup.tar.gz, redis_backup.tar.gz, es_backup.tar.gz


Functions


show_help()

Displays usage instructions, available operations, parameters, and examples.

Usage:

show_help

Output:


check_docker()

Verifies if Docker is running and accessible. Exits with error if Docker is not available.

Usage:

check_docker

Behavior:


volume_exists(volume_name)

Checks if a Docker volume exists.

Parameters:

Returns:

Usage Example:

if volume_exists "docker_mysql_data"; then
  echo "Volume exists"
fi

check_containers_using_volumes()

Checks if any running Docker containers are currently using any of the target volumes.

Usage:

check_containers_using_volumes

Behavior:


confirm_action(message)

Prompts the user for confirmation before proceeding.

Parameters:

Returns:

Usage Example:

if confirm_action "Do you want to continue?"; then
  echo "Proceeding..."
else
  echo "Cancelled."
fi

perform_backup(backup_folder)

Creates backup archives of the specified Docker volumes.

Parameters:

Behavior:

  1. Checks for any containers using target volumes.

  2. Creates the backup folder if missing.

  3. Iterates over all volumes:

    • Checks if volume exists.

    • Runs an Alpine Docker container to tar and compress the volume contents into a .tar.gz file in the backup folder.

  4. Prints summary of created backup files with sizes.

Usage Example:

perform_backup "my_backup_folder"

perform_restore(backup_folder)

Restores data from backup archives into Docker volumes.

Parameters:

Behavior:

  1. Checks for containers using target volumes.

  2. Validates the backup folder exists.

  3. Checks all required backup files exist in the folder.

  4. Warns if Docker volumes already exist and prompts user to confirm overwrite.

  5. For each volume:

    • Creates the Docker volume if missing.

    • Restores data by extracting the archive into the volume using an Alpine container.

  6. Prints completion message with recommendation to start services.

Usage Example:

perform_restore "my_backup_folder"

Main Script Logic (main() function)

Entry point of the script which:

  1. Checks Docker availability.

  2. Parses command-line arguments for operation (backup or restore) and optional backup folder.

  3. Shows help if arguments are missing or help is requested.

  4. Calls the appropriate function (perform_backup or perform_restore) based on operation.

  5. Exits with error for invalid operations.


Important Implementation Details & Algorithms


Interaction With Other System Components


Usage Examples


Mermaid Flowchart Diagram: Workflow of migration.sh

flowchart TD
    A[Start Script] --> B{Parse Operation}
    B -->|help| C[show_help()]
    B -->|backup| D[perform_backup()]
    B -->|restore| E[perform_restore()]
    B -->|invalid| F[Show Error + show_help()]

    D --> D1[check_docker()]
    D --> D2[check_containers_using_volumes()]
    D --> D3[Create backup folder if missing]
    D --> D4[For each volume]
    D4 --> D4a[Check if volume exists]
    D4a --> D4b[Run alpine container to tar volume]
    D4b --> D4c[Save archive to backup folder]
    D4c --> D5[Print backup summary]

    E --> E1[check_docker()]
    E --> E2[check_containers_using_volumes()]
    E --> E3[Validate backup folder & files]
    E --> E4[Check existing volumes]
    E4 -->|exists| E5[Prompt user to confirm overwrite]
    E --> E6[For each volume]
    E6 --> E6a[Create volume if missing]
    E6a --> E6b[Run alpine container to extract archive]
    E6b --> E7[Print restore summary]

    C --> G[Exit]
    F --> G
    D5 --> G
    E7 --> G

Summary

The migration.sh script is a robust and user-friendly utility for managing RAGFlow persistent data by backing up and restoring Docker volumes safely. It integrates tightly with Docker, enforces safety checks, and guides users through backup and restore workflows with clear instructions and feedback. The modular design with discrete functions enhances maintainability and clarity.


End of documentation for migration.sh