Restore Packages Using dselect-upgrade

It's always a good idea to backup important data. Your files and settings can easily be archived. But how can you backup & restore all applications that you've installed over the last couple of years? Here's an easy trick that works for both desktops & servers, and that can also be used to synchronize installed packages in a web cluster, making all the servers run the same software.

The method described in this article depends on the command apt-get, so it works on Debian & Ubuntu systems.__ This article does not describe a full backup & restore method, it's a trick to add to your existing backup procedure. Still, it's a trick that will really make your life easier.

APT packages

The basic idea is that we generate a list of all currently installed packages, keep it some place safe, and upon a reinstall, we can upload this list again and have the system install all the packages in this list automatically.

How to backup

So first we need to create a list of all the installed APT packages and save it in a file:

$ sudo dpkg --get-selections > /tmp/dpkglist.txt

That's it! The list is now stored in /tmp/dpkglist.txt. If you want you can add this command to your crontab and then just include the file /tmp/dpkglist.txt in your backup procedure so that it's safe and up to date at all times.

How to restore

Now if your system crashes (let's all hope it won't) and you need to reinstall, this will be the procedure:

  • install a fresh OS (of course)
  • restore the package list
  • restore your important files & settings

But how can can we restore the package list? Simple. Just copy your backed up dpkglist.txt file to your fresh system's /tmp directory again and execute the following:

$ sudo dpkg --set-selections < /tmp/dpkglist.txt
$ sudo apt-get -y update
$ sudo apt-get dselect-upgrade

Great! All of your apt packages have been restored!

(Don't worry! This method only adds and upgrades packages, it will not remove packages that do not exist in the list)

Additional trick: PEAR packages (web servers only)

The same method can be used to restore PEAR extensions. Though there aren't any standard tools that I know of, with a little creativity it's not so hard.

How to backup

This will generate a list of all installed PEAR packages and save it to a file:

$ sudo pear -q list | egrep 'alpha|beta|stable' |awk '{print $1}' > /tmp/pearlist.txt

That's it! A list of your installed PEAR packages is stored in the file /tmp/pearlist.txt. Now, if you want you can add this command to your crontab and then just include the file /tmp/pearlist.txt in your backup procedure so that it's safe and up to date at all times.

How to restore

To restore: make sure PEAR is installed, simply copy the pearlist.txt file back to your new system's /tmp directory and type:

cat /tmp/pearlist.txt |awk '{print "pear install -f "$0}' |sudo bash

Great! All of your PEAR packages have been restored!