Skip to content

Instantly share code, notes, and snippets.

@gnanet
Last active May 3, 2026 02:53
Show Gist options
  • Select an option

  • Save gnanet/694a03d5dec2ae4dd45946f7741cfcab to your computer and use it in GitHub Desktop.

Select an option

Save gnanet/694a03d5dec2ae4dd45946f7741cfcab to your computer and use it in GitHub Desktop.
Mirror php sury ppa to local, to e safe in case ondrej pulls the floor out of your server
#!/bin/bash
#
# source: https://www.reddit.com/r/PHP/comments/njwqpg/comment/gzc18jp/
# author: https://www.reddit.com/user/sleemanj/
#
# For a simple "first steps" with aptly read following sources:
# https://ahelpme.com/software/aptly/install-aptly-under-ubuntu-18-lts-with-nginx-serving-the-packages-and-the-first-steps/
# https://ahelpme.com/linux/ubuntu/mirror-a-ppa-repositories-using-aptly-php-ppaondrej-php/
#
# IMPORTANT First choose a partition with at least 100GB free space, and choose a folder you will place the "aptly" folder
# create the folder named "aptly" on that partition, and set ownership of the "aptly" folder to the user you want to run aptly with
# set the username in the variable APTLY_USER
# Note the full path to the "aptly" folder and provide it to the APTLY_ROOTDIR variable
#
# this version of the script was modified, to allow execution as root, but run aptly with a non privileged user
# it generates a base configuration file, and includes the initial mirror create command.
#
APTLY_USER=
if [ -z "${APTLY_USER}" ]; then echo "Create or delegate a user for running aptly, and set APTLY_USER accordingly"; exit 127; fi
APTLY_USERHOME=$(getent passwd ${APTLY_USER} | cut -d ':' -f6)
APTLY_ROOTDIR=
if [ -z "${APTLY_ROOTDIR}" ]; then
echo "First choose a partition with at least 100GB free space, and choose a sub-folder you want to place the \"aptly\" folder into,"
echo "and set APTLY_ROOTDIR accordingly to the choosen full path";
echo "Example the partition is mounted to \"/var/bigpartiton\"; the folder you will place the \"aptly\" folder into is \"some-sub-folder\""
echo "then your full path would be /var/bigpartiton/some-sub-folder/aptly"; exit 127; fi
APTLY_ROOTCONF=${APTLY_USERHOME}/.aptly.conf
CREATE_REPO_TAG=main
CREATE_REPO_BASE="ondrej/php"
CREATE_REPO="${CREATE_REPO_BASE}/${CREATE_REPO_TAG}"
CREATE_TAGGED_CONF=$(echo "${CREATE_REPO_BASE}" | tr '/' '-').conf.${CREATE_REPO_TAG}
if [ -d $(dirname ${APTLY_ROOTDIR}) ] && [ ! -d ${APTLY_ROOTDIR} ]; then
echo "mkdir -p ${APTLY_ROOTDIR}"
mkdir -p ${APTLY_ROOTDIR}
echo "chown ${APTLY_USER}:${APTLY_USER} ${APTLY_ROOTDIR}"
chown ${APTLY_USER}:${APTLY_USER} ${APTLY_ROOTDIR}
fi
if [ -n "${APTLY_USER}" ] && [ ! -f ${APTLY_ROOTCONF} ]; then
cat <<EOT > ${APTLY_ROOTCONF}
{
"rootDir": "${APTLY_ROOTDIR}",
"downloadConcurrency": 4,
"downloadSpeedLimit": 0,
"architectures": [],
"dependencyFollowSuggests": false,
"dependencyFollowRecommends": false,
"dependencyFollowAllVariants": false,
"dependencyFollowSource": false,
"dependencyVerboseResolve": false,
"gpgDisableSign": false,
"gpgDisableVerify": false,
"gpgProvider": "gpg",
"downloadSourcePackages": false,
"skipLegacyPool": true,
"ppaDistributorID": "ubuntu",
"ppaCodename": "",
"skipContentsPublishing": false,
"FileSystemPublishEndpoints": {},
"S3PublishEndpoints": {},
"SwiftPublishEndpoints": {}
}
EOT
su - ${APTLY_USER} -c "chown ${APTLY_USER}:${APTLY_USER} ${APTLY_ROOTCONF}"
fi
if [ -f ${APTLY_ROOTDIR}/db/CURRENT ]; then
su - ${APTLY_USER} -c "aptly -config=${APTLY_ROOTCONF} -ignore-signatures -architectures=\"amd64\" mirror create ${CREATE_REPO} ppa:ondrej/php"
fi
if [ ! -f ${APTLY_ROOTDIR}/${CREATE_TAGGED_CONF} ]; then
su - ${APTLY_USER} -c "cp ${APTLY_ROOTCONF} ${APTLY_ROOTDIR}/${CREATE_TAGGED_CONF}"
fi
# Filter the packages to mirror so that only certain (vital) php versions are included
FILTER="Name (% __DUMMY__)"
# Below you can list one or more php version, which you want to be mirrored.
# for example:
# 8.2 8.3 8.4 8.5
for PV in 8.2
do
FILTER="${FILTER} | Name (% *$PV*)"
done
ERROR=0
cd ${APTLY_ROOTDIR}
for conf in *.conf.*
do
REPO="ondrej/php/$(echo ${conf} | sed -r 's/^.*\.conf\.//')"
# Ensure we are filtering to the required PHP Versions only
su - ${APTLY_USER} -c "aptly -config=${APTLY_ROOTDIR}/${conf} mirror edit -filter=\"${FILTER}\" -filter-with-deps ${REPO}"
if su - ${APTLY_USER} -c "aptly -config=${APTLY_ROOTDIR}/${conf} mirror update ${REPO}"
then
if su - ${APTLY_USER} -c "aptly -config=${APTLY_ROOTDIR}/${conf} snapshot create \"$(date +'%Y-%m-%d-%Z')/${REPO}\" from mirror ${REPO}"
then
echo "${REPO} Updated" >&2
else
echo "Error creating snapshot of ${REPO} mirror" >&2
ERROR=1
fi
else
echo "Error updating mirror of ${REPO}" >&2
ERROR=1
fi
done
exit $ERROR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment