debian-atomic/scripts/apt-cacher-ng.sh

55 lines
2 KiB
Bash
Executable file

#!/bin/bash
# Define the hostname and port for the new mirror
NEW_HOST="192.168.1.101"
NEW_PORT="3142"
# Path to the sources files (Debian 13+ uses debian.sources)
SOURCES_LIST="/etc/apt/sources.list"
DEBIAN_SOURCES="/etc/apt/sources.list.d/debian.sources"
# Check which sources file exists
if [[ -f "$DEBIAN_SOURCES" ]]; then
SOURCES_FILE="$DEBIAN_SOURCES"
echo "Using Debian 13+ sources format: $DEBIAN_SOURCES"
elif [[ -f "$SOURCES_LIST" ]]; then
SOURCES_FILE="$SOURCES_LIST"
echo "Using traditional sources.list format: $SOURCES_LIST"
else
echo "Error: No sources file found at $SOURCES_LIST or $DEBIAN_SOURCES"
exit 1
fi
# Create a backup of the original sources file
cp "$SOURCES_FILE" "${SOURCES_FILE}.bak"
echo "Backup of $SOURCES_FILE created at ${SOURCES_FILE}.bak"
# Use sed to modify the file
# Explanation of the sed command:
# -i: edit the file in place
# -E: use extended regular expressions
# s|...|...|g: substitute (replace)
#
# The regex breaks down as follows:
# ^: start of line
# (deb(?:-src)?): captures "deb" or "deb-src"
# \s+: one or more spaces
# (https?://)?: captures "http://" or "https://" (optional)
# ([^/]+): captures the hostname, anything that is not a slash
# (.*): captures the rest of the line, including the path
#
# The replacement string:
# \1: the captured "deb" or "deb-src"
# : a space
# http://${NEW_HOST}:${NEW_PORT}/: the new prefix
# \3: the captured protocol (e.g., "http://", "https://"), if it exists. Replaces "https://" with "HTTPS///" to follow the example.
# \4: the captured hostname
# \5: the captured rest of the line
sed -i -E "s|^(deb(?:-src)?)\s+(https?://)?([^/]+)(.*)$|\1 http://${NEW_HOST}:${NEW_PORT}/\3\4|" "$SOURCES_FILE"
# A second sed command to handle the "HTTPS///" case as per the example
# It replaces "https://" with "HTTPS///"
sed -i -E "s|http://${NEW_HOST}:${NEW_PORT}/https://|http://${NEW_HOST}:${NEW_PORT}/HTTPS///|" "$SOURCES_FILE"
echo "The file $SOURCES_FILE has been successfully modified."
echo "Please verify the changes and run 'sudo apt update' to refresh the package list."