Update restore script to use apt-mark, apt-get, and comm

This commit is contained in:
Eric Renfro 2024-07-07 14:59:30 -04:00
parent deb8d825a8
commit 4274a52812
Signed by: psi-jack
SSH key fingerprint: SHA256:1TKB8Z257L8EHK8GWNxKgMhD8a+FAR+f+j3nnlcuNVM

View file

@ -7,10 +7,10 @@ function is_bin_in_path {
}
hook_before() {
if ! is_bin_in_path aptitude; then
echo "aptitude needs to be installed for backups to work properly."
exit 1
fi
#if ! is_bin_in_path aptitude; then
# echo "aptitude needs to be installed for backups to work properly."
# exit 1
#fi
if ! is_bin_in_path rsync; then
echo "rsync needs to be installed for backups to work properly."
@ -21,7 +21,8 @@ hook_before() {
pushd "$restoreDir" || exit 2
dpkg --get-selections > Package.list
aptitude search --disable-columns -F%p '~i!~M!~v' > InstallOnly.list
#aptitude search --disable-columns -F%p '~i!~M!~v' > InstallOnly.list
apt-mark showmanual | sort > InstallOnly.list
#apt-key exportall > /etc/restore/Repo.keys
cp -a /etc/apt/sources.list "$restoreDir/"
rsync -avhHi /etc/apt/sources.list.d "$restoreDir/"
@ -40,6 +41,13 @@ if [[ ! -d "trusted.gpg.d" ]]; then
exit 1
fi
TMPDIR="\$(mktemp -d -t restore-XXXX)"
cleanup() {
[[ -n "\$TMPDIR" && -d "\$TMPDIR" ]] && rm -rf "\$TMPDIR"
}
trap cleanup EXIT
#apt-key add /etc/restore/Repo.keys
#dpkg --set-selections < /etc/restore/Package.list
#apt-get dselect-upgrade
@ -48,9 +56,8 @@ install=""
echo " * Installing required packages"
dpkg-query -s 'rsync' &>/dev/null || install+=" rsync"
dpkg-query -s 'aptitude' &>/dev/null || install+=" aptitude"
dpkg-query -s 'borgbackup' &>/dev/null || install+=" borgbackup"
dpkg-query -s 'borgmatic' &>/dev/null || install+=" borgmatic"
#dpkg-query -s 'borgbackup' &>/dev/null || install+=" borgbackup"
#dpkg-query -s 'borgmatic' &>/dev/null || install+=" borgmatic"
dpkg-query -s 'apt-transport-https' &>/dev/null || install+=" apt-transport-https"
if [[ -n "\$install" ]]; then
@ -60,50 +67,58 @@ fi
echo " * Enabling 32-bit packages"
grep ':i386' InstallOnly.list &>/dev/null && dpkg --add-architecture i386
echo " * Restoring repositories and keys"
cp -a sources.list /etc/apt/sources.list
rsync --ignore-existing -raz sources.list.d/ /etc/apt/sources.list.d/
rsync --ignore-existing -raz trusted.gpg.d/ /etc/apt/trusted.gpg.d/
[[ -d keyrings ]] && rsync --ignore-existing -raz keyrings/ /etc/apt/keyrings/
echo " * Checking for flatpak"
apt update
if [[ -r "sources.list" ]]; then
echo
echo "=============================="
echo "INSTALL REPOS"
echo "=============================="
echo
read -p "Do you want to continue to install backup repositories? [Y/n] " -n 1 -sr promptRepos
if [[ "\$promptRepos" =~ ^[Yy]$ ]]; then
echo "Yes"
cp -a sources.list /etc/apt/sources.list
rsync --ignore-existing -raz sources.list.d/ /etc/apt/sources.list.d/
rsync --ignore-existing -raz trusted.gpg.d/ /etc/apt/trusted.gpg.d/
[[ -d keyrings ]] && rsync --ignore-existing -raz keyrings/ /etc/apt/keyrings/
apt update
else
echo -e "Cancelled\n"
fi
fi
failinstall=""
flatpak=0
echo " * Gathering installed packages"
#xargs aptitude --schedule-only install < InstallOnly.list
while read p; do
if [[ "\$p" = "flatpak" ]]; then
flatpak=1
fi
dpkg-query -s "\$p" &>/dev/null
if [[ "\$?" -eq 1 ]]; then
aptitude --schedule-only install "\$p"
if [[ "\$?" -ne 0 ]]; then
if [[ -z "\$failinstall" ]]; then
failinstall+="\$p"
else
failinstall+=", \$p"
fi
fi
fi
done < InstallOnly.list
echo "Packages that failed to schedule for install:"
echo "\$failinstall"
echo " * Restoring installed packages (please confirm)"
aptitude install
grep 'flatpak' InstallOnly.list &>/dev/null && flatpak=1
if [[ "\$flatpak" -eq 1 ]]; then
echo " * Adding flatpak repo: Flathub"
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
fi
echo
echo "=============================="
echo "INSTALL PACKAGES"
echo "=============================="
echo
read -p "About to install the system packages per the backup. Do you want to continue? [Y/n] " -n 1 -sr promptPkgs
if [[ "\$promptPkgs" =~ ^[Yy] ]]; then
echo "Yes"
comm --nocheck-order -23 InstallOnly.list <(apt-mark showmanual|sort) grep -Ev 'linux-image|linux-headers' > "\${TMPDIR}/diff.list"
apt-get --simulate install \$(cat diff.list) |& awk '/^E: Unable to locate package / {print \$NF}' | sort > "\${TMPDIR}/diff.fail"
comm --nocheck-order -23 "\${TMPDIR}/diff.list" "\${TMPDIR}/diff.fail" | xargs apt-get install
read -p "Did everything above look okay and do you want to proceed? [Y/n] " -n 1 -sr promptPkgsDo
if [[ "\$promptPkgsDo" =~ ^[Yy] ]]; then
comm --nocheck-order -23 "\${TMPDIR}/diff.list" "\${TMPDIR}/diff.fail" | xargs apt-get install
else
echo -e "Cancelled\n"
fi
else
echo -e "Cancelled\n"
fi
echo "Packages that failed to schedule for install:"
echo "\$failinstall"
cat "\${TMPDIR}/diff.fail"
EOF
chmod ug+rx restore.sh
popd || exit 2