diff --git a/scripts/os b/scripts/os index 8f8f520..9df90ad 100755 --- a/scripts/os +++ b/scripts/os @@ -12,14 +12,14 @@ fi createRestoreDir() { if [[ -d "${restoreDir}" ]]; then - rm -rf "${restoreDir}/*" || exit 2 + rm -rf "${restoreDir:?}/*" || exit 2 else mkdir -p "${restoreDir}" || exit 2 fi } checkOS() { - if [[ "$kernel" == "Darin" ]]; then + if [[ "$kernel" == "Darwin" ]]; then return 0 elif [[ "$kernel" == "Linux" ]]; then if [[ -f /etc/os-release ]]; then @@ -27,7 +27,7 @@ checkOS() { DISTRO="$ID" DISTRO_CURRENT="$DISTRO" if [[ -n "$ID_LIKE" ]]; then - DISTRO="$ID_LIKE" + DISTRO="${ID_LIKE%% *}" fi return 0 else @@ -45,9 +45,9 @@ runOsHook() { "${scriptPath}/os_macos" "$hook" elif [[ "$kernel" == "Linux" ]]; then case "$DISTRO" in - debian) "${scriptPath}/os_debian" "$hook";; + debian|ubuntu) "${scriptPath}/os_debian" "$hook";; fedora) "${scriptPath}/os_fedora" "$hook";; - garuda) "${scriptPath}/os_garuda" "$hook";; + arch) "${scriptPath}/os_arch" "$hook";; solus) "${scriptPath}/os_solus" "$hook";; opensuse-leap) "${scriptPath}/os_suse" "$hook";; opensuse-tumbleweed) "${scriptPath}/os_suse" "$hook";; @@ -62,10 +62,10 @@ runOsHook() { hook_before() { checkOS || exit 200 - pushd "$scriptPath" &>/dev/null + pushd "$scriptPath" &>/dev/null || exit 201 git checkout -- . git pull - popd &>/dev/null + popd &>/dev/null || exit 201 createRestoreDir || exit $? runOsHook before diff --git a/scripts/os_garuda b/scripts/os_arch similarity index 100% rename from scripts/os_garuda rename to scripts/os_arch diff --git a/scripts/os_debian b/scripts/os_debian index 8ea880d..3dfe0f1 100755 --- a/scripts/os_debian +++ b/scripts/os_debian @@ -7,11 +7,6 @@ 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 rsync; then echo "rsync needs to be installed for backups to work properly." exit 1 @@ -20,9 +15,8 @@ hook_before() { mkdir -p "$restoreDir" || exit 1 pushd "$restoreDir" || exit 2 - dpkg --get-selections > Package.list - aptitude search --disable-columns -F%p '~i!~M!~v' > InstallOnly.list - #apt-key exportall > /etc/restore/Repo.keys + dpkg --get-selections | sort > Package.list + apt-mark showmanual | sort > InstallOnly.list cp -a /etc/apt/sources.list "$restoreDir/" rsync -avhHi /etc/apt/sources.list.d "$restoreDir/" rsync -avhHi /etc/apt/trusted.gpg.d "$restoreDir/" @@ -40,70 +34,97 @@ if [[ ! -d "trusted.gpg.d" ]]; then exit 1 fi +TMPDIR="\$(mktemp -d -t restore-XXXX)" +bold="$(tput bold)" +normal="$(tput sgr0)" + +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 install="" -echo " * Installing required packages" +echo "\${bold} * Installing required packages\${normal}" 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 apt -y install \$install fi -echo " * Enabling 32-bit packages" +echo "\${bold} * Enabling 32-bit packages\${normal}" 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/ - -apt update - -failinstall="" +echo "\${bold} * Checking for flatpak\${normal}" 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 "Packages that failed to schedule for install:" -echo "\$failinstall" +if [[ -r "sources.list" ]]; then + echo "\${bold}" + echo "==============================" + echo "INSTALL REPOS" + echo "==============================" + echo "\${normal}" + + read -p "\${bold}Do you want to continue to install backup repositories? [Y/n]\${normal} " -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 + +echo "\${bold}" +echo "==============================" +echo "INSTALL PACKAGES" +echo "==============================" +echo "\${normal}" + +read -p "\${bold}About to install the system packages per the backup. Do you want to continue? [Y/n]\${normal} " -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 "\${TMPDIR}/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 + + echo + echo "Packages that were omitted because they could not be found:" + cat "\${TMPDIR}/diff.fail" | tr '\n' ' ' | fold -s + echo + echo + + read -p "\${bold}Did everything above look okay and do you want to proceed? [Y/n]\${normal} " -n 1 -sr promptPkgsDo + if [[ "\$promptPkgsDo" =~ ^[Yy] ]]; then + comm --nocheck-order -23 "\${TMPDIR}/diff.list" "\${TMPDIR}/diff.fail" | xargs apt-get install + + echo + echo + echo "\${bold}Packages that failed to schedule for install:\${normal}" + cat "\${TMPDIR}/diff.fail" | tr '\n' ' ' | fold -s + echo + echo + else + echo -e "Cancelled\n" + fi +else + echo -e "Cancelled\n" +fi EOF chmod ug+rx restore.sh popd || exit 2