Step 1: Gather details on packages installed on the source machine

To get the list of all packages installed on the machine. The following command can be used. This one line script will get all the installed package and store them into a file.

sudo dpkg --get-selections | sed "s/.*deinstall//" | sed "s/install$//g" | sed  "s/ *$//" | sort -u > ~/packagelist

To get the list of packages that are manually installed, the following command can be used.

comm -23 <(aptitude search '~i !~M' -F '%p' | sed  "s/ *$//" | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p'  |sort -u) |  sed -e '/^linux\-headers\-*/d' -e '/^linux\-image\-*/d' > ~/manualpackagelist

The above commands works by

  1. Get the list of packages that are not installed as dependency using aptitude.
  2. Get the list of packages installed right after a fresh install which can be read from initial-status.gz log.
  3. Compare the two lists to identify the packages that are manually installed by comparing and extracting lines from the result of aptitude command.

Alternative command for getting manually installed packages using apt-mark

comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u) > ~/manualpackagelist

Step 2: Copy configurations, home folder and package repositories.

  • Copy the package manually added repository source list. The source list is present under /etc/apt/source.list.d in Ubuntu. The source list can either be manually copied or use y-ppa-manager to backup and import source list. Detailed installation instruction can be found at [ppa manager installation].
  • Copy all the application specific configuration. Usually these configurations are in hidden directories under user home directory.
  • Copy the ssh keys which would be under /home/user/.ssh directory.
  • Make sure you copy any customized configuration under /etc like apache config or fail2ban config etc.
  • Export the repository key.
sudo apt-key exportall > ~/Repository.keys

The key can be imported back into the system using

sudo apt-key add ~/Repository.keys

Step 3: Installing packages on target machine.

The package list file must be copied across to the target machine using rsync or scp and then use the apt-get command to install the packages.

sudo aptitude update && cat pkglist | xargs sudo aptitude install -y

To install packages from the package list file and to verify package whether it is already installed before installing, the following script can be used.