From 5bf03bc0a49e230b5d4cefb4be867d0b2ac98546 Mon Sep 17 00:00:00 2001 From: Eric Renfro Date: Sat, 4 Sep 2021 09:17:00 -0400 Subject: [PATCH] Added new os style hooked agents --- scripts/gitea | 29 ++++++++++++++++++ scripts/os | 75 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/os_debian | 65 ++++++++++++++++++++++++++++++++++++++++ scripts/os_fedora | 41 ++++++++++++++++++++++++++ scripts/os_macos | 42 ++++++++++++++++++++++++++ scripts/os_solus | 29 ++++++++++++++++++ scripts/os_suse | 41 ++++++++++++++++++++++++++ 7 files changed, 322 insertions(+) create mode 100644 scripts/gitea create mode 100755 scripts/os create mode 100644 scripts/os_debian create mode 100644 scripts/os_fedora create mode 100644 scripts/os_macos create mode 100644 scripts/os_solus create mode 100644 scripts/os_suse diff --git a/scripts/gitea b/scripts/gitea new file mode 100644 index 0000000..d423d2d --- /dev/null +++ b/scripts/gitea @@ -0,0 +1,29 @@ +#!/bin/bash + +restoreDir=/etc/restore +giteaDir="$restoreDir/gitea" + + +hook_check() { + if [[ -d "$giteaDir" ]]; then + rm -rf "$giteaDir" || exit 1 + fi +} + +hook_pre() { + backupDate=$(date +"%Y-%m-%d") + giteaHome=$(getent passwd git | cut -f6 -d:) + + mkdir -p "$giteaDir" + sudo -u git "${giteaHome}/bin/gitea" dump --type tar.xz --file - > "${giteaDir}/gitea-dump-${backupDate}.tar.xz" +} + +hook_post() { + rm -rf "$giteaDir" || exit 1 +} + +case "$1" in + before_check) hook_check;; + before_backup) hook_pre;; + after_backup) hook_post;; +esac diff --git a/scripts/os b/scripts/os new file mode 100755 index 0000000..f015e7c --- /dev/null +++ b/scripts/os @@ -0,0 +1,75 @@ +#!/bin/bash + +restoreDir="/etc/restore" + +kernel="$(uname -s)" +scriptPath="$(dirname "$(readlink -f "$0")")" + + +if [[ "$kernel" == "Darwin" ]]; then + restoreDir="${HOME}/restore" +fi + +createRestoreDir() { + if [[ -d "${restoreDir}" ]]; then + rm -rf "${restoreDir}" || exit 2 + fi + mkdir -p "${restoreDir}" || exit 2 +} + +checkOS() { + if [[ "$kernel" == "Darin" ]]; then + return 0 + elif [[ "$kernel" == "Linux" ]]; then + if [[ -f /etc/os-release ]]; then + source /etc/os-release + return 0 + else + echo "Unknown Linux Distribution" + return 1 + fi + fi + return 1 +} + +runOsHook() { + local hook="$1" + + if [[ "$kernel" == "Darwin" ]]; then + "${scriptPath}/os_macos" "$hook" + elif [[ "$kernel" == "Linux" ]]; then + case "$ID" in + debian) "${scriptPath}/os_debian" "$hook";; + fedora) "${scriptPath}/os_fedora" "$hook";; + solus) "${scriptPath}/os_solus" "$hook";; + opensuse-leap) "${scriptPath}/os_suse" "$hook";; + opensuse-tumbleweed) "${scriptPath}/os_suse" "$hook";; + esac + fi + return $? +} + + +hook_check() { + checkOS || exit 200 + + runOsHook before_check +} + +hook_pre() { + hook_check || exit 200 + + createRestoreDir || exit $? + runOsHook before_backup +} + +hook_post() { + rm -rf "$restoreDir" +} + + +case "$1" in + before_check) hook_check || exit $?;; + before_backup) hook_pre || exit $?;; + after_backup) hook_post || exit $?;; +esac diff --git a/scripts/os_debian b/scripts/os_debian new file mode 100644 index 0000000..d194f71 --- /dev/null +++ b/scripts/os_debian @@ -0,0 +1,65 @@ +#!/bin/bash + +restoreDir="/etc/restore" + + +hook_check() { + if [[ -d "$restoreDir" ]]; then + rm -rf "$restoreDir" || exit 1 + fi +} + +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 + rsync -avhHi /etc/apt/trusted.gpg.d /etc/restore/ + + cat > restore.sh </dev/null || install+=" rsync" +dpkg-query -l 'aptitude' &>/dev/null || install+=" aptitude" +dpkg-query -l 'borgbackup' &>/dev/null || install+=" borgbackup" +dpkg-query -l 'borgmatic' &>/dev/null || install+=" borgmatic" + +if [[ -n "\$install" ]]; then + apt -y install \$install +fi + +rsync --ignore-existing -raz trusted.gpg.d/ /etc/apt/trusted.gpg.d/ +xargs aptitude --schedule-only install < InstallOnly.list +aptitude install +EOF + + popd || exit 2 +} + +hook_post() { + rm -rf "$restoreDir" +} + + +case "$1" in + before_check) hook_check || exit $?;; + before_backup) hook_pre || exit $?;; + after_backup) hook_post || exit $?;; +esac diff --git a/scripts/os_fedora b/scripts/os_fedora new file mode 100644 index 0000000..c4f1cea --- /dev/null +++ b/scripts/os_fedora @@ -0,0 +1,41 @@ +#!/bin/bash + +restoreDir="/etc/restore" + + +hook_check() { + if [[ -d "$restoreDir" ]]; then + rm -rf "$restoreDir" || exit 1 + fi +} + +hook_pre() { + mkdir -p "$restoreDir" || exit 1 + pushd "$restoreDir" || exit 2 + + rpm -qa | sort > Package.versions.list + rpm -qa --queryformat '%{NAME}.%{ARCH}\n' | sort > Package.list + + cat > restore.sh < restore.sh < Packages.list + + popd || exit 2 +} + +hook_post() { + rm -rf "$restoreDir" +} + +case "$1" in + before_check) hook_check;; + before_backup) hook_pre;; + after_backup) hook_post;; +esac diff --git a/scripts/os_suse b/scripts/os_suse new file mode 100644 index 0000000..c830257 --- /dev/null +++ b/scripts/os_suse @@ -0,0 +1,41 @@ +#!/bin/bash + +restoreDir="/etc/restore" + + +hook_check() { + if [[ -d "$restoreDir" ]]; then + rm -rf "$restoreDir" || exit 1 + fi +} + +hook_pre() { + mkdir -p "$restoreDir" || exit 1 + pushd "$restoreDir" || exit 2 + + rpm -qa | sort > Package.versions.list + rpm -qa --queryformat '%{NAME}\n' | sort > Package.list + + cat > restore.sh <