การนำเข้าข้อมูล Samba ผ่าน Docker เป็นกระบวนการที่จำเป็นสำหรับการจัดการและดูแลระบบ Samba บน Docker. ในบทความนี้ เราจะแนะนำวิธีการนำเข้าข้อมูล Samba ผ่าน Docker โดยใช้ Shell Script.
สร้าง Shell Script สำหรับการนำเข้าข้อมูล Samba
ขั้นตอนแรกคือสร้างไฟล์ Shell Script สำหรับการนำเข้าข้อมูล Samba. ใช้คำสั่งต่อไปนี้เพื่อสร้างไฟล์:
SQL
touch data_import.sh
จากนั้น ใส่เนื้อหาลงในไฟล์ data_import.sh ดังนี้:
SQL
#!/bin/bash
# Configuration with Defaults
VOLUME_NAME="${1:-ad-samba-data}"
IMPORT_DIR="${2:-./data_import}"
echo "=========================================="
echo " Samba AD DC Import Utility"
echo "=========================================="
echo "Volume Name: $VOLUME_NAME"
echo "Import Dir : $IMPORT_DIR"
echo "------------------------------------------"
# List available import files
echo "Available Import Files:"
ls -1 "$IMPORT_DIR"/*.tar.gz 2>/dev/null
if [ $? -ne 0 ]; then
echo "❌ No import files found in $IMPORT_DIR"
exit 1
fi
echo "------------------------------------------"
# Ask user for import file to restore
read -p "Enter import filename to import (e.g., ad_samba_import_2026...tar.gz): " IMPORT_FILENAME
FULL_IMPORT_PATH="${IMPORT_DIR}/${IMPORT_FILENAME}"
if [ ! -f "$FULL_IMPORT_PATH" ]; then
echo "❌ Error: Import file not found: $FULL_IMPORT_PATH"
exit 1
fi
echo "⚠️ WARNING: This will OVERWRITE existing data in volume '$VOLUME_NAME'!"
read -p "Are you sure you want to continue? (y/N): " CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "Import cancelled."
exit 0
fi
echo "Importing from: $FULL_IMPORT_PATH"
# 1. Stop the container first
echo "Stopping container..."
docker compose stop
if [ $? -ne 0 ]; then
echo "❌ Failed to stop container. Import aborted."
exit 1
fi
# 2. Import using a temporary alpine container
# - Mount the volume to /target_volume
# - Mount the import directory to /import_dir
# - Wipe the volume
# - Extract the import file
echo "Wiping volume and extracting import..."
docker run --rm
-v "$VOLUME_NAME":/target_volume
-v "$(pwd)/$IMPORT_DIR":/import_dir
alpine
sh -c "rm -rf /target_volume/* && tar xzf /import_dir/$IMPORT_FILENAME -C /target_volume"
IMPORT_STATUS=$?
# 3. Start the container again
echo "Starting container..."
docker compose start
# 4. Check Result
if [ $IMPORT_STATUS -eq 0 ]; then
echo "✅ Import successful."
else
echo "❌ Import failed!"
exit 1
fi
ให้สิทธิ์ให้กับไฟล์ Shell Script ด้วยคำสั่ง:
SQL
chmod +x data_import.sh
การนำเข้าข้อมูล Samba
สามารถทำได้ง่าย ๆ โดยคำสั่ง:
SQL
./data_import.sh
บทสรุป
บทความนี้ได้แนะนำวิธีการนำเข้าข้อมูล Samba ผ่าน Docker โดยใช้ Shell Script. กระบวนการนี้ประกอบด้วยการสร้างไฟล์ Shell Script สำหรับการนำเข้าข้อมูล Samba และการให้สิทธิ์ให้กับไฟล์ Shell Script. ผ่านนี้ คุณจะสามารถนำเข้าข้อมูล Samba ได้อย่างง่ายและมีประสิทธิภาพ.