对象存储 - 同步数据
1. S3不同主账号+AK同步
通过AK从源s3路径同步数据到本地, 再从本地上传到指定目的s3路径
#!/bin/bash# 有异常立即退出
set -e# 设定aws的profile, 切换AK读写S3
set_aws_ai_profile() {echo -e "\n --------------开始aws profile配置------------- \n"aws configure set aws_access_key_id AK --profile ai_profileaws configure set aws_secret_access_key SK --profile ai_profileaws configure set region ap-south-1 --profile ai_profileaws configure set output json --profile ai_profileaws configure list --profile ai_profileecho -e "\n --------------结束aws profile配置------------- \n"
}# 封装: 检查 S3 目录大小是否超过指定阈值
# 参数: 1 = S3 路径 (必填), 2 = 阈值(字节,可选,默认10G)
check_s3_size_exceed() {echo -e "\n --------------开始检测大小是否超限------------- \n"local S3_PATH=$1local THRESHOLD=${2:-$((10 * 1024 * 1024 * 1024))} # 默认10Gif [ -z "$S3_PATH" ]; thenecho "请提供 S3 路径"return 2fi# 获取总大小 (字节)local TOTAL_SIZETOTAL_SIZE=$(aws s3 ls "$S3_PATH" --recursive --summarize --profile ai_profile | grep "Total Size" | awk '{print $3}')echo -e "\n 检查目录: $S3_PATH"echo "总大小 : $TOTAL_SIZE bytes"echo "阈值 : $THRESHOLD bytes"# 判断是否超过if [ "$TOTAL_SIZE" -gt "$THRESHOLD" ]; thenecho -e "超过阈值!\n"return 1 # 异常退出elseecho -e "未超过阈值\n"return 0 # 正常fiecho -e "\n --------------结束检测大小是否超限------------- \n"
}# 跨账号同步数据
cross_account_sync() {echo -e "\n --------------开始同步------------- \n"local source_path=$1local target_path=$2local temp_dir=/tmp/shy_tmp_synce/# 清除历史数据## 清空本地的if [ -d "$temp_dir" ]; thenrm -rf "$temp_dir"echo "已删除目录: $temp_dir"elseecho "目录不存在: $temp_dir"fi## 清空s3的aws s3 rm $target_path --recursive# 从源下载数据到本地aws s3 cp \--profile ai_profile \--recursive \$source_path \$temp_dir# 从本地上传数据到目的s3aws s3 cp \--recursive \$temp_dir \$target_path# 清空本地数据rm -rf "$temp_dir"echo -e "\n --------------结束同步------------- \n"
}# 设定profile
set_aws_ai_profile# 设定源路径与目标路径
s3_source_base_path="s3://bucket_aa/11"
s3_target_base_path="s3://bucket_bb/22"# 检查 10G 阈值, 避免本地磁盘打满
check_s3_size_exceed "$s3_source_base_path/p_date=${dt_1}/p_insert_day=${ds_1}/"# 同步数据, 注意7天回溯
p_dates="${dt_1} ${dt_2} ${dt_3} ${dt_4} ${dt_5} ${dt_6} ${dt_7}"
for p_date in $p_dates;
dosuffix="p_date=$p_date/p_insert_day=${ds_1}/"command_str="cross_account_sync $s3_source_base_path/$suffix $s3_target_base_path/$suffix"echo -e "\n $command_str \n"$command_str
done