Skip to content

Instantly share code, notes, and snippets.

@batden
Last active May 18, 2024 13:58
Show Gist options
  • Save batden/993b5ee997b3df2c3b075907a1dff116 to your computer and use it in GitHub Desktop.
Save batden/993b5ee997b3df2c3b075907a1dff116 to your computer and use it in GitHub Desktop.
Backup-installed-packages
#!/bin/bash
# Usage:
# chmod +x pbackup.sh
# ./pbackup.sh
DISTRO=$(lsb_release -sc)
DATE=$(date +%Y-%m-%d)
# Clean up the system.
if [ $DISTRO == jammy ]; then
sudo apt autoremove --purge
fi
# Backup list of installed DEB packages.
echo
apt-cache dumpavail >/tmp/apt-avail
sudo dpkg --merge-avail /tmp/apt-avail
rm /tmp/apt-avail
dpkg --get-selections >$HOME/installed_pkgs_$DATE.log
echo
# Backup list of available repositories.
if [ $DISTRO == jammy ]; then
grep -Erh ^deb /etc/apt/sources.list* >$HOME/available_repos_$DATE.txt
fi
# Backup list of installed snap packages.
snap list --all >$HOME/installed_snaps_$DATE.txt
# Backup list of manually installed DEB packages.
if [ $DISTRO == jammy ]; then
echo $(comm -23 <(apt-mark showmanual |
sort -u) <(gzip -dc /var/log/installer/initial-status.gz |
sed -n 's/^Package: //p' | sort -u)) >$HOME/manually_installed_pkgs_$DATE.txt
else
apt-mark showmanual >$HOME/manually_installed_pkgs_$DATE.txt
fi
# Backup list of installed packages covered by the Expanded Security Maintenance updates.
if /usr/bin/pro security-status --format json 2>/dev/null |
sed -n '/"This machine is attached to an Ubuntu Pro subscription."/p'; then
/usr/bin/pro security-status --esm-apps 2>/dev/null >$HOME/pro_security_pkgs_$DATE.txt
fi
echo "Way to go!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment