AWS清理缓存脚本

144 人阅读
分类:

运行前请先安装aws cli,然后认证成功后才能执行该脚本,该脚本是根据回源域名或者IP来清理缓存的

#!/bin/bash
tmplist=()
i=0
strb='\.'

Print_Usage() {
    echo "说明,该脚本需要传递一个回源的域名或者IP,比如删除xxxxx.com这个源站cdn缓存,默认删除/*"
    echo "sh $0 xxxxx.com"
    exit
}

#需要传递要清理缓存的回源域名或者IP
clean_aws() {
for n in `cat tmp_aws_map`
do
	#将读取的文件保存到tmplist数组中
	tmplist[$i]=$n
	#如果找到了需要清理的域名,则开始找该域名对应的ID
	if [[ ${tmplist[$i]} == $1 ]];then
		let "j=$i-1"
		while true
		do
			#如果该回源域名前面还是个域名,这继续找
			if [[ ${tmplist[$j]} =~ $strb ]];then
				((j--))
				continue	
			#如果该域名前面不是域名了,这表明就是对应的ID
			else
				aws cloudfront create-invalidation --distribution-id ${tmplist[$j]} --paths "/*" > /dev/null 2>&1
				break
			fi
		done
	fi
	((i++))
done
}

#判断输入的参数数量否合理,不合理打印用法
if [ $# -ne 1 ];then
    echo -e "\nError,You should input a correct Parameter"  
    Print_Usage
fi
 
if [ "$1" == "help" ];then
    Print_Usage
fi

#获取所有的分配信息,并且过滤出源站域名和分配的ID,写到tmp_aws_map文件中去
aws cloudfront list-distributions |grep '"Id"' |awk '{print $2}' | awk -F'"' '{print $2}'> tmp_aws_map
clean_aws $1


和博主交个朋友吧
    发布篇幅
    • 文章总数:0
    • 原创:0
    • 转载:0
    • 译文:0
    文章分类
      文章存档
      阅读排行