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() { hook_before() {
if ! is_bin_in_path aptitude; then #if ! is_bin_in_path aptitude; then
echo "aptitude needs to be installed for backups to work properly." # echo "aptitude needs to be installed for backups to work properly."
exit 1 # exit 1
fi #fi
if ! is_bin_in_path rsync; then if ! is_bin_in_path rsync; then
echo "rsync needs to be installed for backups to work properly." echo "rsync needs to be installed for backups to work properly."
@ -21,7 +21,8 @@ hook_before() {
pushd "$restoreDir" || exit 2 pushd "$restoreDir" || exit 2
dpkg --get-selections > Package.list 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 #apt-key exportall > /etc/restore/Repo.keys
cp -a /etc/apt/sources.list "$restoreDir/" cp -a /etc/apt/sources.list "$restoreDir/"
rsync -avhHi /etc/apt/sources.list.d "$restoreDir/" rsync -avhHi /etc/apt/sources.list.d "$restoreDir/"
@ -40,6 +41,13 @@ if [[ ! -d "trusted.gpg.d" ]]; then
exit 1 exit 1
fi 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 #apt-key add /etc/restore/Repo.keys
#dpkg --set-selections < /etc/restore/Package.list #dpkg --set-selections < /etc/restore/Package.list
#apt-get dselect-upgrade #apt-get dselect-upgrade
@ -48,9 +56,8 @@ install=""
echo " * Installing required packages" echo " * Installing required packages"
dpkg-query -s 'rsync' &>/dev/null || install+=" rsync" 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 'borgbackup' &>/dev/null || install+=" borgbackup" #dpkg-query -s 'borgmatic' &>/dev/null || install+=" borgmatic"
dpkg-query -s 'borgmatic' &>/dev/null || install+=" borgmatic"
dpkg-query -s 'apt-transport-https' &>/dev/null || install+=" apt-transport-https" dpkg-query -s 'apt-transport-https' &>/dev/null || install+=" apt-transport-https"
if [[ -n "\$install" ]]; then if [[ -n "\$install" ]]; then
@ -60,50 +67,58 @@ fi
echo " * Enabling 32-bit packages" echo " * Enabling 32-bit packages"
grep ':i386' InstallOnly.list &>/dev/null && dpkg --add-architecture i386 grep ':i386' InstallOnly.list &>/dev/null && dpkg --add-architecture i386
echo " * Restoring repositories and keys" echo " * Checking for flatpak"
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 if [[ -r "sources.list" ]]; then
echo
failinstall="" echo "=============================="
flatpak=0 echo "INSTALL REPOS"
echo "=============================="
echo " * Gathering installed packages" echo
#xargs aptitude --schedule-only install < InstallOnly.list read -p "Do you want to continue to install backup repositories? [Y/n] " -n 1 -sr promptRepos
while read p; do if [[ "\$promptRepos" =~ ^[Yy]$ ]]; then
if [[ "\$p" = "flatpak" ]]; then echo "Yes"
flatpak=1 cp -a sources.list /etc/apt/sources.list
fi rsync --ignore-existing -raz sources.list.d/ /etc/apt/sources.list.d/
rsync --ignore-existing -raz trusted.gpg.d/ /etc/apt/trusted.gpg.d/
dpkg-query -s "\$p" &>/dev/null [[ -d keyrings ]] && rsync --ignore-existing -raz keyrings/ /etc/apt/keyrings/
if [[ "\$?" -eq 1 ]]; then apt update
aptitude --schedule-only install "\$p"
if [[ "\$?" -ne 0 ]]; then
if [[ -z "\$failinstall" ]]; then
failinstall+="\$p"
else else
failinstall+=", \$p" echo -e "Cancelled\n"
fi fi
fi fi
fi
done < InstallOnly.list
echo "Packages that failed to schedule for install:"
echo "\$failinstall"
echo " * Restoring installed packages (please confirm)"
aptitude install
flatpak=0
grep 'flatpak' InstallOnly.list &>/dev/null && flatpak=1
if [[ "\$flatpak" -eq 1 ]]; then if [[ "\$flatpak" -eq 1 ]]; then
echo " * Adding flatpak repo: Flathub" echo " * Adding flatpak repo: Flathub"
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
fi 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 "Packages that failed to schedule for install:"
echo "\$failinstall" cat "\${TMPDIR}/diff.fail"
EOF EOF
chmod ug+rx restore.sh chmod ug+rx restore.sh
popd || exit 2 popd || exit 2