เป็น shell script ที่จะ backup samba container ไว้ที่ folder data_backup
- สร้าง folder เก็บข้อมูลก่อน โดยคำสั่ง
mkdir -p /Users/Shared/Portable/ad-samba-m/data_backup - สร้างไฟล์
touch data_backup.sh - ใส่เนื้อหา
JavaScript
#!/bin/bash # Configuration with Defaults VOLUME_NAME="${1:-ad-samba-data}" BACKUP_DIR="${2:-./data_backup}" TIMESTAMP=$ (date +"%Y%m%dT%H%M%S") BACKUP_FILE="${BACKUP_DIR}/ad_samba_backup_${TIMESTAMP}.tar.gz" echo "==========================================" echo " Samba AD DC Backup Utility" echo "==========================================" echo "Volume Name: $VOLUME_NAME" echo "Backup Dir : $BACKUP_DIR" echo "Target File: $BACKUP_FILE" echo "------------------------------------------" # Create backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" # 1. Stop the container to ensure data consistency echo "Stopping container..." docker compose stop if [ $? -ne 0 ]; then echo "❌ Failed to stop container. Backup aborted." exit 1 fi # 2. Perform Backup echo "Starting backup..." # Run a temporary alpine container to mount the volume and backup directory, # then compress the volume contents into a tar.gz file. docker run --rm \ -v "$VOLUME_NAME":/source_volume \ -v "$ (pwd) /$BACKUP_DIR":/backup_dir \ alpine \ tar czf "/backup_dir/ad_samba_backup_${TIMESTAMP}.tar.gz" -C /source_volume . BACKUP_STATUS=$? # 3. Start the container again echo "Starting container..." docker compose start # 4. Check Result if [ $BACKUP_STATUS -eq 0 ]; then echo "✅ Backup successful: $BACKUP_FILE" # List the created file to show size ls -lh "$BACKUP_FILE" else echo "❌ Backup failed!" exit 1 fi