#!/bin/bash # # This Bash script is designed to transfer files from a bind-mounted directory on the host machine to a Docker volume # using the local driver. It ensures that the operation is performed with root privileges and provides # detailed feedback during execution. # # Purpose # # The script is useful in scenarios where you need to migrate or copy files from a # local directory (bind-mounted volume) to a Docker volume. This is particularly helpful when: # # You want to back up files from a bind-mounted volume to a Docker volume. # You need to migrate data from a local directory to a Docker volume for containerized applications. # You want to ensure that files retain their permissions and ownership during the transfer. # # # # Ensure the script is run as root if [ "$EUID" -ne 0 ]; then echo "This script must be run as root. Please use sudo or run as root." exit 1 fi # Source directory (bind-mounted volume) SOURCE_DIR="/media/orbitzs/nextcloud_files/apex/storage/data" # Target Docker volume (local driver) TARGET_VOLUME="nc3_files" # Create the target volume if it doesn't exist if ! docker volume inspect "$TARGET_VOLUME" &> /dev/null; then echo "Creating Docker volume: $TARGET_VOLUME" docker volume create --driver local "$TARGET_VOLUME" fi # Temporary container to copy files into the target volume TEMP_CONTAINER="temp_container_$(date +%s)" # Start a temporary container with the target volume mounted docker run --rm -d \ --name "$TEMP_CONTAINER" \ -v "$TARGET_VOLUME:/target" \ alpine tail -f /dev/null # Copy files from the source directory to the target volume echo "Copying files from $SOURCE_DIR to $TARGET_VOLUME..." docker cp "$SOURCE_DIR/." "$TEMP_CONTAINER:/target" # Stop the temporary container docker stop "$TEMP_CONTAINER" echo "Files have been successfully transferred to the Docker volume: $TARGET_VOLUME"