merge both scripts - simplify fedora-mirror.sh to the bone
[build.git] / fedora-mirror.sh
1 #!/bin/bash
2 # this can help you create/update your fedora mirror
3
4 COMMAND=$(basename $0)
5 LOGDIR=/var/log/fedora-mirror
6 DATE=$(date '+%Y-%m-%d-%H-%M')
7 LOG=${LOGDIR}/${DATE}.log
8
9 dry_run=
10 verbose=--verbose
11 log=
12 skip_core=true
13 root=/mirror/
14
15
16 us_fedora_url=rsync://mirrors.kernel.org/fedora
17 eu_fedora_url=rsync://mirror1.hs-esslingen.de/fedora/linux
18
19 default_distroname="f22"
20 all_distronames="f20 f21 f22"
21
22 global_arch="x86_64"
23
24 # use EU mirror
25 fedora_url=$eu_fedora_url
26
27 function mirror_distro_arch () {
28     distroname=$1; shift
29     arch=$1; shift
30
31     distroname=$(echo $distroname | tr '[A-Z]' '[a-z]')
32     case $distroname in
33         f*)
34             distroindex=$(echo $distroname | sed -e "s,f,,g")
35             distro="Fedora"
36             rsyncurl=$fedora_url
37             ;;
38         *)
39             echo "WARNING -- Unknown distribution $distroname -- skipped"
40             return 1
41             ;;
42     esac
43
44     excludelist="debug/ iso/ ppc/ source/"
45     options=""
46     [ -n "$(rsync --help | grep no-motd)" ] && options="$options --no-motd"
47     options="$options $dry_run $verbose"
48     options="$options -aH --numeric-ids"
49     options="$options --delete --delete-excluded --delete-after --delay-updates"
50     for e in $excludelist; do
51         options="$options --exclude $e"
52     done
53
54     echo ">>>>>>>>>>>>>>>>>>>> distroname=$distroname arch=$arch rsyncurl=$rsyncurl"
55     [ -n "$verbose" ] && echo "rsync options=$options"
56
57     paths=""
58     [ -z "$skip_core" ] && paths="releases/$distroindex/Everything/$arch/os/"
59     paths="$paths updates/$distroindex/$arch/"
60     localpath=fedora
61
62     for repopath in $paths; do
63         echo "===== $distro -> $distroindex $repopath"
64         [ -z "$dry_run" ] && mkdir -p ${root}/${localpath}/${repopath}
65         command="rsync $options ${rsyncurl}/${repopath} ${root}/${localpath}/${repopath}"
66         echo $command
67         $command
68     done
69
70     echo "<<<<<<<<<<<<<<<<<<<< $distroname $arch"
71
72     return $RES 
73 }
74
75 function usage () {
76     echo "Usage: $COMMAND [-n] [-v] [-l] [-c] [-e|-s|-u rsyncurl] [-f distroname|-F]"
77     echo "Options:"
78     echo " -n : dry run"
79     echo " -v : turn off verbose"
80     echo " -l : turns on autologging in $LOGDIR"
81     echo " -c : also sync core repository (releases)"
82     echo " -s : uses US mirror $us_fedora_url"
83     echo " -e : uses EU mirror $eu_fedora_url"
84     echo " -u <url> : use this (rsync) for fedora (default is $fedora_url)"
85     echo " -f distroname - default is $default_distroname"
86     echo " -F : do on all distros $all_distronames"
87     exit 1
88 }
89
90 function run () {
91     RES=0
92     for distroname in $distronames ; do 
93         for arch in $archs; do 
94             mirror_distro_arch "$distroname" "$arch" || RES=1
95         done
96     done
97     return $RES
98 }
99
100 function main () {
101     distronames=""
102     archs="$global_arch"
103     while getopts "nvlc:u:sef:Fh" opt ; do
104         case $opt in
105             n) dry_run=--dry-run ;;
106             v) verbose= ;;
107             l) log=true ;;
108             c) skip_core= ;;
109             u) fedora_url=$OPTARG ;;
110             s) fedora_url=$us_fedora_url ;;
111             e) fedora_url=$eu_fedora_url ;;
112             f) distronames="$distronames $OPTARG" ;;
113             F) distronames="$distronames $all_distronames" ;;
114             h|*) usage ;;
115         esac
116     done
117     shift $(($OPTIND-1))
118     [[ -n "$@" ]] && usage
119     [ -z "$distronames" ] && distronames=$default_distroname
120
121     # auto log : if specified
122     if [ -n "$log" ] ; then
123         mkdir -p $LOGDIR
124         run &> $LOG
125     else
126         run
127     fi 
128     if [ "$?" == 0 ]; then
129         # report to fedora's infra
130         # can't get the config right...
131         #/usr/bin/report_mirror
132         exit 0
133     else
134         exit 1
135     fi
136 }
137
138 main "$@"