#!/usr/bin/env bash ## Last Updated: 2022-04-25 # ## Info: Download all pre-built iso from Debian ## (via wget) # ## Source: https://gitlab.com/-/snippets/2132933 # ## Quit on error #set -e ## Quit if any command in the chain errors #set -o pipefail ## Variable has to be defined set -u # ----------------------------------------------------------------------------- ## get_deb_ver [archive|release] function get_deb_ver() { curl -s http://cdimage.debian.org/cdimage/$1/ \ | awk -F '"' '/href/ {print $6}' \ | grep -v '-' \ | grep -v '_' \ | grep -v '?' \ | grep -v 'live/' \ | grep -v 'current/' \ | grep -v 'project' \ | grep '/$' \ | grep -v '^/' \ | sed 's_/__g' \ | sort -u \ | sort -t. -k 1,1n -k 2,2n -k 3,3n } function print_status() { printf '%-13s:' "${ver}/${arch}" } ## download_iso [archive|release] $ver $arch function download_iso(){ major=$( printf "%02d" "$( echo ${ver} | cut -d '.' -f1 )" ) status=$( curl \ --silent \ --fail \ --location \ "http://cdimage.debian.org/cdimage/$1/${ver}/${arch}/iso-cd/" ) [ $? != "0" ] \ && echo "[-] $( print_status ) Not getting HTTP 200 ~ http://cdimage.debian.org/cdimage/$1/${ver}/${arch}/iso-cd/" >&2 \ && return iso=$( curl --silent "http://cdimage.debian.org/cdimage/$1/${ver}/${arch}/iso-cd/" \ | awk -F '"' '/href/ {print $6}' \ | grep 'debian-[0-9]' \ | grep -v businesscard ) if [ "$( echo "${iso}" | wc -l )" -gt 1 ]; then iso=$( echo "${iso}" \ | grep CD-1.iso \ | head -n 1 ) fi [ -z "${iso}" ] \ && echo "[-] $( print_status ) Missing pre-built iso-cd (will need to use jigdo) ~ http://cdimage.debian.org/cdimage/$1/${ver}/${arch}/iso-cd/" >&2 \ && return new_iso=$( echo "${iso}" | sed 's_debian-[0-9]*-'${arch}'_debian-'${ver}'-'${arch}'_' ) echo -n "[i] $( print_status ) Downloading ${iso}" if [ "${iso}" != "$new_iso" ]; then echo " -> ${new_iso}" else echo "" fi mkdir -p "Debian-${major}/" wget \ --continue \ --no-verbose \ -O "Debian-${major}/${new_iso}" \ "http://cdimage.debian.org/cdimage/$1/${ver}/${arch}/iso-cd/${iso}" \ || echo -e "[-] Issue with wget\n" >&2 } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Check if root (due to NAS) if [ "$(id -u)" -eq 0 ]; then echo "[-] You are root. Please switch to non-root" >&2 exit 1 fi ## Move to script folder cd "$( dirname $BASH_SOURCE )/" script_path="$( readlink -f "$( readlink "${BASH_SOURCE[0]}" )" || true)" cd "$( dirname "${script_path}" )/" pwd ## Timestamp date -u > last.txt chmod 0644 last.txt ## Fresh start, delete any incorrect files find . \ -type f \ -name '[a-z]*.iso' \ -empty \ -print \ -delete \ 2>/dev/null \ || true echo -e "\n[i] Doing archived images ~ http://cdimage.debian.org/cdimage/archive/" for ver in $( get_deb_ver archive ); do for arch in amd64 i386; do download_iso archive done done # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - echo -e "\n[i] Doing current release images ~ http://cdimage.debian.org/cdimage/release/" for ver in $( get_deb_ver release ); do for arch in amd64 i386; do download_iso release done done echo "" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Generate hashes rm -f {MD5,SHA1,SHA256}SUM echo "[i] Generating hashes" find . \ -type f \ -name '*.iso' \ 2>/dev/null \ | sort -h \ | while read -r x; do echo "${x}" md5sum "${x}" >> MD5SUM; sha1sum "${x}" >> SHA1SUM; sha256sum "${x}" >> SHA256SUM; done echo "" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Timestamp date -u >> last.txt # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Done echo "[i] Done" exit