Sync Installed Packages on Ubuntu Hosts

April 15, 2017

Here's how I've gone about synchronizing installed packages in Ubuntu 14.04 LTS across several hosts. What this means is it will attempt to mirror all installed packages installed on one host, to another host. There are two scripts - one to generate a packages.tar.gz containing everything necessary to transfer installed packages (not the actual packages themselves), and another script to apply the same installed packages on a different host.

Of course, this will likely only work running across the same versions of a Debian based distrobution. Meaning, I ran this on the same version of Ubuntu.

The first thing to do is run the following backup.sh.

backup.sh

#!/bin/sh
 
# packages                                                                      
dpkg --get-selections > Package.list
sudo cp -R /etc/apt/sources.list* .
sudo apt-key exportall > Repo.keys
tar cvfz packages.tar.gz Package.list sources.list* Repo.keys
sudo rm -rf Package.list Repo.keys sources.list*
./backup.sh

Once that is done, copy packages.tar.gz and the following restore.sh to new host.

restore.sh

#!/bin/sh
 
set -e
 
# packages
tar xvf packages.tar.gz
sudo apt-key add Repo.keys
sudo cp -R sources.list* /etc/apt/
sudo apt-get update
sudo apt-get install dselect
sudo dselect update
sudo dpkg --set-selections < Package.list
sudo apt-get dselect-upgrade -y
sudo rm -rf Repo.keys sources.list* Package.list
./restore.sh

This can take awhile to run, depending on how out of sync they are.

Related Posts