Going Backwards

Yesterday I wrote about how I made a mistake by updating my primary Ubuntu computer to include the proposed pocket. I shouldn’t have done this. So today I quickly hacked together a script to take any packages which came from proposed and “downgrade” them back to the release pocket. It’s not pretty, but it worked, for me.

#!/bin/bash

TMPDIR=$(mktemp -d)
PACKAGES=$TMPDIR/packages
DOWNGRADE=$TMPDIR/downgrade

# Get list of all installed packages
dpkg -l | grep ^ii | awk -F ' ' '{ print $2}' > $PACKAGES

# Start the downgrade script
echo  "sudo apt install \\" > $DOWNGRADE

# For each package in the list of installed packages
while read p; do
  # Get the summary of where the package came from
  apt-cache policy $p > $TMPDIR/$p
  # Get the line after (grep -A 1 and tail -n 1) the highlighted one with 3 stars
  SOURCE=$(grep -A 1 "^\ *\*\*" $TMPDIR/$p | tail -n 1 | awk -F ' ' '{ print $3}' )
  # If that line suggests we got the package from proposed, add it to the script
  if [[ "$SOURCE" == *"hirsute-proposed"* ]]; then
    echo "$p/hirsute \\" >> $DOWNGRADE
  fi
done <$PACKAGES
# Tell the user what to run to actually do the downgrade
echo "Run sh $DOWNGRADE"

Don’t use this. As they say on YouTube, this script is for educational purposes only. There’s probably a thousand ways to make this more elegantly. Indeed I tried a suggestion on AskUbuntu which didn’t work at all.

[Read More]