Backup and Restore Linux Packages and PPAs

Want to keep a record of all packages installed on your server or desktop? Follow these simple steps to create an export script, designed for Debian-based Linux systems like Ubuntu and Linux Mint. Please note that this works only for the packages installed using apt-get.

Export Script

This does two main things:
Step 1: Saves the list of all currently installed packages in a separate text file.

Step 2: Saves the custom PPA lists that you might have used, along with third-party non-PPA lists and their corresponding keys.

One needs to execute this in the source system. The script will create a folder ‘apt-backup’ in the home directory.

#!/bin/bash
set -e

OUT="$HOME/apt-backup"
UBUNTU_CODENAME=$(lsb_release -sc)
MINT_VERSION=$(lsb_release -rs)

echo "Creating backup in $OUT"
mkdir -p "$OUT"

echo "Saving system info..."
cat <<EOF > "$OUT/system-info.txt"
Mint version: $MINT_VERSION
Ubuntu codename: $UBUNTU_CODENAME
EOF

echo "Saving manually installed packages..."
apt-mark showmanual | sort > "$OUT/manual-packages.txt"

echo "Saving PPA list..."
grep -R "^deb " /etc/apt/sources.list.d \
  | grep -v "official-package-repositories" \
  | awk '{print $2}' \
  | sort -u > "$OUT/ppa-urls.txt"

echo "Saving third-party non-PPA repos..."
grep -R "^deb " /etc/apt/sources.list \
  | grep -v "linuxmint" \
  | grep -v "ubuntu.com" \
  | awk '{print $2}' \
  | sort -u > "$OUT/thirdparty-repos.txt"

echo "Saving trusted keys (reference only)..."
apt-key list > "$OUT/apt-keys.txt" 2>/dev/null || true

echo "Backup complete."
echo "Location: $OUT"
Import Script

Using the files generated by the export script, the import script below will restore the PPAs and install the missing packages in the target system. One needs to execute this on the target system. Before running this, make sure you have placed the apt-backup folder(generated from Export script) in the home location.

#!/bin/bash
set -e

BACKUP="$HOME/apt-backup"

if [ ! -d "$BACKUP" ]; then
  echo "Backup directory not found!"
  exit 1
fi

echo "=== System compatibility check ==="
cat "$BACKUP/system-info.txt"
echo
read -p "Ensure Mint & Ubuntu base match. Continue? (y/N): " CONFIRM
[[ "$CONFIRM" == "y" ]] || exit 1

echo "=== Updating base system ==="
sudo apt update
sudo apt upgrade -y

echo "=== Ensuring add-apt-repository exists ==="
sudo apt install -y software-properties-common

echo "=== Restoring PPAs ==="
if [ -f "$BACKUP/ppa-urls.txt" ]; then
  while read -r PPA; do
    sudo add-apt-repository -y "$PPA"
  done < "$BACKUP/ppa-urls.txt"
fi

echo "=== Updating after PPAs ==="
sudo apt update

echo "=== Computing package difference ==="

# Packages already manually installed on target
apt-mark showmanual | sort > /tmp/target-manual.txt

# Packages you want restored (already filtered!)
sort "$BACKUP/user-manual.txt" > /tmp/source-manual.txt

# Compute missing packages
comm -23 /tmp/source-manual.txt /tmp/target-manual.txt > /tmp/to-install.txt

if [ ! -s /tmp/to-install.txt ]; then
  echo "No new packages to install. System already matches."
  exit 0
fi

echo "Packages to be installed:"
cat /tmp/to-install.txt
echo

read -p "Proceed with installation? (y/N): " INSTALL_CONFIRM
[[ "$INSTALL_CONFIRM" == "y" ]] || exit 1

echo "=== Installing missing packages ==="
sudo apt install -y $(cat /tmp/to-install.txt)

echo "=== Fixing dependencies & cleanup ==="
sudo apt -f install -y
sudo apt autoremove -y

echo "=== Restore complete ==="
echo "Reboot recommended."

Leave a Reply

Your email address will not be published. Required fields are marked *