Skip to content

Commit dc672ed

Browse files
committed
Add local repository backup fallback to avoid GitHub rate limits
- Add ensure_local_backup_repo() to clone repo fresh on each update - Add try_local_backup() to copy files from local backup when GitHub fails - Modify curl_to_dir() to automatically fall back to local backup - Update fetch_lib.sh to use local backup when downloading lib.sh fails - Call ensure_local_backup_repo() in update and install scripts This solves issue #2771 by providing a local fallback when GitHub raw.githubusercontent.com hits rate limits. The implementation: - Prioritizes GitHub (online-first approach) - Falls back to local clone automatically on download failures - Clones fresh repo on each update run (no stale files) - Minimal code changes (~55 lines across 4 files) - Zero breaking changes - fully backward compatible
1 parent d4536dd commit dc672ed

File tree

4 files changed

+106
-7
lines changed

4 files changed

+106
-7
lines changed

lib.sh

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ fi
618618
}
619619

620620
# A function to fetch a file with curl to a directory
621+
# Falls back to local backup if download fails
621622
# 1 = https://example.com
622623
# 2 = name of file
623624
# 3 = directory that the file should end up in
@@ -629,18 +630,29 @@ fi
629630
rm -f "$3"/"$2"
630631
if [ -n "$download_script_function_in_use" ]
631632
then
632-
curl -sfL "$1"/"$2" -o "$3"/"$2"
633+
if ! curl -sfL "$1"/"$2" -o "$3"/"$2"
634+
then
635+
# Try local backup
636+
try_local_backup "$1" "$2" "$3"
637+
fi
633638
else
634639
local retries=0
635640
while :
636641
do
637642
if [ "$retries" -ge 10 ]
638643
then
639-
if yesno_box_yes "Tried 10 times but didn't succeed. We will now exit the script because it might break things. You can choose 'No' to continue on your own risk."
644+
# Exhausted retries, try local backup
645+
if try_local_backup "$1" "$2" "$3"
640646
then
641-
exit 1
647+
print_text_in_color "$IGreen" "✓ Used local backup copy"
648+
break
642649
else
643-
return 1
650+
if yesno_box_yes "Tried 10 times but didn't succeed. We will now exit the script because it might break things. You can choose 'No' to continue on your own risk."
651+
then
652+
exit 1
653+
else
654+
return 1
655+
fi
644656
fi
645657
fi
646658
if ! curl -sfL "$1"/"$2" -o "$3"/"$2"
@@ -2285,6 +2297,71 @@ https://shop.hanssonit.se/product/upgrade-between-major-owncloud-nextcloud-versi
22852297
fi
22862298
}
22872299

