Update: Debian Lenny 5.0 now has support for the option autoremove of apt-get that is able to clean all unused dependencies.

debian-backupCuriosity and laziness are the two most important reasons that my system ends up with lots of useless software after a couple months of usage. Many times I want to test drive a program and if it suits my needs I’ll keep it otherwise it will end up in /dev/null.

With apt-get magic it is amazingly easy to install lots of programs in seconds. The problem comes when you want to get rid of them because each program comes with a list of dependencies that get installed automatically for you. So you have to keep a list of the dependencies to be able to uninstall them when you don’t need the program anymore. I used to copy the dependencies from the apt-get confirmation screen to a text file so that I can remove them later. That didn’t work for me, thus I had to find a way to do it automatically.

I created two scripts that use Debian tools to create a checkpoint before I install a new program and then rollback to that state when I don’t need that program anymore. The first script is named dpkg-backup because it creates a backup copy of the installed packages list. Here it is:

#!/bin/sh
# dpkg-backup.sh

dpkg --get-selections > /tmp/dpkg-list.txt

The second script is named dpkg-restore because it restores the packages to the saved state. Source code:

#!/bin/sh
# dpkg-restore.sh

if [ -f /tmp/dpkg-list.txt ]
then
    /usr/bin/dpkg --clear-selections
    /usr/bin/dpkg --set-selections < /tmp/dpkg-list.txt
    /usr/bin/dpkg --get-selections | sed -e 's/deinstall/purge/' > /tmp/dpkg-list.txt
    /usr/bin/dpkg --set-selections < /tmp/dpkg-list.txt
    rm /tmp/dpkg-list.txt
    /usr/bin/apt-get dselect-upgrade
else
    echo "You must use dpkg-backup first."
fi


Usage
is pretty simple. All you have to do is run dpkg-backup.sh before you install a program with apt-get. Then use apt-get to install it and when you want to get rid of it use dpkg-restore.sh and everything associated with that program is gone for good :). An example usage scenario is provided below:

$ dpkg-backup.sh
$ apt-get install test-program
...
# After playing around with the program you realize that you don't need it anymore!
$ dpkg-restore.sh

That’s it! I hope you find these two scripts as useful as I did. By the way both are licensed as GPLv2 so feel free to modify and distribute. If you find any bugs or have any ideas/comments on how to improve them let me know!

pixelstats trackingpixel
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
If you enjoyed this post, make sure you subscribe to my RSS feed!
Leave a Reply