#! /usr/bin/env bash # Mirror repositories. For example: # # mrrepo -s git.example.org /var/scm # # Will first download (via http or https if -s specified) the manifest file # from git.example.org which should list all publicly available repositories. # It will then mirror each repository in /var/scm using the git protocol. # # -v # Run verbose. # # -s # Use https rather than http to download the manifest (git protocol is still # used for mirroring). # # Notes: # - needs curl # - run from cron as user scm (which belongs to the group scm). # # To test, run: # # runuser -u scm -- /var/scm/mrrepo -s -v git.example.org /var/scm # usage="usage: $0 [-v] [-s] " owd="$(pwd)" trap "{ cd '$owd'; exit 1; }" ERR set -o errtrace # Trap in functions. function info () { echo "$*" 1>&2; } function error () { info "$*"; exit 1; } prot="http" host= path= verb=0 while [ "$#" -gt 0 ]; do case "$1" in -v) verb=1 shift ;; -s) prot="https" shift ;; *) if [ -z "$host" ]; then host="$1" elif [ -z "$path" ]; then path="${1%/}" else error "$usage" fi shift ;; esac done if [ -z "$host" -o -z "$path" ]; then error "$usage" fi if [ ! -d "$path" ]; then error "$path is not a directory" fi cd "$path" curl_ops=() curl_ops+=(-f) # Fail on HTTP errors. curl_ops+=(--max-time 30) # Finish in 30 seconds. if [ "$verb" -ge 1 ]; then curl_ops+=(--progress-bar) else curl_ops+=(-s -S) # Silent but show errors. fi function fetch () # [] { local u="$1"; shift if [ "$verb" -ge 1 ]; then info curl "${curl_ops[@]}" "$@" "$u" fi curl "${curl_ops[@]}" "$@" "$u" } fetch "$prot://$host/manifest" -z manifest -o manifest new=() while read r || [ -n "$r" ]; do new+=("$r") done