Backing up and restoring Debian packages

As you maintain your server, your list of installed packages will grow. If, for some reason, you needed to rebuild your server, you would need to reproduce exactly what you had installed before, which can be a pain. It's always recommended that you document all changes made to your server via a change control process, but, at the very least, keeping track of which packages are installed is an absolute must. In some cases, a server may only include one or two extra packages in order to meet its goal, but, in other cases, you may need an exact combination of software and libraries in order to get things working like they were. Thankfully, the dpkg command allows us to export and import a list of packages to install.

To export a list of installed packages, we can use the following command:

dpkg --get-selections > packages.list 

This command will dump a list of package selections to a standard text file. If you open it, you'll see a list of your installed packages, one per line. A typical line within the exported file will look similar to the following:

tmux install 

With this list, we can import our selections back into the server if we need to reinstall Ubuntu Server, or into a new server that will serve a similar purpose. First, before we manage any packages, we should update our index:

sudo apt update 

Next, we'll need to ensure we have the dselect package installed. At your shell prompt, type which dselect and you should see output similar to the following:

/usr/bin/dselect 

If you don't see the output, you'll need to install the dselect package with apt:

sudo apt install dselect 

Once that's complete, you can now import your previously saved package list, and have the missing packages reinstalled on your server. The following commands will complete the process:

sudo dselect update 
sudo dpkg --set-selections < packages.list 
sudo apt-get dselect-upgrade 

Normally, we simply use apt instead of apt-get nowadays, but some commands, such as dselect-upgrade, only work with apt-get.

After you have run those commands, the packages that are contained in your packages list, but aren't already installed, will be installed once you confirm the changes. This method allows you to easily restore the packages previously installed on your server, if for some reason you need to rebuild it, as well as setting up a new server to be configured in a similar way to an existing one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.161.225