2300+
## LOCAL REPOSITORY BACKUP FUNCTIONS
2301+
# These functions provide fallback to a local clone when GitHub is unavailable
2302+
2303+
# Clone or refresh local backup of repository
2304+
# This runs during update to keep a fresh copy as fallback
2305+
ensure_local_backup_repo() {
2306+
local BACKUP_REPO="/var/scripts/vm-repo-backup"
2307+
2308+
# Install git if needed
2309+
install_if_not git
2310+
2311+
# Remove old clone and get fresh copy
2312+
print_text_in_color "$ICyan" "Creating local backup of repository (in case of network issues)..."
2313+
rm -rf "$BACKUP_REPO"
2314+
2315+
# Clone with timeout, don't fail if it doesn't work
2316+
if timeout 120 git clone --depth 1 --branch main \
2317+
https://github.com/nextcloud/vm.git "$BACKUP_REPO" &>/dev/null
2318+
then
2319+
chmod -R +rx "$BACKUP_REPO"
2320+
print_text_in_color "$IGreen" "✓ Local backup ready"
2321+
return 0
2322+
else
2323+
print_text_in_color "$IYellow" "Could not create local backup (continuing anyway)"
2324+
return 1
2325+
fi
2326+
}
2327+
2328+
# Try to copy file from local backup repository
2329+
# Called when curl download fails (rate limits, network issues)
2330+
try_local_backup() {
2331+
local url="$1"
2332+
local file="$2"
2333+
local dest_dir="$3"
2334+
local BACKUP_REPO="/var/scripts/vm-repo-backup"
2335+
2336+
# Check if backup exists
2337+
if [ ! -d "$BACKUP_REPO" ]
2338+
then
2339+
return 1
2340+
fi
2341+
2342+
# Convert URL to local path
2343+
# https://raw.githubusercontent.com/nextcloud/vm/main/apps/script.sh -> apps/script.sh
2344+
local path_part
2345+
path_part=$(echo "$url" | sed 's|https://raw.githubusercontent.com/nextcloud/vm/main/||' | sed 's|https://raw.githubusercontent.com/nextcloud/vm/master/||')
2346+
2347+
# If path_part is empty, we're downloading from root
2348+
if [ -z "$path_part" ]
2349+
then
2350+
local local_file="$BACKUP_REPO/$file"
2351+
else
2352+
local local_file="$BACKUP_REPO/$path_part/$file"
2353+
fi
2354+
2355+
if [ -f "$local_file" ]
2356+
then
2357+
print_text_in_color "$IYellow" "⚠ GitHub unavailable, using local backup: $file"
2358+
cp "$local_file" "$dest_dir/$file"
2359+
return 0
2360+
else
2361+
return 1
2362+
fi
2363+
}
2364+
22882365
## bash colors
22892366
# Reset
22902367
Color_Off='\e[0m' # Text Reset

nextcloud_install_production.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ install_if_not iputils-ping
164164

165165
# Download needed libraries before execution of the first script
166166
mkdir -p "$SCRIPTS"
167+
168+
# Create local backup (in case GitHub is rate limited during install)
169+
ensure_local_backup_repo
170+
167171
download_script GITHUB_REPO lib
168172
download_script STATIC fetch_lib
169173

nextcloud_update.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ source <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/main/lib.sh)
1717
ncdb
1818
nc_update
1919

20+
# Create local backup of repository (for fallback if GitHub has issues)
21+
ensure_local_backup_repo
22+
2023
# Check for errors + debug code and abort if something isn't right
2124
# 1 = ON
2225
# 0 = OFF

static/fetch_lib.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,28 @@ if ! [ -f /var/scripts/lib.sh ]
2222
then
2323
if ! curl -sfL https://raw.githubusercontent.com/nextcloud/vm/main/lib.sh -o /var/scripts/lib.sh
2424
then
25-
print_text_in_color "$IRed" "You don't seem to have an internet \
25+
# Try local backup
26+
if [ -f /var/scripts/vm-repo-backup/lib.sh ]
27+
then
28+
print_text_in_color "$IYellow" "⚠ GitHub unavailable, using local backup: lib.sh"
29+
cp /var/scripts/vm-repo-backup/lib.sh /var/scripts/lib.sh
30+
else
31+
print_text_in_color "$IRed" "You don't seem to have an internet \
2632
connection and the local lib isn't available. Hence you cannot run this script."
27-
exit 1
33+
exit 1
34+
fi
2835
fi
2936
elif ! [ -f /var/scripts/nextcloud-startup-script.sh ]
3037
then
31-
curl -sfL https://raw.githubusercontent.com/nextcloud/vm/main/lib.sh -o /var/scripts/lib.sh
38+
if ! curl -sfL https://raw.githubusercontent.com/nextcloud/vm/main/lib.sh -o /var/scripts/lib.sh
39+
then
40+
# Try local backup
41+
if [ -f /var/scripts/vm-repo-backup/lib.sh ]
42+
then
43+
print_text_in_color "$IYellow" "⚠ GitHub unavailable, using local backup: lib.sh"
44+
cp /var/scripts/vm-repo-backup/lib.sh /var/scripts/lib.sh
45+
fi
46+
fi
3247
fi
3348

3449
# shellcheck source=lib.sh

0 commit comments

Comments
 (0)