Advertisement
rockdrilla

debmirror

Sep 22nd, 2016
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.25 KB | None | 0 0
  1. #!/bin/bash
  2. mirror_root=/var/www/debmirror
  3. exclude_regex="(\?|installer|udeb|changelog|\.(changes|diff)|-(updates|backports)|by-hash/(md5sum|sha1)|arm(64|el|hf)|mips(64|64el|el)?|powerpc|ppc64el|s390x)"
  4.  
  5. if [ $# -eq 0 ]; then while read name url; do [ -z "${name}" ] && continue; [ -z "${url}" ] && continue; "$0" "${name}" "${url}"; done <<-EOF
  6.  
  7.     kali    http://http.kali.org/kali
  8.     debian  http://ftp.debian.org/debian
  9.  
  10. EOF
  11. exit 0; fi
  12.  
  13. if [ $# -eq 2 ]; then screen -d -m -S "debmirror-$1" "$0" "$1" "$2" "!"; exit 0; fi
  14.  
  15. do_mirror() {
  16.     [ -z "$1" ] && return; [ -z "$2" ] && return
  17.  
  18.     local repo_root="${mirror_root}/$1"
  19.     if ! readlink -m "${repo_root}/" | egrep -qe "^${mirror_root}/"; then
  20.         exit 1
  21.     fi
  22.  
  23.     exclude_regex=$(echo "${exclude_regex}" | sed -re "s#([a-zA-Z])#\[\u\1\l\1\]#g")
  24.  
  25.     local tmp_dir=$(mktemp -d --tmpdir="${mirror_root}")
  26.     cd "${tmp_dir}/"
  27.  
  28.     ### clone mirror in temporary directory
  29.     printf "name: %s\n" "$1" >> stats
  30.     printf "uri: %s\n" "$2" >> stats
  31.  
  32.     local repo_ts=$(date -u -R)
  33.     printf "timestamp: %s\n" "${repo_ts}" >> stats
  34.  
  35.     local wget_depth=$(echo "${2}" | sed -re "s#/+\$##g;s#^[^:]+:/*[^/]+(/.*)?\$#\1#" | tr -cd '/' | wc -c)
  36.  
  37.     local wget_start=$(date "+%s")
  38.     wget --quiet --inet4-only --prefer-family=IPv4 --recursive --no-host-directories --no-parent --level=10 --cut-dirs=${wget_depth} --regex-type pcre --reject-regex "${exclude_regex}" "$2/dists/"
  39.     local wget_end=$(date "+%s")
  40.     sleep 1
  41.  
  42.     local wget_seconds=$[wget_end - wget_start]
  43.     local wget_time=$(date -u -d "@${wget_seconds}" "+%H:%M:%S")
  44.     printf "wget time: %s (%s seconds)\n" "${wget_time}" ${wget_seconds} >> stats
  45.  
  46.     ### cleanup and re-create destination folder
  47.     ### move files from temporary folder to destination folder
  48.     ### remove temporary folder
  49.     if [ -e "${repo_root}" ] ; then
  50.         rm -rf "${repo_root}/"
  51.     fi
  52.     mkdir -p "${repo_root}/"
  53.     mv * "${repo_root}/"
  54.     cd "${repo_root}/"
  55.     rm -rf "${tmp_dir}/"
  56.  
  57.     ### force hardlinks in repo
  58.     local repo_size_before=$(du -d0 "${repo_root}/" | egrep -oe "^[0-9]+")
  59.     printf "mirror size (as is): %s\n" ${repo_size_before} >> stats
  60.  
  61.     local t0=$(mktemp); local t1=$(mktemp)
  62.     local merge_start=$(date "+%s")
  63.     find "${repo_root}/" -mindepth 1 -type f -exec sha256sum -b {} + | sed -re "s#^([0-9a-fA-F]+) +\*?([^ ].*)\$#\1 \2#" > "${t0}"
  64.     cut -d " " -f1 < "${t0}" | sort | uniq > "${t1}"
  65.     while read hsum; do
  66.         local src_file=""
  67.         egrep -e "^${hsum} " "${t0}" | while read tsum tfile; do
  68.             if [ -z "${src_file}" ]; then src_file=${tfile}; continue; fi
  69.             rm "${tfile}"; ln "${src_file}" "${tfile}"
  70.         done
  71.     done < "${t1}"
  72.     local merge_end=$(date "+%s")
  73.  
  74.     rm "${t0}" "${t1}"; sleep 1
  75.  
  76.     local repo_size_after=$(du -d0 "${repo_root}/" | egrep -oe "^[0-9]+")
  77.     printf "mirror size (merged): %s\n" ${repo_size_after} >> stats
  78.  
  79.     local merge_seconds=$[merge_end - merge_start]
  80.     local merge_time=$(date -u -d "@${merge_seconds}" "+%H:%M:%S")
  81.     printf "merge time: %s (%s seconds)\n" "${merge_time}" ${merge_seconds} >> stats
  82.  
  83.     ### reset timestamps in destination folder
  84.     find "${repo_root}/" -exec touch {} +
  85. }
  86.  
  87. do_mirror "$@"
  88. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement