bind2local.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. #
  3. # This Bash script is designed to transfer files from a bind-mounted directory on the host machine to a Docker volume
  4. # using the local driver. It ensures that the operation is performed with root privileges and provides
  5. # detailed feedback during execution.
  6. #
  7. # Purpose
  8. #
  9. # The script is useful in scenarios where you need to migrate or copy files from a
  10. # local directory (bind-mounted volume) to a Docker volume. This is particularly helpful when:
  11. #
  12. # You want to back up files from a bind-mounted volume to a Docker volume.
  13. # You need to migrate data from a local directory to a Docker volume for containerized applications.
  14. # You want to ensure that files retain their permissions and ownership during the transfer.
  15. #
  16. #
  17. #
  18. # Ensure the script is run as root
  19. if [ "$EUID" -ne 0 ]; then
  20. echo "This script must be run as root. Please use sudo or run as root."
  21. exit 1
  22. fi
  23. # Source directory (bind-mounted volume)
  24. #SOURCE_DIR="./files/program_data"
  25. #SOURCE_DIR="./files/cache"
  26. SOURCE_DIR="./files/log"
  27. # Target Docker volume (local driver)
  28. #TARGET_VOLUME="jellyfin_"
  29. #TARGET_VOLUME="jellyfin_cache"
  30. TARGET_VOLUME="jellyfin_log"
  31. # Create the target volume if it doesn't exist
  32. if ! docker volume inspect "$TARGET_VOLUME" &> /dev/null; then
  33. echo "Creating Docker volume: $TARGET_VOLUME"
  34. docker volume create --driver local "$TARGET_VOLUME"
  35. fi
  36. # Temporary container to copy files into the target volume
  37. TEMP_CONTAINER="temp_container_$(date +%s)"
  38. # Start a temporary container with the target volume mounted
  39. docker run --rm -d \
  40. --name "$TEMP_CONTAINER" \
  41. -v "$TARGET_VOLUME:/target" \
  42. alpine tail -f /dev/null
  43. # Copy files from the source directory to the target volume
  44. echo "Copying files from $SOURCE_DIR to $TARGET_VOLUME..."
  45. docker cp "$SOURCE_DIR/." "$TEMP_CONTAINER:/target"
  46. # Stop the temporary container
  47. docker stop "$TEMP_CONTAINER"
  48. echo "Files have been successfully transferred to the Docker volume: $TARGET_VOLUME"