Shell脚本如何测试云存储可用性

wen 实用脚本 1

本文目录导读:

Shell脚本如何测试云存储可用性

  1. HTTP/HTTPS 端点测试
  2. AWS S3 兼容存储测试
  3. FTP/SFTP 存储测试
  4. 综合可用性测试脚本
  5. 高级测试:性能监控
  6. 使用建议

HTTP/HTTPS 端点测试

基础可用性测试

#!/bin/bash
# 云存储服务端点
STORAGE_URL="https://your-storage-endpoint.com"
TIMEOUT=10
# 测试函数
test_endpoint() {
    local url=$1
    local response=$(curl -s -o /dev/null -w "%{http_code}" --max-time $TIMEOUT $url)
    if [ "$response" -eq 200 ] || [ "$response" -eq 403 ] || [ "$response" -eq 401 ]; then
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] 存储服务可用 - HTTP $response"
        return 0
    else
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] 存储服务不可用 - HTTP $response"
        return 1
    fi
}
# 执行测试
test_endpoint "$STORAGE_URL"
exit $?

AWS S3 兼容存储测试

检查桶是否存在并可访问

#!/bin/bash
# AWS S3 配置
AWS_ENDPOINT="https://s3.amazonaws.com"
AWS_BUCKET="your-bucket-name"
AWS_ACCESS_KEY="your-access-key"
AWS_SECRET_KEY="your-secret-key"
test_s3_bucket() {
    echo "测试 S3 桶可用性..."
    # 列出桶内容
    response=$(curl -s -o /dev/null -w "%{http_code}" \
        --aws-sigv4 "aws:amz:us-east-1:s3" \
        --user "$AWS_ACCESS_KEY:$AWS_SECRET_KEY" \
        "$AWS_ENDPOINT/$AWS_BUCKET/" \
        --max-time 10)
    if [ "$response" -eq 200 ]; then
        echo "✅ S3 桶可正常访问"
        return 0
    else
        echo "❌ S3 桶访问失败 (HTTP $response)"
        return 1
    fi
}
# 测试上传/下载功能
test_s3_operations() {
    local test_file="/tmp/s3_test_$(date +%s).txt"
    echo "S3 可用性测试文件" > "$test_file"
    # 上传测试
    echo "测试上传..."
    aws s3 cp "$test_file" "s3://$AWS_BUCKET/test/" --endpoint-url $AWS_ENDPOINT
    upload_status=$?
    # 下载测试
    echo "测试下载..."
    aws s3 cp "s3://$AWS_BUCKET/test/$(basename $test_file)" "/tmp/s3_download_test.txt" --endpoint-url $AWS_ENDPOINT
    download_status=$?
    # 清理
    aws s3 rm "s3://$AWS_BUCKET/test/$(basename $test_file)" --endpoint-url $AWS_ENDPOINT
    rm -f "$test_file" "/tmp/s3_download_test.txt"
    if [ $upload_status -eq 0 ] && [ $download_status -eq 0 ]; then
        echo "✅ S3 上传/下载功能正常"
        return 0
    else
        echo "❌ S3 操作失败"
        return 1
    fi
}
test_s3_bucket
test_s3_operations

FTP/SFTP 存储测试

#!/bin/bash
# FTP 配置
FTP_HOST="ftp.your-storage.com"
FTP_USER="username"
FTP_PASS="password"
FTP_PORT=21
test_ftp_connection() {
    echo "测试 FTP 连接..."
    # 使用 lftp 测试连接
    lftp -c "open -u $FTP_USER,$FTP_PASS -p $FTP_PORT $FTP_HOST; ls" --timeout 30
    if [ $? -eq 0 ]; then
        echo "✅ FTP 连接正常"
        return 0
    else
        echo "❌ FTP 连接失败"
        return 1
    fi
}
# SFTP 测试(更安全)
test_sftp_connection() {
    echo "测试 SFTP 连接..."
    # 尝试连接并列出文件
    sftp -o StrictHostKeyChecking=no -o ConnectTimeout=10 \
        -b - $FTP_USER@$FTP_HOST <<< "ls" 2>/dev/null
    if [ $? -eq 0 ]; then
        echo "✅ SFTP 连接正常"
        return 0
    else
        echo "❌ SFTP 连接失败"
        return 1
    fi
}
test_ftp_connection
test_sftp_connection

综合可用性测试脚本

