| 1 | #!/bin/bash |
| 2 | # fc4upd.sh |
| 3 | |
| 4 | # Script author: Frank Wang. |
| 5 | # Slight stylistic modifications by ABS Guide author. |
| 6 | # Used in ABS Guide with permission. |
| 7 | |
| 8 | |
| 9 | # Download Fedora Core 4 update from mirror site using rsync. |
| 10 | # Should also work for newer Fedora Cores -- 5, 6, . . . |
| 11 | # Only download latest package if multiple versions exist, |
| 12 | #+ to save space. |
| 13 | |
| 14 | URL=rsync://distro.ibiblio.org/fedora-linux-core/updates/ |
| 15 | # URL=rsync://ftp.kddilabs.jp/fedora/core/updates/ |
| 16 | # URL=rsync://rsync.planetmirror.com/fedora-linux-core/updates/ |
| 17 | |
| 18 | DEST=${1:-/var/www/html/fedora/updates/} |
| 19 | LOG=/tmp/repo-update-$(/bin/date +%Y-%m-%d).txt |
| 20 | PID_FILE=/var/run/${0##*/}.pid |
| 21 | |
| 22 | E_RETURN=85 # Something unexpected happened. |
| 23 | |
| 24 | |
| 25 | # General rsync options |
| 26 | # -r: recursive download |
| 27 | # -t: reserve time |
| 28 | # -v: verbose |
| 29 | |
| 30 | OPTS="-rtv --delete-excluded --delete-after --partial" |
| 31 | |
| 32 | # rsync include pattern |
| 33 | # Leading slash causes absolute path name match. |
| 34 | INCLUDE=( |
| 35 | "/4/i386/kde-i18n-Chinese*" |
| 36 | # ^ ^ |
| 37 | # Quoting is necessary to prevent globbing. |
| 38 | ) |
| 39 | |
| 40 | |
| 41 | # rsync exclude pattern |
| 42 | # Temporarily comment out unwanted pkgs using "#" . . . |
| 43 | EXCLUDE=( |
| 44 | /1 |
| 45 | /2 |
| 46 | /3 |
| 47 | /testing |
| 48 | /4/SRPMS |
| 49 | /4/ppc |
| 50 | /4/x86_64 |
| 51 | /4/i386/debug |
| 52 | "/4/i386/kde-i18n-*" |
| 53 | "/4/i386/openoffice.org-langpack-*" |
| 54 | "/4/i386/*i586.rpm" |
| 55 | "/4/i386/GFS-*" |
| 56 | "/4/i386/cman-*" |
| 57 | "/4/i386/dlm-*" |
| 58 | "/4/i386/gnbd-*" |
| 59 | "/4/i386/kernel-smp*" |
| 60 | # "/4/i386/kernel-xen*" |
| 61 | # "/4/i386/xen-*" |
| 62 | ) |
| 63 | |
| 64 | |
| 65 | init () { |
| 66 | # Let pipe command return possible rsync error, e.g., stalled network. |
| 67 | set -o pipefail # Newly introduced in Bash, version 3. |
| 68 | |
| 69 | TMP=${TMPDIR:-/tmp}/${0##*/}.$$ # Store refined download list. |
| 70 | trap "{ |
| 71 | rm -f $TMP 2>/dev/null |
| 72 | }" EXIT # Clear temporary file on exit. |
| 73 | } |
| 74 | |
| 75 | |
| 76 | check_pid () { |
| 77 | # Check if process exists. |
| 78 | if [ -s "$PID_FILE" ]; then |
| 79 | echo "PID file exists. Checking ..." |
| 80 | PID=$(/bin/egrep -o "^[[:digit:]]+" $PID_FILE) |
| 81 | if /bin/ps --pid $PID &>/dev/null; then |
| 82 | echo "Process $PID found. ${0##*/} seems to be running!" |
| 83 | /usr/bin/logger -t ${0##*/} \ |
| 84 | "Process $PID found. ${0##*/} seems to be running!" |
| 85 | exit $E_RETURN |
| 86 | fi |
| 87 | echo "Process $PID not found. Start new process . . ." |
| 88 | fi |
| 89 | } |
| 90 | |
| 91 | |
| 92 | # Set overall file update range starting from root or $URL, |
| 93 | #+ according to above patterns. |
| 94 | set_range () { |
| 95 | include= |
| 96 | exclude= |
| 97 | for p in "${INCLUDE[@]}"; do |
| 98 | include="$include --include \"$p\"" |
| 99 | done |
| 100 | |
| 101 | for p in "${EXCLUDE[@]}"; do |
| 102 | exclude="$exclude --exclude \"$p\"" |
| 103 | done |
| 104 | } |
| 105 | |
| 106 | |
| 107 | # Retrieve and refine rsync update list. |
| 108 | get_list () { |
| 109 | echo $$ > $PID_FILE || { |
| 110 | echo "Can't write to pid file $PID_FILE" |
| 111 | exit $E_RETURN |
| 112 | } |
| 113 | |
| 114 | echo -n "Retrieving and refining update list . . ." |
| 115 | |
| 116 | # Retrieve list -- 'eval' is needed to run rsync as a single command. |
| 117 | # $3 and $4 is the date and time of file creation. |
| 118 | # $5 is the full package name. |
| 119 | previous= |
| 120 | pre_file= |
| 121 | pre_date=0 |
| 122 | eval /bin/nice /usr/bin/rsync \ |
| 123 | -r $include $exclude $URL | \ |
| 124 | egrep '^dr.x|^-r' | \ |
| 125 | awk '{print $3, $4, $5}' | \ |
| 126 | sort -k3 | \ |
| 127 | { while read line; do |
| 128 | # Get seconds since epoch, to filter out obsolete pkgs. |
| 129 | cur_date=$(date -d "$(echo $line | awk '{print $1, $2}')" +%s) |
| 130 | # echo $cur_date |
| 131 | |
| 132 | # Get file name. |
| 133 | cur_file=$(echo $line | awk '{print $3}') |
| 134 | # echo $cur_file |
| 135 | |
| 136 | # Get rpm pkg name from file name, if possible. |
| 137 | if [[ $cur_file == *rpm ]]; then |
| 138 | pkg_name=$(echo $cur_file | sed -r -e \ |
| 139 | 's/(^([^_-]+[_-])+)[[:digit:]]+\..*[_-].*$/\1/') |
| 140 | else |
| 141 | pkg_name= |
| 142 | fi |
| 143 | # echo $pkg_name |
| 144 | |
| 145 | if [ -z "$pkg_name" ]; then # If not a rpm file, |
| 146 | echo $cur_file >> $TMP #+ then append to download list. |
| 147 | elif [ "$pkg_name" != "$previous" ]; then # A new pkg found. |
| 148 | echo $pre_file >> $TMP # Output latest file. |
| 149 | previous=$pkg_name # Save current. |
| 150 | pre_date=$cur_date |
| 151 | pre_file=$cur_file |
| 152 | elif [ "$cur_date" -gt "$pre_date" ]; then |
| 153 | # If same pkg, but newer, |
| 154 | pre_date=$cur_date #+ then update latest pointer. |
| 155 | pre_file=$cur_file |
| 156 | fi |
| 157 | done |
| 158 | echo $pre_file >> $TMP # TMP contains ALL |
| 159 | #+ of refined list now. |
| 160 | # echo "subshell=$BASH_SUBSHELL" |
| 161 | |
| 162 | } # Bracket required here to let final "echo $pre_file >> $TMP" |
| 163 | # Remained in the same subshell ( 1 ) with the entire loop. |
| 164 | |
| 165 | RET=$? # Get return code of the pipe command. |
| 166 | |
| 167 | [ "$RET" -ne 0 ] && { |
| 168 | echo "List retrieving failed with code $RET" |
| 169 | exit $E_RETURN |
| 170 | } |
| 171 | |
| 172 | echo "done"; echo |
| 173 | } |
| 174 | |
| 175 | # Real rsync download part. |
| 176 | get_file () { |
| 177 | |
| 178 | echo "Downloading..." |
| 179 | /bin/nice /usr/bin/rsync \ |
| 180 | $OPTS \ |
| 181 | --filter "merge,+/ $TMP" \ |
| 182 | --exclude '*' \ |
| 183 | $URL $DEST \ |
| 184 | | /usr/bin/tee $LOG |
| 185 | |
| 186 | RET=$? |
| 187 | |
| 188 | # --filter merge,+/ is crucial for the intention. |
| 189 | # + modifier means include and / means absolute path. |
| 190 | # Then sorted list in $TMP will contain ascending dir name and |
| 191 | #+ prevent the following --exclude '*' from "shortcutting the circuit." |
| 192 | |
| 193 | echo "Done" |
| 194 | |
| 195 | rm -f $PID_FILE 2>/dev/null |
| 196 | |
| 197 | return $RET |
| 198 | } |
| 199 | |
| 200 | # ------- |
| 201 | # Main |
| 202 | init |
| 203 | check_pid |
| 204 | set_range |
| 205 | get_list |
| 206 | get_file |
| 207 | RET=$? |
| 208 | # ------- |
| 209 | |
| 210 | if [ "$RET" -eq 0 ]; then |
| 211 | /usr/bin/logger -t ${0##*/} "Fedora update mirrored successfully." |
| 212 | else |
| 213 | /usr/bin/logger -t ${0##*/} \ |
| 214 | "Fedora update mirrored with failure code: $RET" |
| 215 | fi |
| 216 | |
| 217 | exit $RET |