nginx_dir="/data/nginx"
mvn_idx_dir="$nginx_dir/maven2/.index"
mvn_idx_dl_dir="$nginx_dir/maven2/.CacheIndex"
mvn_idx_name="nexus-maven-repository-index"
mvn_idx_path_name="$mvn_idx_dir/$mvn_idx_name"
mvn_idx_gz_name="$mvn_idx_dir/$mvn_idx_name.gz"
mvn_idx_prop_name="$mvn_idx_dir/$mvn_idx_name.properties"
mvn_idx_dl_gz_name="$mvn_idx_dl_dir/$mvn_idx_name.gz"
mvn_idx_dl_prop_name="$mvn_idx_dl_dir/$mvn_idx_name.properties"
log_file="/data/nginx/maven_index_log.txt"
dt_fmt="`date "+%Y-%m-%d %H:%M:%S"`"
if [ ! -d $mvn_idx_dl_dir ]; then
mkdir -p $mvn_idx_dl_dir
fi
if [ ! -d $mvn_idx_dir ]; then
mkdir -p $mvn_idx_dir
fi
if [ ! -d $mvn_idx_dl_dir ]; then
echo "$dt_fmt mkdir $mvn_idx_dl_dir failed !" >> $log_file
exit -1
fi
if [ ! -d $mvn_idx_dir ]; then
echo "$dt_fmt mkdir $mvn_idx_dir failed !" >> $log_file
exit -1
fi
cd $mvn_idx_dl_dir
dl_idx="true"
dl_prop="true"
#echo mvn_idx_gz_name="$mvn_idx_gz_name"
#先检查是不是需要下载文件,然后再下载文件,减少不必要的网络请求数据
if [ -f "$mvn_idx_gz_name" ]; then
remote_idx_md5=$(curl -s https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.gz.md5)
if [ "${#remote_idx_md5}" != "32" ] ; then
echo "$dt_fmt download $mvn_idx_gz_name.md5 failed !" >> $log_file
exit -1
fi
local_idx_md5=$(md5sum $mvn_idx_gz_name | cut -b 1-32)
#echo remote_idx_md5="$remote_idx_md5" local_idx_md5="$local_idx_md5"
if [ "$remote_idx_md5" = "$local_idx_md5" ] ; then
dl_idx="false"
if [ -f "$mvn_idx_prop_name" ]; then
remote_prop_md5=$(curl -s https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.properties.md5)
if [ "${#remote_prop_md5}" != "32" ] ; then
echo "$dt_fmt download $mvn_idx_prop_name failed !" >> $log_file
exit -1
fi
local_prop_md5=$(md5sum $mvn_idx_prop_name | cut -b 1-32)
if [ "$remote_prop_md5" = "$local_prop_md5" ] ; then
dl_prop="false"
echo "$dt_fmt check file success ,no need to update !" >> $log_file
exit 0
fi
fi
fi
fi
if [ "$dl_idx" = "true" ] ; then
aria2c -c https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.gz
aria2c -c https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.gz.md5
aria2c -c https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.gz.sha1
fi
if [ "$dl_prop" = "true" ] ; then
aria2c -c https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.properties
aria2c -c https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.properties.md5
aria2c -c https://repo1.maven.org/maven2/.index/nexus-maven-repository-index.properties.sha1
fi
#校验下载到的文件
if [ "$dl_idx" = "true" ] ; then
dl_idx_md5=$(md5sum $mvn_idx_dl_gz_name | cut -b 1-32)
dl_idx_f_md5=$(cat $mvn_idx_dl_gz_name.md5)
if [ "$dl_idx_md5" = "$dl_idx_f_md5" ] ; then
rm -rf $mvn_idx_gz_name
mv $mvn_idx_dl_gz_name $mvn_idx_gz_name
rm -rf $mvn_idx_gz_name.md5
mv $mvn_idx_dl_gz_name.md5 $mvn_idx_gz_name.md5
rm -rf $mvn_idx_gz_name.sha1
mv $mvn_idx_dl_gz_name.sha1 $mvn_idx_gz_name.sha1
else
echo "$dt_fmt check downloaded index file failed !" >> $log_file
cd $nginx_dir
rm -rf $mvn_idx_dl_dir
fi
fi
if [ "$dl_prop" = "true" ] ; then
dl_prop_md5=$(md5sum $mvn_idx_dl_prop_name | cut -b 1-32)
dl_prop_f_md5=$(cat $mvn_idx_dl_prop_name.md5)
if [ "$dl_prop_md5" = "$dl_prop_f_md5" ] ; then
rm -rf $mvn_idx_prop_name
mv $mvn_idx_dl_prop_name $mvn_idx_prop_name
rm -rf $mvn_idx_prop_name.md5
mv $mvn_idx_dl_prop_name.md5 $mvn_idx_prop_name.md5
rm -rf $mvn_idx_prop_name.sha1
mv $mvn_idx_dl_prop_name.sha1 $mvn_idx_prop_name.sha1
else
echo "$dt_fmt check downloaded properties file failed !" >> $log_file
cd $nginx_dir
rm -rf $mvn_idx_dl_dir
fi
fi