Last active
February 22, 2020 15:10
-
-
Save travislee89/bbc25e5e74f17cfa428c3343c545c9ac to your computer and use it in GitHub Desktop.
下载官方最新的lxc容器镜像
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 下载LCX的最新镜像 | |
download_dir='/var/lib/vz/template/cache' | |
#proxy='socks5://127.0.0.1:8081' | |
if [ -n "$proxy" ]; then | |
export https_proxy="${proxy}" http_proxy="${proxy}" | |
fi | |
log() { | |
#local now=$(date "+%Y-%m-%d %H:%M:%S") | |
printf "\e[35m[%(%Y-%m-%d %H:%M:%S)T]\e[0m $1\e[0m\n"; | |
} | |
download() { | |
local url distribution version architecture latest date file_temp file_target temp status | |
# url="https://us.images.linuxcontainers.org/images/" | |
url="https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/" | |
distribution=$1 | |
version=$2 | |
architecture=${3-amd64} | |
url="${url}${distribution}/${version}/${architecture}/default/" | |
log "Downloading \e[31m${distribution} ${version} ${architecture}" | |
temp=$(curl -s -f --connect-timeout 10 --retry 3 --retry-delay 5 "${url}") | |
status=$? | |
if [ "${status}" -ne 0 ]; then | |
log "\e[31mGet ${distribution} ${version} ${architecture} information FAILED, skip download ${distribution} ${version} ${architecture}." | |
exit 1 | |
fi | |
latest=$(echo -e "${temp}" \ | |
| grep -o -P "a href=\"(\.\/)?20\d{6}_\d\d(:|%3A)\d\d\/\"" \ | |
| grep -o -P "20\d{6}_\d\d(:|%3A)\d\d\/" \ | |
| sed 's/%3A/:/g' \ | |
| tail -1) | |
# 提取之后的数据格式:20180811_07:42/ 或者 20180811_07%3A42/ | |
date=$(echo ${latest} | cut -d"_" -f1) | |
url="${url}${latest}rootfs.tar.xz" | |
file_temp="${download_dir}/${distribution}-${version}_${date}.tar.xz.tmp" | |
file_target="${download_dir}/${distribution}-${version}_${date}.tar.xz" | |
log "URL: \e[32m${url}" | |
log "Temp file: \e[32m${file_temp}" | |
curl -f --connect-timeout 5 --retry 3 --retry-delay 5 --progress-bar \ | |
"${url}" -o "${file_temp}" && mv "${file_temp}" "${file_target}" \ | |
&& (echo; | |
log "Download \e[31m${distribution} ${version} ${architecture}\e[0m FINISH."; | |
log "File: \e[32m${file_target}\n";) \ | |
|| (echo; | |
log "Download \e[31m${distribution} ${version} ${architecture} FAILED."; | |
rm -f "${file_temp}"; | |
log "Remove file: \e[32m${file_temp}\n";) | |
} | |
#download alpine 3.9 | |
#download fedora 30 | |
download debian buster | |
#download debian stretch | |
#download debian jessie | |
#download debian sid | |
#download centos 7 | |
#download gentoo current | |
#download archlinux current | |
#download ubuntu xenial | |
#download ubuntu bionic | |
#download ubuntu cosmic | |
#download openwrt current | |
#download kali current |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment