Sept. 11, 2009, 2:05 p.m.
posted by void
Clone an Installation
Export a list of installed packages on one Ubuntu system, and import them into another to build a duplicate system. We've installed many different Debian-based distributions over the years, and one thing we've found handy to have is a complete list of packages you have installed. If you want to create a system that is similar to a different system you have already set up, it can be difficult to remember each and every package you had installed. In this hack, we cover a method to export your current package list and import it into a new system.
Export the List of Installed PackagesThe first step in cloning an installation is to grab the complete list of installed packages from the first system. To do so, you basically instruct dpkg to dump its entire list of packages, filter out any packages that aren't currently installed, and then redirect the output to a file: $ sudo dpkg --get-selections | grep '[[:space:]]install$' | \\
awk '{print $1}' > package_list
Next, copy this text file to the destination system over the network, via a USB key or whatever method you prefer. You may also want to copy over the /etc/apt/sources.list file from the base system, since the new system may not have all of the same repositories enabled (if the repositories aren't the same, the destination system may not be able to find some of the packages in the list). Prepare the Destination SystemNow you need to prepare the destination system. If both systems are running the same release of Ubuntu, this may be as simple as just copying the initial system's /etc/apt/sources.list on top of the one on the current system. Otherwise, if the systems are from different releases, you will want to compare the initial system's sources.list with the /etc/apt/sources.list file on your destination system and see if there are any extra third-party repositories or repository subcategories that need to be enabled. (Read "Modify the List of Package Repositories" [Hack #60] for more information on Ubuntu's repositories and how to edit sources.list.) Once your sources.list file is settled, update your package list to make sure you get the latest version of the packages: $ sudo apt-get update
Import the Package ListTo import the package list, pipe the entire list to xargs, which then splits it into manageable chunks for the apt-get command: $ cat package_list | xargs sudo apt-get install
If you are migrating to a different Ubuntu release, this will require a bit of trial and error, since you will likely get complaints about packages no longer existing. The simple fix for this is to edit the package list and remove the package from the list, save your changes, and then rerun the command. You will likely need to do this a number of times, but eventually you will have a list of valid packages. A more complicated but thorough fix would involve checking the latest list of packages for a potential update or replacement for the package that no longer exists and installing them one by one. Once apt-get completes, you are finished. All of the files from the package list will have been imported into the new system. Now keep in mind that this doesn't mean that all of the settings have transferred over. To do that, you will likely need to copy settings from the /etc directory or possibly other directories, depending on the package. Hacking the HackIt can be rather handy to have a complete list of installed packages for backup and restore purposes. An easy way to automatically generate a complete list is to have a cron job update the list of installed packages every night. To do so, create the following script and save it in /etc/cron.daily/package_list: $!/bin/sh
dpkg --get-selections | grep '[[:space:]]install$' | \\
awk '{print $1}' > /etc/package_listNow make the script executable: $ sudo chmod a+x /etc/cron.daily/package_list
This /etc/package_list file can then be backed up [Hack #79] with the rest of your system settings. |
- Comment
