Repository navigation
Expand file tree
/
Copy pathIranMirrors.sh
More file actions
653 lines (558 loc) · 18.1 KB
/
Copy pathIranMirrors.sh
File metadata and controls
653 lines (558 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# ----------------------------- utilities -----------------------------
RED=$'\033[0;31m'
GRN=$'\033[0;32m'
YLW=$'\033[0;33m'
BLU=$'\033[0;34m'
RST=$'\033[0m'
log() { printf "%s\n" "$*"; }
info() { printf "%s[INFO]%s %s\n" "$BLU" "$RST" "$*"; }
ok() { printf "%s[OK]%s %s\n" "$GRN" "$RST" "$*"; }
warn() { printf "%s[WARN]%s %s\n" "$YLW" "$RST" "$*"; }
err() { printf "%s[ERR]%s %s\n" "$RED" "$RST" "$*"; }
need_cmd() {
command -v "$1" >/dev/null 2>&1 || { err "Missing dependency: $1"; exit 1; }
}
is_root() { [[ "${EUID:-$(id -u)}" -eq 0 ]]; }
ts_now() { date +"%Y%m%d-%H%M%S"; }
mkdirp() { mkdir -p "$1"; }
# curl wrapper: returns http code, also collects total time
curl_http_code() {
local url="$1"
curl -fsS -o /dev/null --max-time 6 -w "%{http_code}" "$url" 2>/dev/null || echo "000"
}
curl_time_total_ms() {
local url="$1"
local t
t="$(curl -fsS -o /dev/null --max-time 6 -w "%{time_total}" "$url" 2>/dev/null || echo "99.999")"
awk -v t="$t" 'BEGIN{ printf "%.0f", (t*1000) }'
}
ping_ms() {
local host="$1"
if command -v ping >/dev/null 2>&1; then
local out
out="$(ping -c 1 -W 1 "$host" 2>/dev/null || true)"
awk 'match($0,/time=([0-9.]+)[ ]*ms/,a){print a[1]}' <<<"$out" | head -n1 | awk '{printf "%.0f",$1}' || true
fi
}
host_from_url() {
local url="$1"
url="${url#http://}"
url="${url#https://}"
url="${url%%/*}"
printf "%s" "$url"
}
# score: lower is better
# Uses ping if available; else uses curl time.
# Also requires HTTP check to pass for a given probe path.
score_mirror() {
local base="$1"
local probe="$2" # full probe path appended to base, may be ""
local expect="$3" # "any" or "ok" (ok=200/301/302) or "docker"(200/401)
local url
if [[ -n "$probe" ]]; then
url="${base%/}/${probe#/}"
else
url="${base%/}/"
fi
local code
code="$(curl_http_code "$url")"
local pass=0
case "$expect" in
ok)
[[ "$code" == "200" || "$code" == "301" || "$code" == "302" ]] && pass=1
;;
docker)
[[ "$code" == "200" || "$code" == "401" ]] && pass=1
;;
any)
[[ "$code" != "000" ]] && pass=1
;;
*)
pass=0
;;
esac
if [[ "$pass" -ne 1 ]]; then
printf "999999 %s %s\n" "$code" "$url"
return 0
fi
local host pms tms
host="$(host_from_url "$base")"
pms="$(ping_ms "$host" || true)"
if [[ -n "${pms:-}" ]]; then
# ping-based score
printf "%s %s %s\n" "$pms" "$code" "$url"
else
tms="$(curl_time_total_ms "$url")"
# add small penalty because curl timing includes TLS/HTTP but is less stable than ping
printf "%s %s %s\n" "$((tms+25))" "$code" "$url"
fi
}
choose_best_mirrors() {
# Inputs:
# - array of "name|base|probe|expect"
# Outputs:
# - prints best base URLs (one per line) sorted by score; limited by BEST_N
local -n _arr="$1"
local BEST_N="${2:-4}"
local scored=()
local item name base probe expect
for item in "${_arr[@]}"; do
IFS='|' read -r name base probe expect <<<"$item"
local s code url
read -r s code url < <(score_mirror "$base" "$probe" "$expect")
scored+=("${s}|${name}|${base}|${code}|${url}")
done
printf "%s\n" "${scored[@]}" \
| sort -t'|' -k1,1n \
| head -n "$BEST_N" \
| awk -F'|' '{print $3}'
}
# ----------------------------- OS detection -----------------------------
OS_ID=""
OS_LIKE=""
OS_NAME=""
OS_VERSION_ID=""
OS_VERSION_MAJOR=0
VERSION_CODENAME=""
detect_os() {
if [[ -r /etc/os-release ]]; then
# shellcheck disable=SC1091
. /etc/os-release
OS_ID="${ID:-}"
OS_LIKE="${ID_LIKE:-}"
OS_NAME="${NAME:-}"
OS_VERSION_ID="${VERSION_ID:-}"
VERSION_CODENAME="${VERSION_CODENAME:-}"
OS_VERSION_MAJOR="${OS_VERSION_ID%%.*}"
else
err "/etc/os-release not found. Unsupported system."
exit 1
fi
if [[ -z "$OS_VERSION_MAJOR" ]]; then
OS_VERSION_MAJOR=0
fi
info "Detected OS: ${OS_NAME:-unknown} (ID=${OS_ID:-?}, VERSION_ID=${OS_VERSION_ID:-?})"
}
is_debian_family() {
[[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "$OS_LIKE" == *"debian"* ]]
}
is_rhel_family() {
[[ "$OS_ID" == "centos" || "$OS_ID" == "rhel" || "$OS_ID" == "rocky" || "$OS_ID" == "almalinux" || "$OS_LIKE" == *"rhel"* || "$OS_LIKE" == *"fedora"* ]]
}
is_arch_family() {
[[ "$OS_ID" == "arch" || "$OS_ID" == "manjaro" || "$OS_LIKE" == *"arch"* ]]
}
get_ubuntu_deb822() {
# Return 0 if this Ubuntu release uses the DEB822 source format by default (>= 24.04)
[[ "$OS_ID" == "ubuntu" && "$OS_VERSION_MAJOR" -ge 24 ]]
}
# ----------------------------- backups -----------------------------
BACKUP_DIR="/var/backups/mirrorgpt"
BACKUP_META="${BACKUP_DIR}/LAST_BACKUP"
backup_debian() {
mkdirp "$BACKUP_DIR"
local stamp; stamp="$(ts_now)"
local dest="${BACKUP_DIR}/debian-apt-${stamp}"
mkdirp "$dest"
if [[ -d /etc/apt ]]; then
cp -a /etc/apt/sources.list "$dest/" 2>/dev/null || true
if [[ -d /etc/apt/sources.list.d ]]; then
cp -a /etc/apt/sources.list.d "$dest/"
fi
ok "Backed up APT config to: $dest"
else
warn "/etc/apt not found."
fi
printf "%s\n" "$dest" >"$BACKUP_META"
}
restore_debian() {
if [[ ! -r "$BACKUP_META" ]]; then
err "No backup metadata found at $BACKUP_META"
exit 1
fi
local src; src="$(cat "$BACKUP_META")"
if [[ ! -d "$src" ]]; then
err "Backup directory not found: $src"
exit 1
fi
# Remove our custom file if present
rm -f /etc/apt/sources.list.d/mirrorgpt.sources 2>/dev/null || true
if [[ -f "$src/sources.list" ]]; then
cp -a "$src/sources.list" /etc/apt/sources.list
else
rm -f /etc/apt/sources.list 2>/dev/null || true
fi
if [[ -d "$src/sources.list.d" ]]; then
rm -rf /etc/apt/sources.list.d
cp -a "$src/sources.list.d" /etc/apt/
fi
ok "Restored APT config from: $src"
info "Run: apt update"
}
backup_rhel() {
mkdirp "$BACKUP_DIR"
local stamp; stamp="$(ts_now)"
local dest="${BACKUP_DIR}/rhel-repos-${stamp}"
mkdirp "$dest"
if [[ -d /etc/yum.repos.d ]]; then
cp -a /etc/yum.repos.d "$dest/"
ok "Backed up YUM/DNF repos to: $dest"
else
warn "/etc/yum.repos.d not found."
fi
printf "%s\n" "$dest" >"$BACKUP_META"
}
restore_rhel() {
if [[ ! -r "$BACKUP_META" ]]; then
err "No backup metadata found at $BACKUP_META"
exit 1
fi
local src; src="$(cat "$BACKUP_META")"
if [[ ! -d "$src" ]]; then
err "Backup directory not found: $src"
exit 1
fi
if [[ -d "$src/yum.repos.d" ]]; then
rm -rf /etc/yum.repos.d
cp -a "$src/yum.repos.d" /etc/
ok "Restored YUM/DNF repos from: $src"
info "Run: dnf makecache or yum makecache"
else
err "Backup does not contain yum.repos.d: $src"
exit 1
fi
}
backup_arch() {
mkdirp "$BACKUP_DIR"
local stamp; stamp="$(ts_now)"
local dest="${BACKUP_DIR}/arch-pacman-${stamp}"
mkdirp "$dest"
if [[ -f /etc/pacman.d/mirrorlist ]]; then
cp -a /etc/pacman.d/mirrorlist "$dest/"
ok "Backed up pacman mirrorlist to: $dest"
else
warn "/etc/pacman.d/mirrorlist not found."
fi
printf "%s\n" "$dest" >"$BACKUP_META"
}
restore_arch() {
if [[ ! -r "$BACKUP_META" ]]; then
err "No backup metadata found at $BACKUP_META"
exit 1
fi
local src; src="$(cat "$BACKUP_META")"
if [[ ! -d "$src" ]]; then
err "Backup directory not found: $src"
exit 1
fi
if [[ -f "$src/mirrorlist" ]]; then
cp -a "$src/mirrorlist" /etc/pacman.d/mirrorlist
ok "Restored pacman mirrorlist from: $src"
info "Run: pacman -Syy"
else
err "Backup does not contain mirrorlist: $src"
exit 1
fi
}
do_backup() {
detect_os
if is_debian_family; then
backup_debian
elif is_rhel_family; then
backup_rhel
elif is_arch_family; then
backup_arch
else
err "Backup not implemented for this OS."
exit 1
fi
}
do_restore() {
detect_os
if is_debian_family; then
restore_debian
elif is_rhel_family; then
restore_rhel
elif is_arch_family; then
restore_arch
else
err "Restore not implemented for this OS."
exit 1
fi
}
# ----------------------------- mirror catalog -----------------------------
# Format: "name|base_url|probe_path|expect"
# Probes are lightweight. For distribution-specific probes they can be overridden later.
MIRRORS_UBUNTU=(
"Ubuntu Official Archive|https://archive.ubuntu.com/ubuntu|dists/|ok"
"Ubuntu Official Security|https://security.ubuntu.com/ubuntu|dists/|ok"
"Ubuntu IR Official (ir.archive.ubuntu.com)|http://ir.archive.ubuntu.com/ubuntu|dists/|ok"
"Shatel Mirror|https://mirror.shatel.ir/ubuntu|dists/|ok"
"Arvan Linux Repo|https://arvancloud.ir/dev/linux-repository/ubuntu|dists/|ok"
"IranServer Mirror|https://mirror.iranserver.com/ubuntu|dists/|ok"
"MobinHost Mirror|https://mirror.mobinhost.com/ubuntu|dists/|ok"
"0-1 Cloud Mirror|https://mirror.0-1.cloud/ubuntu|dists/|ok"
"ManageIT Mirror|https://mirror.manageit.ir/ubuntu|dists/|ok"
"AminiDC Mirror|https://mirror.aminidc.com/ubuntu|dists/|ok"
"Kimiahost Ubuntu Mirror|https://ubuntu-mirror.kimiahost.com/ubuntu|dists/|ok"
"DigitalVPS Mirror|https://mirror.digitalvps.ir/ubuntu|dists/|ok"
"Sindad Ubuntu Mirror|https://ir.ubuntu.sindad.cloud/ubuntu|dists/|ok"
"Afranet Mirror|https://afranet.com/ubuntu|dists/|ok"
"Pishgaman Mirror|https://pishgaman.net/ubuntu|dists/|ok"
"Parsdev Mirror|https://parsdev.com/ubuntu|dists/|ok"
"LinuxMirrors.ir|https://linuxmirrors.ir/ubuntu|dists/|ok"
"IUT Mirror|https://repo.iut.ac.ir/ubuntu|dists/|ok"
"Pardisco Mirror|https://pardisco.co/ubuntu|dists/|ok"
"Abrha Mirror|https://abrha.net/ubuntu|dists/|ok"
"AtlanticsCloud Mirror|https://atlanticscloud.ir/ubuntu|dists/|ok"
)
MIRRORS_DEBIAN=(
"Debian Official|https://deb.debian.org/debian|dists/|ok"
"Debian Security|https://security.debian.org/debian-security|dists/|ok"
"Shatel Mirror|https://mirror.shatel.ir/debian|dists/|ok"
"Arvan Linux Repo|https://arvancloud.ir/dev/linux-repository/debian|dists/|ok"
"IranServer Mirror|https://mirror.iranserver.com/debian|dists/|ok"
"MobinHost Mirror|https://mirror.mobinhost.com/debian|dists/|ok"
"0-1 Cloud Mirror|https://mirror.0-1.cloud/debian|dists/|ok"
"AminiDC Mirror|https://mirror.aminidc.com/debian|dists/|ok"
"IUT Mirror|https://repo.iut.ac.ir/debian|dists/|ok"
"Pardisco Mirror|https://pardisco.co/debian|dists/|ok"
"Abrha Mirror|https://abrha.net/debian|dists/|ok"
"Parsdev Mirror|https://parsdev.com/debian|dists/|ok"
"LinuxMirrors.ir|https://linuxmirrors.ir/debian|dists/|ok"
)
MIRRORS_ARCH=(
"Arch Official|https://geo.mirror.pkgbuild.com|core/os/x86_64/|ok"
"IUT Mirror|https://repo.iut.ac.ir/archlinux|core/os/x86_64/|ok"
"MobinHost Mirror|https://mirror.mobinhost.com/archlinux|core/os/x86_64/|ok"
"0-1 Cloud Mirror|https://mirror.0-1.cloud/archlinux|core/os/x86_64/|ok"
"Arvan Linux Repo|https://arvancloud.ir/dev/linux-repository/archlinux|core/os/x86_64/|ok"
"Pardisco Mirror|https://pardisco.co/archlinux|core/os/x86_64/|ok"
"Liara Mirror (docs)|https://liara.ir| |any"
)
# ----------------------------- apply mirrors -----------------------------
write_apt_classic() {
local primary="$1"
local secondary="$2"
local codename="$3"
local f="/etc/apt/sources.list"
if [[ -f "$f" ]]; then
cp -a "$f" "${f}.mirrorgpt.bak.$(ts_now)" 2>/dev/null || true
fi
cat >"$f" <<EOF
# Generated by mirrorgpt.sh at $(date -Is)
deb ${primary%/} ${codename} main restricted universe multiverse
deb ${primary%/} ${codename}-updates main restricted universe multiverse
deb ${primary%/} ${codename}-backports main restricted universe multiverse
deb ${secondary%/} ${codename} main restricted universe multiverse
deb ${secondary%/} ${codename}-updates main restricted universe multiverse
deb ${secondary%/} ${codename}-backports main restricted universe multiverse
EOF
if [[ "$OS_ID" == "ubuntu" ]]; then
cat >>"$f" <<EOF
deb http://security.ubuntu.com/ubuntu ${codename}-security main restricted universe multiverse
EOF
else
cat >>"$f" <<EOF
deb https://security.debian.org/debian-security ${codename}-security main contrib non-free non-free-firmware
EOF
fi
ok "Wrote $f"
info "Next: apt update"
}
write_apt_deb822() {
local primary="$1"
local secondary="$2"
local codename="$3"
local f="/etc/apt/sources.list.d/mirrorgpt.sources"
# Disable old one-line style sources.list if present
local old_list="/etc/apt/sources.list"
if [[ -f "$old_list" ]]; then
mv "$old_list" "${old_list}.mirrorgpt.disabled.$(ts_now)" 2>/dev/null || true
fi
# Move the default ubuntu.sources file out of sources.list.d to avoid apt warnings
local default_deb822="/etc/apt/sources.list.d/ubuntu.sources"
if [[ -f "$default_deb822" ]]; then
mkdirp "$BACKUP_DIR"
mv "$default_deb822" "$BACKUP_DIR/ubuntu.sources.original-$(ts_now)" 2>/dev/null || true
fi
# Determine if security mirror is reachable
local security_url="http://security.ubuntu.com/ubuntu"
local security_reachable=0
if curl_http_code "${security_url}/dists/${codename}-security/Release" | grep -qE '^(200|301|302)'; then
security_reachable=1
fi
cat >"$f" <<EOF
# Generated by mirrorgpt.sh at $(date -Is)
Types: deb
URIs: ${primary%/}
Suites: ${codename} ${codename}-updates ${codename}-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: deb
URIs: ${secondary%/}
Suites: ${codename} ${codename}-updates ${codename}-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
if [[ "$security_reachable" -eq 1 ]]; then
cat >>"$f" <<EOF
Types: deb
URIs: ${security_url}
Suites: ${codename}-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
else
warn "Official security mirror unreachable; security updates omitted."
warn "You may add a trusted local mirror for -security manually if needed."
fi
ok "Wrote $f"
info "Next: apt update"
}
apply_arch_mirrorlist() {
local primary="$1"
local secondary="$2"
local f="/etc/pacman.d/mirrorlist"
cp -a "$f" "${f}.mirrorgpt.bak.$(ts_now)" 2>/dev/null || true
cat >"$f" <<EOF
# Generated by mirrorgpt.sh at $(date -Is)
# Ranked mirrors (top first)
Server = ${primary%/}/\$repo/os/\$arch
Server = ${secondary%/}/\$repo/os/\$arch
EOF
ok "Wrote $f"
info "Next: pacman -Syy"
}
apply_rhel_repo_hint() {
local primary="$1"
local secondary="$2"
warn "Automatic YUM/DNF repo rewriting is not enabled (distro-specific)."
info "Selected mirrors:"
log " Primary: $primary"
log " Secondary: $secondary"
info "If your mirror provides CentOS/Rocky/Alma paths, create .repo entries accordingly."
}
switch_to_best_mirrors() {
detect_os
if ! is_root; then
err "Run as root (sudo) to modify system repo configuration."
exit 1
fi
info "Creating backup first..."
do_backup
info "Testing mirrors, this may take a while..."
local best=()
local primary="" secondary=""
if is_debian_family; then
local codename="${VERSION_CODENAME:-}"
if [[ -z "$codename" ]]; then
err "Could not determine distribution codename."
exit 1
fi
# Replace generic probe with release-specific probe so mirrors lacking the release fail
local -a mirrors_release
local base_array_name
local original_entry
if [[ "$OS_ID" == "ubuntu" ]]; then
base_array_name="MIRRORS_UBUNTU"
else
base_array_name="MIRRORS_DEBIAN"
fi
local -n base_mirrors="$base_array_name"
mirrors_release=()
for original_entry in "${base_mirrors[@]}"; do
IFS='|' read -r name base probe expect <<<"$original_entry"
# Only override probe if it is generic (exactly "dists/") to avoid breaking custom ones
if [[ "$probe" == "dists/" ]]; then
probe="dists/${codename}/Release"
fi
mirrors_release+=("${name}|${base}|${probe}|${expect}")
done
mapfile -t best < <(choose_best_mirrors mirrors_release 4)
primary="${best[0]:-}"
secondary="${best[1]:-${best[0]:-}}"
if [[ -z "$primary" ]]; then
err "No working mirrors found."
exit 1
fi
if [[ "$OS_ID" == "ubuntu" ]] && get_ubuntu_deb822; then
write_apt_deb822 "$primary" "$secondary" "$codename"
else
write_apt_classic "$primary" "$secondary" "$codename"
fi
return 0
fi
if is_arch_family; then
mapfile -t best < <(choose_best_mirrors MIRRORS_ARCH 4)
primary="${best[0]:-}"
secondary="${best[1]:-${best[0]:-}}"
if [[ -z "$primary" ]]; then
err "No working mirrors found."
exit 1
fi
apply_arch_mirrorlist "$primary" "$secondary"
return 0
fi
if is_rhel_family; then
local MIRRORS_RHEL=(
"ITO Repo Portal|https://repo-portal.ito.gov.ir| |any"
"Arvan Linux Repo|https://arvancloud.ir/dev/linux-repository| |any"
"IranServer Mirror|https://mirror.iranserver.com| |any"
"MobinHost Mirror|https://mirror.mobinhost.com| |any"
"0-1 Cloud Mirror|https://mirror.0-1.cloud| |any"
"AminiDC Mirror|https://mirror.aminidc.com| |any"
"IUT Mirror|https://repo.iut.ac.ir| |any"
"Abrha Mirror|https://abrha.net| |any"
"LinuxMirrors.ir|https://linuxmirrors.ir| |any"
"Pardisco Mirror|https://pardisco.co| |any"
)
mapfile -t best < <(choose_best_mirrors MIRRORS_RHEL 4)
primary="${best[0]:-}"
secondary="${best[1]:-${best[0]:-}}"
if [[ -z "$primary" ]]; then
err "No working mirrors found."
exit 1
fi
apply_rhel_repo_hint "$primary" "$secondary"
return 0
fi
err "Unsupported OS for mirror switching."
exit 1
}
# ----------------------------- menu -----------------------------
menu() {
cat <<'EOF'
Select an action:
1) Backup current configuration
2) Restore from last backup
3) Switch mirrors (official first, then Iranian mirrors; auto-ranked)
Enter a number (1-3):
EOF
}
main() {
need_cmd curl
menu
read -r choice
case "$choice" in
1)
if ! is_root; then err "Run as root (sudo) to backup system repo configuration."; exit 1; fi
do_backup
;;
2)
if ! is_root; then err "Run as root (sudo) to restore system repo configuration."; exit 1; fi
do_restore
;;
3)
switch_to_best_mirrors
;;
*)
err "Invalid choice."
exit 1
;;
esac
}
main "$@"