Skip to content

Instantly share code, notes, and snippets.

Last active June 26, 2023 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jikamens/206e8abf7f5ff381e79951c313da78d8 to your computer and use it in GitHub Desktop.
Save jikamens/206e8abf7f5ff381e79951c313da78d8 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
# This script reads a dpkg.log fragment, identifies all of the
# packages upgraded in it, finds and downloads the pre-upgrade
# versions of those packages from Launchpad, and installs them,
# downgrading the system to approximately the state it was in before
# the packages were upgraded.
# To use it, copy the section of /var/log/dpkg.log containing the
# upgrade you want to undo into a new file, remove the lines
# corresponding to packages that you _don't_ want to downgrade, and
# then feed the rest into this script either on stdin or by specifying
# the log file name with `--log-file`. You need to either run the
# script as root or specify `--package-dir` to write the downloaded
# packages into a directory you have write access to and
# `--download-only` to download without running `apt-get install` on
# the package files (since you can't do that if you're not root). If
# you do the latter then you can run `sudo apt-get install` afterward,
# specifying the paths to the downloaded deb files. Note that
# `apt-get` won't recognize them as files to be installed unless
# there's at least one slash in their names, so if you're in the same
# directory as the files prefix their names with "./".
# Run the script with `--help` for more usage information or just read
# the usage message below.
# This is especially useful if you are running a pre-release version
# of Ubuntu, because when they release new versions of prerelease
# packages they delete the old ones from the release repositories, so
# you can't just use apt or apt-get to download and install the old
# version. This script's "secret sauce" is fetching the old versions
# from Launchpad instead of relying on them being in the apt
# repository.
# N.B. I don't know if old package builds that aren't actually in a
# final release stay in Launchpad forever or eventually get reaped,
# and if the latter than how long they are saved before being reaped.
# Therefore, it's possible that depending on how long after an upgrade
# you run the script, it won't be able to fetch all the old packages
# to downgrade.
# This script was written by Jonathan Kamens <>. I
# release it into the public domain. Feel free to do whatever you want
# with it.
whoami="$(basename "$0")"
usage="Usage: $whoami [--help] [--download-only] [--package-dir directory]
[--force] [--log-file dpkg-log]
--download-only Download packages without installing them
--package-dir directory Store packages in specified directory
(default $pkg_dir)
--force Install packages even if some can't be downloaded
--log-file dpkg-log Read specified log file instead of stdin"
while [ -n "$1" ]; do
case "$1" in
--help) echo "$usage"; exit ;;
--download-only) shift; download_only=true ;;
shift; shift
if [ ! -d "$pkg_dir" ]; then
echo "'$pkg_dir' is not a directory" 1>&2
echo "$usage" 1>&2
exit 1
--force) shift; force=true ;;
shift; shift
if [ ! -f "$log_file" ]; then
echo "'$log_file' is not a regular file" 1>&2
echo "$usage" 1>&2
exit 1
echo "Unrecognized command-line argument '$1'" 1>&2
echo "$usage" 1>&2
exit 1
if touch $pkg_dir/writable.$$ 2>/dev/null; then
rm -f $pkg_dir/writable.$$
echo "Can't write to '$pkg_dir'" 1>&2
exit 1
while read log_line; do
set $log_line
shift # date
shift # time
if [ "$1" != "upgrade" ]; then
shift # action
shift # pkg_name:arch
if [ -f "$pkg_dir/$deb_name" ]; then
echo "Already have $deb_name"
if ! curl --location --silent --output "$pkg_dir/$deb_name" \
"$launchpad_base/$deb_name"; then
echo "Failed to download $deb_name" 1>&2
if ! $force; then
exit 1
echo "Downloaded $deb_name"
to_install="$to_install $pkg_dir/$deb_name"
done < <(cat $log_file)
if ! $download_only; then
echo "Installing..."
apt-get install $to_install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment