2 new options -l for logging and -s to use standard mirrors
[build.git] / vbuild-fedora-mirror.sh
1 #!/bin/bash
2 # this can help you create/update your fedora mirror
3 # $Id$
4
5 COMMAND=$(basename $0)
6 LOGDIR=/root/fedora-mirroring
7 DATE=$(date '+%Y-%m-%d-%H-%M')
8 LOG=${LOGDIR}/${DATE}.log
9
10 dry_run=
11 verbose=
12 log=
13 skip_core=
14 root=/mirror/
15
16 us_fedora_url=rsync://mirrors.kernel.org/fedora
17 # change this
18 us_centos_url=rsync://mirrors.rit.edu/centos
19
20 eu_fedora_url=rsync://ftp-stud.hs-esslingen.de/fedora/linux
21 eu_centos_url=rsync://mirrors.ircam.fr/CentOS
22
23 # change this
24 jp_fedora_url="need-to-be-defined"
25 # change this
26 jp_centos_url="need-to-be-defined"
27
28 default_distroname=f8
29 all_distronames="f7 f8 f9 centos5.2"
30 default_arch=i386
31 all_archs="i386 x86_64"
32
33 case $(hostname) in 
34     *.fr|*.de|*.uk)
35         fedora_url=$eu_fedora_url ; centos_url=$eu_centos_url ;;
36     *.jp)
37         fedora_url=$jp_fedora_url ; centos_url=$jp_centos_url ;;
38     *)
39         fedora_url=$us_fedora_url ; centos_url=$us_centos_url ;;
40 esac
41
42 function mirror_distro_arch () {
43     distroname=$1; shift
44     arch=$1; shift
45
46     distroname=$(echo $distroname | tr '[A-Z]' '[a-z]')
47     case $distroname in
48         fc*[1-6])
49             distroindex=$(echo $distroname | sed -e "s,fc,,g")
50             distro="Fedora Core"
51             rsyncurl=$fedora_url
52             ;;
53         f*[7-9])
54             distroindex=$(echo $distroname | sed -e "s,f,,g")
55             distro="Fedora"
56             rsyncurl=$fedora_url
57             ;;
58         centos[4-5]|centos[4-5].[0-9])
59             distroindex=$(echo $distroname | sed -e "s,centos,,g")
60             distro="CentOS"
61             rsyncurl=$centos_url
62             ;;
63         *)
64             echo "WARNING -- Unknown distribution $distroname -- skipped"
65             return 1
66             ;;
67     esac
68
69     excludelist="debug/ iso/ ppc/ source/"
70     options="--archive --compress --delete --delete-excluded $dry_run $verbose"
71     [ -n "$(rsync --help | grep no-motd)" ] && options="$options --no-motd"
72     for e in $excludelist; do
73         options="$options --exclude $e"
74     done
75
76     if [ -n "$verbose" ] ; then 
77         echo "root=$root"
78         echo "distro=$distroname"
79         echo "distroname=$distroname"
80         echo "distroindex=$distroindex"
81         echo "arch=$arch"
82         echo rsyncurl="$rsyncurl"
83         echo "rsync options=$options"
84     fi
85
86     RES=1
87     paths=""
88     case $distro in
89         [Ff]edora*)
90             case $distroindex in
91                 2|4|6)
92                     [ -z "$skip_core" ] && paths="core/$distroindex/$arch/os/"
93                     paths="$paths core/updates/$distroindex/$arch/ extras/$distroindex/$arch/"
94                     RES=0
95                     ;;
96                 7|8|9)
97                     [ -z "$skip_core" ] && paths="releases/$distroindex/Everything/$arch/os/"
98                     paths="$paths updates/$distroindex/$arch/"
99                     # f8 and f9 have the additional newkey repo
100                     case $distroindex in 
101                         8|9) paths="$paths updates/$distroindex/${arch}.newkey/" ;;
102                     esac
103                     RES=0
104                     ;;
105             esac
106             localpath=fedora
107             ;;
108     
109         CentOS*)
110             case $distroindex in
111                 5*)
112                     [ -z "$skip_core" ] && paths="$distroindex/os/$arch/"
113                     paths="$paths $distroindex/updates/$arch/"
114                     RES=0
115                     ;;
116             esac
117             localpath=centos
118             ;;
119
120     esac
121
122     if [ "$RES" = 1 ] ; then
123         echo "DISTRIBUTION $distro $distroindex CURRENTLY UNSUPPORTED - skipped"
124     else
125         for repopath in $paths; do
126             echo "============================== $distro -> $distroindex $repopath"
127             [ -z "$dry_run" ] && mkdir -p ${root}/${localpath}/${repopath}
128             command="rsync $options ${rsyncurl}/${repopath} ${root}/${localpath}/${repopath}"
129             echo $command
130             $command
131         done
132     fi
133
134     return $RES 
135 }
136
137 function usage () {
138     echo "Usage: $COMMAND [-n] [-v] [-c] [-r root] [-u|U rsyncurl] [-e|-j] [-f distroname|-F] [-a arch|-A]"
139     echo "Defaults to -r $root -f $default_distroname -a $default_arch"
140     echo "Default urls : $fedora_url $centos_url"
141     echo "Options:"
142     echo " -n : dry run"
143     echo " -v : verbose"
144     echo " -l : turns on autologging in $LOGDIR"
145     echo " -c : skips core repository"
146     echo " -r root (default is $root)"
147     echo " -u rsyncurl for fedora (default is $fedora_url)"
148     echo " -U rsyncurl for centos (default is $centos_url)"
149     echo " -s : uses standard (US) mirrors $us_fedora_url $us_centos_url"
150     echo " -e : uses European mirrors $eu_fedora_url $eu_centos_url"
151     echo " -j : uses Japanese mirrors $jp_fedora_url $jp_centos_url"
152     echo " -f distroname - use vserver convention, e.g. fc6 and f7"
153     echo " -F : for distroname in $all_distronames"
154     echo " -a arch - use yum convention"
155     echo " -A : for arch in $all_archs"
156     exit 1
157 }
158
159 function run () {
160     RES=0
161     for distroname in $distronames ; do 
162         for arch in $archs; do 
163             mirror_distro_arch "$distroname" "$arch" || RES=1
164         done
165     done
166     return $RES
167 }
168
169 function main () {
170     distronames=""
171     archs=""
172     while getopts "nvlcr:u:U:sejf:Fa:Ah" opt ; do
173         case $opt in
174             n) dry_run=--dry-run ;;
175             v) verbose=--verbose ;;
176             l) log=true ;;
177             c) skip_core=true ;;
178             r) root=$OPTARG ;;
179             u) fedora_url=$OPTARG ;;
180             U) centos_url=$OPTARG ;;
181             s) fedora_url=$us_fedora_url ; centos_url=$us_centos_url ;;
182             e) fedora_url=$eu_fedora_url ; centos_url=$eu_centos_url ;;
183             j) fedora_url=$jp_fedora_url ; centos_url=$jp_centos_url ;;
184             f) distronames="$distronames $OPTARG" ;;
185             F) distronames="$distronames $all_distronames" ;;
186             a) archs="$archs $OPTARG" ;;
187             A) archs="$archs $all_archs" ;;
188             h|*) usage ;;
189         esac
190     done
191     [ -z "$distronames" ] && distronames=$default_distroname
192     [ -z "$archs" ] && archs=$default_arch
193
194     # auto log : if specified
195     if [ -n "$log" ] ; then
196         mkdir -p $LOGDIR
197         run &> $LOG
198     else
199         run
200     fi
201     exit $?
202 }
203
204 main "$@"