|
2 | 2 |
|
3 | 3 | # T&M Hansson IT AB © - 2024, https://www.hanssonit.se/
|
4 | 4 |
|
5 |
| -# shellcheck disable=2034,2059 |
6 | 5 | true
|
7 |
| -SCRIPT_NAME="Locate Mirror" |
| 6 | +SCRIPT_NAME="Locate mirror" |
8 | 7 | # shellcheck source=lib.sh
|
9 |
| -source /var/scripts/fetch_lib.sh || source <(curl -sL https://raw.githubusercontent.com/techandme/wordpress-vm/master/lib.sh) |
| 8 | +source /var/scripts/fetch_lib.sh |
| 9 | +SCRIPT_EXPLAINER="To make downloads as fast as possible when updating Ubuntu \ |
| 10 | +you should download mirrors that are as geographically close to you as possible. |
| 11 | +
|
| 12 | +Please note that there are no guarantees that the download mirrors \ |
| 13 | +this script finds will remain for the lifetime of this server. |
| 14 | +Because of this, we don't recommend that you change the mirror unless you live far away from the default. |
| 15 | +
|
| 16 | +This is the method used: https://github.com/vegardit/fast-apt-mirror.sh" |
| 17 | + |
| 18 | +# Check for errors + debug code and abort if something isn't right |
| 19 | +# 1 = ON |
| 20 | +# 0 = OFF |
| 21 | +DEBUG=0 |
| 22 | +debug_mode |
10 | 23 |
|
11 | 24 | # Must be root
|
12 | 25 | root_check
|
13 | 26 |
|
14 |
| -# Use another method if the new one doesn't work |
15 |
| -if [ -z "$REPO" ] |
| 27 | +# Check if Locate Mirror is already installed |
| 28 | +if ! [ -f /usr/local/bin/fast-apt-mirror ] |
16 | 29 | then
|
17 |
| - REPO=$(apt update -q4 && apt-cache policy | grep http | tail -1 | awk '{print $2}') |
| 30 | + # Ask for installing |
| 31 | + install_popup "$SCRIPT_NAME" |
| 32 | +else |
| 33 | + # Ask for removal or reinstallation |
| 34 | + reinstall_remove_menu "$SCRIPT_NAME" |
| 35 | + # Removal |
| 36 | + rm -f /usr/local/bin/fast-apt-mirror |
| 37 | + rm -f /etc/apt/sources.list.backup |
| 38 | + # Show successful uninstall if applicable |
| 39 | + removal_popup "$SCRIPT_NAME" |
18 | 40 | fi
|
19 | 41 |
|
20 |
| -# Check where the best mirrors are and update |
21 |
| -msg_box "To make downloads as fast as possible when updating Ubuntu \ |
22 |
| -you should have download mirrors that are as close to you as possible. |
23 |
| -
|
24 |
| -Please note that there are no gurantees that the download mirrors \ |
25 |
| -this script will find are staying up for the lifetime of this server. |
26 |
| -Because of this, we don't recommend to change the mirror, except you live far away from the default mirror. |
| 42 | +# Install |
| 43 | +install_if_not bash |
| 44 | +install_if_not curl |
| 45 | +install_if_not apt-transport-https |
| 46 | +install_if_not ca-certificates |
| 47 | +curl_to_dir https://raw.githubusercontent.com/vegardit/fast-apt-mirror.sh/v1/ fast-apt-mirror.sh /usr/local/bin |
| 48 | +mv /usr/local/bin/fast-apt-mirror.sh /usr/local/bin/fast-apt-mirror |
| 49 | +chmod 755 /usr/local/bin/fast-apt-mirror |
27 | 50 |
|
28 |
| -This is the method used: https://github.com/jblakeman/apt-select" |
29 |
| -msg_box "Your current server repository is: $REPO" |
| 51 | +# Check current mirror |
| 52 | +CURRENT_MIRROR="$(fast-apt-mirror current)" |
| 53 | +msg_box "Current mirror is $CURRENT_MIRROR" |
30 | 54 |
|
| 55 | +# Ask |
31 | 56 | if ! yesno_box_no "Do you want to try to find a better mirror?"
|
32 | 57 | then
|
33 |
| - print_text_in_color "$ICyan" "Keeping $REPO as mirror..." |
| 58 | + print_text_in_color "$ICyan" "Keeping $CURRENT_MIRROR as mirror..." |
34 | 59 | sleep 1
|
35 | 60 | else
|
36 |
| - if [[ "$KEYBOARD_LAYOUT" =~ ,|/|_ ]] |
| 61 | + if [[ "$KEYBOARD_LAYOUT" =~ ,|/|_ ]] |
37 | 62 | then
|
38 |
| - msg_box "Your keymap contains more than one language, or a special character. ($KEYBOARD_LAYOUT) |
39 |
| -This script can only handle one keymap at the time.\nThe default mirror ($REPO) will be kept." |
| 63 | + msg_box "Your keymap (country code) contains more than one language, or a special character. ($KEYBOARD_LAYOUT) |
| 64 | +This script can only handle one keymap at the time.\nThe default mirror ($CURRENT_MIRROR) will be kept." |
40 | 65 | exit 1
|
41 | 66 | fi
|
| 67 | + # Find |
| 68 | + FIND_MIRROR="$(fast-apt-mirror find -v --healthchecks 100 --speedtests 10 --country "$KEYBOARD_LAYOUT")" |
42 | 69 | print_text_in_color "$ICyan" "Locating the best mirrors..."
|
43 |
| - curl_to_dir https://bootstrap.pypa.io get-pip.py /tmp |
44 |
| - install_if_not python3 |
45 |
| - install_if_not python3-testresources |
46 |
| - install_if_not python3-distutils |
47 |
| - cd /tmp && python3 get-pip.py |
48 |
| - pip install \ |
49 |
| - --upgrade pip \ |
50 |
| - apt-select |
51 |
| - check_command apt-select -m up-to-date -t 4 -c -C "$KEYBOARD_LAYOUT" |
52 |
| - sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup && \ |
53 |
| - if [ -f sources.list ] |
| 70 | + if [ "$CURRENT_MIRROR" != "$FIND_MIRROR" ] |
54 | 71 | then
|
55 |
| - sudo mv sources.list /etc/apt/ |
| 72 | + if yesno_box_yes "Do you want to replace the $CURRENT_MIRROR with $FIND_MIRROR?" |
| 73 | + then |
| 74 | + # Backup |
| 75 | + cp -f /etc/apt/sources.list /etc/apt/sources.list.backup |
| 76 | + # Replace |
| 77 | + if fast-apt-mirror set "$FIND_MIRROR" |
| 78 | + then |
| 79 | + msg_box "Your Ubuntu repo was successfully changed to $FIND_MIRROR" |
| 80 | + fi |
| 81 | + fi |
| 82 | + else |
| 83 | + msg_box "You already have the fastest mirror available, congrats!" |
56 | 84 | fi
|
57 |
| - msg_box "The apt-mirror was successfully changed." |
58 | 85 | fi
|
0 commit comments