13

I just add some problems with my Debian virtual servers and I add to reinstall all of them from scratch. However, I still had access to the old version and I could retrieve the list of installed packages.

In order to facilitate the process of restoring a virtual server in the future, I would like to create a list of specific packages for each one of my server.

To better explain what I want to achieve. I already have an automated process to create a new virtual server with some basic package and configuration used everywhere. Now I want to save the delta with this "skeleton" to ease the reinstallation of a particular server.

A real plus will be to also save the changed configuration files from the default, but I can live with only the package list.

In short, I want a way to create a list of packages installed on a host but not on another.

It will be really great if the list contains only manually installed packages and not the list of all dependencies.

If you have some existing tools which are designed to achieve this particular task, feel free to propose them, but I want to keep the dependency as small as possible. For information, they're not exactly virtual servers, but LXC containers.

krtek
  • 583
  • 4
  • 8
  • Just a thought, but it might be time to start working at this from the opposite direction. Instead of trying to pull the information, why to setup a configuration management system (puppet). Then push things out to your managed systems. – Zoredache Nov 07 '11 at 21:26
  • Oh, BTW, do see this question. http://serverfault.com/questions/3852/what-tool-do-you-recommend-to-track-changes-on-a-linux-unix-server – Zoredache Nov 07 '11 at 21:27
  • @Zoredache I'm currently looking in some better way to manage all this, but in the meantime I wanted to find some quick way to save the state of the servers just in case. Thanks for the link ! – krtek Nov 07 '11 at 21:37

5 Answers5

13

On the reference installation (only once):

dpkg-query -W -f='${Package}\n' | sort > baselist.txt

(The following assumes bash)

To get the packages added from the reference installation (this doesn't show what was removed):

comm -1 -3 baselist.txt <(dpkg-query -W -f='${Package}\n' | sort)

Even better, avoiding copy of baselist.txt:

comm -1 -3 <(ssh user@refserver cat /path/to/baselist.txt) <(dpkg-query -W -f='${Package}\n' | sort)
shellholic
  • 1,287
  • 9
  • 11
3

on old server, run:

dpkg --get-selections > old-packages-list

copy the above file to new server and run this on new server:

dpkg --set-selections < old-packages-list

apt-get update

dselect update

Also, remember to add all the extra repositories from /etc/apt/sources.list from old server to new server as well, before this migration of packages, as if you do not, then most packages will be left un-installed.

Farhan
  • 4,210
  • 9
  • 47
  • 76
  • This will save the entire list of package, and it is exactly the solution I used to restore the servers. But now, to ease the process in the future, I want to only save the list of packages that is different from the base server. – krtek Nov 07 '11 at 21:19
  • 1
    ok, that is very easy as well., first take backup of list from old server by "dpkg --get-selections > old-packages-list", then get list of packages on new server by "dpkg --get-selections > new-packages-list",,,run this command "cat old-packages-list | awk '{print $1}' > old-list",,,same command on new server, but replace old with new. run a diff on both files, which will show you the difference. by diff -w old-list new-list – Farhan Nov 07 '11 at 21:22
  • 1
    `cut -f1` will probablz be faster than awk, and like commented on Khaled answer, diff won't always returns a right result. The solution with `comm` really please for now... However I'm missing a version which lists only manually installed packages. – krtek Nov 07 '11 at 21:43
1

You can use the following command to get the full list of packages on both servers:

     dpkg -l | sort > old_file
     dpkg -l | sort > new_file

Then, you can get the differences using:

     diff -Nur old_file new_file > changes.txt

You can filter the needed changes using grep. Also, diff can be used to get the changes in configuration files and generate patches to be appåied when needed. By the way, diff can compare two folders not only two files.

Khaled
  • 35,688
  • 8
  • 69
  • 98
  • The problem with the diff output is that it can't directly be fed to apt-get to reinstall everything in an automatic way... And like proposed Shellholic, using comm is better in this particular case, diff might provide wrong results if the package list is really long. – krtek Nov 07 '11 at 21:41
  • Another problem is that dpkg autosizes the output's column. So a dpkg -l from a server with a longer package name make the diff hard. – Paolo Mar 17 '17 at 15:35
1

Blueprint is designed for exactly your need. It spits a description packages installed and config files that have been changed. It's able to convert these descriptions into Puppet or Chef scripts in order to apply them to clean boxes.

1

Also found this helpful package, the description of which is as follows:

A Bash script which compares the filesystem tree of a Debian package to the current filesystem tree, printing unified diffs for files that differ.

https://code.google.com/p/dpkg-diffs/

Just a single bash file you can stick in your PATH somewhere and run - seems to work for me and its not too old.

Hope this helps someone.

smaudet
  • 111
  • 1