#!/bin/bash
# 云存储配置
declare -A STORAGE_ENDPOINTS
STORAGE_ENDPOINTS=(
    ["AWS-S3"]="https://s3.amazonaws.com"
    ["Azure-Blob"]="https://yourstorage.blob.core.windows.net"
    ["GCP-Storage"]="https://storage.googleapis.com"
    ["MinIO"]="http://localhost:9000"
)
# 测试结果数组
declare -A TEST_RESULTS
# 测试函数
test_storage_availability() {
    local name=$1
    local url=$2
    local timeout=15
    echo "测试 $name ($url)..."
    # 1. DNS 解析测试
    echo -n "  DNS 解析: "
    local domain=$(echo $url | awk -F/ '{print $3}')
    if host $domain &>/dev/null; then
        echo "✅ 成功"
    else
        echo "❌ 失败"
        return 1
    fi
    # 2. 网络可达性测试
    echo -n "  网络可达性: "
    if ping -c 2 -W 5 $domain &>/dev/null; then
        echo "✅ 成功"
    else
        echo "❌ 失败"
        return 1
    fi
    # 3. HTTP 响应测试
    echo -n "  HTTP 响应: "
    local http_code=$(curl -s -o /dev/null -w "%{http_code}" \
        --max-time $timeout \
        --connect-timeout 10 \
        "$url" 2>/dev/null)
    if [ "$http_code" -ge 200 ] && [ "$http_code" -lt 500 ]; then
        echo "✅ HTTP $http_code"
        TEST_RESULTS["$name"]="可用"
        return 0
    else
        echo "❌ HTTP $http_code"
        TEST_RESULTS["$name"]="不可用"
        return 1
    fi
}
# 测试所有端点
echo "===== 云存储可用性测试 ====="
echo "开始时间: $(date)"
echo "=========================="
for storage_name in "${!STORAGE_ENDPOINTS[@]}"; do
    test_storage_availability "$storage_name" "${STORAGE_ENDPOINTS[$storage_name]}"
    echo "---"
done
# 输出测试报告
echo ""
echo "===== 测试报告 ====="
echo "存储服务 | 状态"
echo "---------|------"
for storage_name in "${!TEST_RESULTS[@]}"; do
    echo "$storage_name | ${TEST_RESULTS[$storage_name]}"
done
# 检查是否有服务不可用
ALL_AVAILABLE=true
for result in "${TEST_RESULTS[@]}"; do
    if [ "$result" != "可用" ]; then
        ALL_AVAILABLE=false
        break
    fi
done
if [ "$ALL_AVAILABLE" = true ]; then
    echo "✅ 所有存储服务正常"
else
    echo "⚠️ 部分存储服务异常"
fi

高级测试:性能监控

#!/bin/bash
# 性能测试配置
STORAGE_URL="https://your-storage.com"
TEST_FILE_SIZE_MB=10
CONCURRENT_CONNECTIONS=5
# 测试下载速度
test_download_speed() {
    local test_file="speedtest_${TEST_FILE_SIZE_MB}MB.dat"
    echo "测试下载速度..."
    # 创建测试文件(需要服务端支持)
    # 或使用已存在的测试文件
    local start_time=$(date +%s.%N)
    curl -s -o /dev/null "$STORAGE_URL/$test_file" --max-time 60
    local end_time=$(date +%s.%N)
    local duration=$(echo "$end_time - $start_time" | bc)
    local speed=$(echo "scale=2; $TEST_FILE_SIZE_MB * 8 / $duration" | bc)
    echo "下载速度: ${speed} Mbps"
    echo "耗时: ${duration} 秒"
}
# 测试并发连接
test_concurrent_connections() {
    echo "测试 $CONCURRENT_CONNECTIONS 并发连接..."
    for i in $(seq 1 $CONCURRENT_CONNECTIONS); do
        curl -s -o /dev/null "$STORAGE_URL/small_file_${i}.txt" --max-time 30 &
    done
    wait
    echo "并发测试完成"
}
test_download_speed
test_concurrent_connections

使用建议

  1. 错误处理:添加重试机制和超时处理
  2. 日志记录:记录测试结果到文件
  3. 告警系统:集成邮件或 Slack 通知
  4. 定期执行:使用 cron 定期运行测试
  5. 多区域测试:从不同地理位置测试

这些脚本可以根据你的具体云存储服务进行调整,建议先在小规模环境中测试后再投入生产使用。

抱歉,评论功能暂时关闭!