borgmatic-base/scripts/os_debian

124 lines
3.3 KiB
Text
Raw Permalink Normal View History

2021-09-04 09:17:00 -04:00
#!/bin/bash
restoreDir="/etc/restore"
2023-09-17 16:43:29 -04:00
function is_bin_in_path {
builtin type -P "$1" &>/dev/null
}
2021-09-04 09:17:00 -04:00
hook_check() {
2023-09-17 16:43:29 -04:00
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
fi
2021-09-04 09:17:00 -04:00
}
hook_pre() {
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
2023-09-17 16:43:29 -04:00
cp -a /etc/apt/sources.list "$restoreDir/"
rsync -avhHi /etc/apt/sources.list.d "$restoreDir/"
rsync -avhHi /etc/apt/trusted.gpg.d "$restoreDir/"
2023-09-22 01:28:14 -04:00
[[ -d /etc/apt/keyrings ]] && rsync -avhHi /etc/apt/keyrings "$restoreDir/"
2021-09-04 09:17:00 -04:00
cat > restore.sh <<EOF
#!/bin/bash
if [[ ! -f "InstallOnly.list" ]]; then
echo "This needs to be run inside the restore directory."
exit 1
fi
if [[ ! -d "trusted.gpg.d" ]]; then
echo "This needs to be run inside the restore directory."
exit 1
fi
#apt-key add /etc/restore/Repo.keys
#dpkg --set-selections < /etc/restore/Package.list
#apt-get dselect-upgrade
install=""
echo " * Installing required packages"
2023-09-22 01:28:14 -04:00
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 'apt-transport-https' &>/dev/null || install+=" apt-transport-https"
2021-09-04 09:17:00 -04:00
if [[ -n "\$install" ]]; then
apt -y install \$install
fi
echo " * Enabling 32-bit packages"
2023-09-21 23:37:43 -04:00
grep ':i386' InstallOnly.list &>/dev/null && dpkg --add-architecture i386
echo " * Restoring repositories and keys"
2023-09-22 00:59:43 -04:00
cp -a sources.list /etc/apt/sources.list
2023-09-17 16:43:29 -04:00
rsync --ignore-existing -raz sources.list.d/ /etc/apt/sources.list.d/
2021-09-04 09:17:00 -04:00
rsync --ignore-existing -raz trusted.gpg.d/ /etc/apt/trusted.gpg.d/
2023-09-22 01:28:14 -04:00
[[ -d keyrings ]] && rsync --ignore-existing -raz keyrings/ /etc/apt/keyrings/
2023-09-22 00:59:43 -04:00
apt update
failinstall=""
flatpak=0
echo " * Gathering installed packages"
#xargs aptitude --schedule-only install < InstallOnly.list
while read p; do
2023-09-23 16:13:00 -04:00
if [[ "\$p" = "flatpak" ]]; then
flatpak=1
fi
2023-09-23 16:13:00 -04:00
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
2023-09-23 16:13:00 -04:00
failinstall+=", \$p"
fi
fi
fi
done < InstallOnly.list
echo "Packages that failed to schedule for install:"
2023-09-23 16:13:00 -04:00
echo "\$failinstall"
echo " * Restoring installed packages (please confirm)"
2021-09-04 09:17:00 -04:00
aptitude install
2023-09-23 16:27:23 -04:00
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:"
2023-09-23 16:13:00 -04:00
echo "\$failinstall"
2021-09-04 09:17:00 -04:00
EOF
2023-09-22 01:02:45 -04:00
chmod ug+rx restore.sh
2021-09-04 09:17:00 -04:00
popd || exit 2
}
hook_post() {
:
2021-09-04 09:17:00 -04:00
}
case "$1" in
before_check) hook_check || exit $?;;
before_backup) hook_pre || exit $?;;
after_backup) hook_post || exit $?;;
esac