From 242edc9dcbbe079501e84f808cc257aea7109637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sch=C3=A4ferdiek?= Date: Fri, 25 Jan 2019 18:28:29 +0100 Subject: [PATCH] Lots of updates --- etc/systemd/system/checkupdates.service | 9 + etc/systemd/system/checkupdates.timer | 9 + etc/systemd/system/disk_space_alert.service | 9 + etc/systemd/system/disk_space_alert.timer | 9 + .../system/docker_housekeeping.service | 9 + etc/systemd/system/docker_housekeeping.timer | 9 + etc/systemd/system/memory_usage_alert.service | 9 + etc/systemd/system/memory_usage_alert.timer | 9 + etc/systemd/system/pkgfile.service | 9 + etc/systemd/system/pkgfile.timer | 9 + .../system/smartctl_tests_long.service | 9 + etc/systemd/system/smartctl_tests_long.timer | 9 + etc/systemd/user/borgwrapper.service | 12 ++ etc/systemd/user/borgwrapper.timer | 9 + etc/systemd/user/docker_check.service | 9 + etc/systemd/user/docker_check.timer | 9 + etc/systemd/user/systemd_check.service | 9 + etc/systemd/user/systemd_check.timer | 9 + sync | 113 +++++++++++ sync.sh | 100 ---------- usr/local/bin/borgwrapper | 185 ++++++++++++++++++ usr/local/bin/checkupdates | 57 ++++++ usr/local/bin/disk_space_alert | 66 +++++++ usr/local/bin/docker_check | 152 ++++++++++++++ usr/local/bin/docker_housekeeping | 7 + usr/local/bin/memory_usage_alert | 64 ++++++ usr/local/bin/smartctl_tests_long | 61 ++++++ usr/local/bin/smartdnotify | 4 + usr/local/bin/systemd_check | 94 +++++++++ .../doc/borgwrapper/.borgwrapper.conf.example | 15 ++ .../checkupdates/.checkupdates.conf.example | 1 + .../.disk_space_alert.conf.example | 3 + .../docker_check/.docker_check.conf.example | 4 + .../.memory_usage_alert.conf.example | 2 + .../.smartctl_tests_long.conf.example | 2 + .../systemd_check/.systemd_check.conf.example | 3 + .../systemd_check/.systemd_check.list.example | 1 + 37 files changed, 999 insertions(+), 100 deletions(-) create mode 100644 etc/systemd/system/checkupdates.service create mode 100644 etc/systemd/system/checkupdates.timer create mode 100644 etc/systemd/system/disk_space_alert.service create mode 100644 etc/systemd/system/disk_space_alert.timer create mode 100644 etc/systemd/system/docker_housekeeping.service create mode 100644 etc/systemd/system/docker_housekeeping.timer create mode 100644 etc/systemd/system/memory_usage_alert.service create mode 100644 etc/systemd/system/memory_usage_alert.timer create mode 100644 etc/systemd/system/pkgfile.service create mode 100644 etc/systemd/system/pkgfile.timer create mode 100644 etc/systemd/system/smartctl_tests_long.service create mode 100644 etc/systemd/system/smartctl_tests_long.timer create mode 100644 etc/systemd/user/borgwrapper.service create mode 100644 etc/systemd/user/borgwrapper.timer create mode 100644 etc/systemd/user/docker_check.service create mode 100644 etc/systemd/user/docker_check.timer create mode 100644 etc/systemd/user/systemd_check.service create mode 100644 etc/systemd/user/systemd_check.timer create mode 100755 sync delete mode 100755 sync.sh create mode 100755 usr/local/bin/borgwrapper create mode 100755 usr/local/bin/checkupdates create mode 100755 usr/local/bin/disk_space_alert create mode 100755 usr/local/bin/docker_check create mode 100755 usr/local/bin/docker_housekeeping create mode 100755 usr/local/bin/memory_usage_alert create mode 100755 usr/local/bin/smartctl_tests_long create mode 100755 usr/local/bin/smartdnotify create mode 100755 usr/local/bin/systemd_check create mode 100644 usr/share/doc/borgwrapper/.borgwrapper.conf.example create mode 100644 usr/share/doc/checkupdates/.checkupdates.conf.example create mode 100644 usr/share/doc/disk_space_alert/.disk_space_alert.conf.example create mode 100644 usr/share/doc/docker_check/.docker_check.conf.example create mode 100644 usr/share/doc/memory_usage_alert/.memory_usage_alert.conf.example create mode 100644 usr/share/doc/smartctl_tests_long/.smartctl_tests_long.conf.example create mode 100644 usr/share/doc/systemd_check/.systemd_check.conf.example create mode 100644 usr/share/doc/systemd_check/.systemd_check.list.example diff --git a/etc/systemd/system/checkupdates.service b/etc/systemd/system/checkupdates.service new file mode 100644 index 0000000..7f5f284 --- /dev/null +++ b/etc/systemd/system/checkupdates.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run checkupdates + +[Service] +Type=oneshot +ExecStart=checkupdates + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/system/checkupdates.timer b/etc/systemd/system/checkupdates.timer new file mode 100644 index 0000000..e14cdf9 --- /dev/null +++ b/etc/systemd/system/checkupdates.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run checkupdates + +[Timer] +OnCalendar=Mon *-*-* 05:00:00 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/system/disk_space_alert.service b/etc/systemd/system/disk_space_alert.service new file mode 100644 index 0000000..943a547 --- /dev/null +++ b/etc/systemd/system/disk_space_alert.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run disk_space_alert + +[Service] +Type=oneshot +ExecStart=disk-space-alert + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/system/disk_space_alert.timer b/etc/systemd/system/disk_space_alert.timer new file mode 100644 index 0000000..62ea1e9 --- /dev/null +++ b/etc/systemd/system/disk_space_alert.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run disk_space_alert + +[Timer] +OnCalendar=daily +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/system/docker_housekeeping.service b/etc/systemd/system/docker_housekeeping.service new file mode 100644 index 0000000..02e1152 --- /dev/null +++ b/etc/systemd/system/docker_housekeeping.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run docker_housekeeping + +[Service] +Type=oneshot +ExecStart=docker_housekeeping + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/system/docker_housekeeping.timer b/etc/systemd/system/docker_housekeeping.timer new file mode 100644 index 0000000..812e753 --- /dev/null +++ b/etc/systemd/system/docker_housekeeping.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run docker_housekeeping + +[Timer] +OnCalendar=daily +Persistent=false + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/system/memory_usage_alert.service b/etc/systemd/system/memory_usage_alert.service new file mode 100644 index 0000000..073df4d --- /dev/null +++ b/etc/systemd/system/memory_usage_alert.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run memory_usage_alert + +[Service] +Type=oneshot +ExecStart=memory_usage_alert + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/system/memory_usage_alert.timer b/etc/systemd/system/memory_usage_alert.timer new file mode 100644 index 0000000..dd4712d --- /dev/null +++ b/etc/systemd/system/memory_usage_alert.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run memory_usage_alert + +[Timer] +OnCalendar=hourly +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/system/pkgfile.service b/etc/systemd/system/pkgfile.service new file mode 100644 index 0000000..54a4a19 --- /dev/null +++ b/etc/systemd/system/pkgfile.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run pkgfile + +[Service] +Type=oneshot +ExecStart=/usr/bin/pkgfile --update + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/system/pkgfile.timer b/etc/systemd/system/pkgfile.timer new file mode 100644 index 0000000..b343872 --- /dev/null +++ b/etc/systemd/system/pkgfile.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run pkgfile + +[Timer] +OnCalendar=Mon *-*-* 05:00:00 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/system/smartctl_tests_long.service b/etc/systemd/system/smartctl_tests_long.service new file mode 100644 index 0000000..530db4a --- /dev/null +++ b/etc/systemd/system/smartctl_tests_long.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run smart_ctl_tests_long + +[Service] +Type=oneshot +ExecStart=smart_ctl_tests_long + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/system/smartctl_tests_long.timer b/etc/systemd/system/smartctl_tests_long.timer new file mode 100644 index 0000000..63bce62 --- /dev/null +++ b/etc/systemd/system/smartctl_tests_long.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run smart_ctl_tests_long + +[Timer] +OnCalendar=*-*-01 03:00:00 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/user/borgwrapper.service b/etc/systemd/user/borgwrapper.service new file mode 100644 index 0000000..ca1466f --- /dev/null +++ b/etc/systemd/user/borgwrapper.service @@ -0,0 +1,12 @@ +[Unit] +Description=Run borgwrapper + +[Service] +Type=oneshot +#ExecStart=borgwrapper /home/myUser/borgwrapper-custom.conf +ExecStart=borgwrapper +#Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/myUser/.customBinaryPathWithBorgSymlinkForVirtualenv" +#Environment="SSH_AUTH_SOCK=/run/user/myUserId/keyring/ssh" + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/user/borgwrapper.timer b/etc/systemd/user/borgwrapper.timer new file mode 100644 index 0000000..13297d0 --- /dev/null +++ b/etc/systemd/user/borgwrapper.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run borgwrapper + +[Timer] +OnCalendar=20:00 +Persistent=false + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/user/docker_check.service b/etc/systemd/user/docker_check.service new file mode 100644 index 0000000..0586ed4 --- /dev/null +++ b/etc/systemd/user/docker_check.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run docker_check + +[Service] +Type=oneshot +ExecStart=docker_check + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/user/docker_check.timer b/etc/systemd/user/docker_check.timer new file mode 100644 index 0000000..536c606 --- /dev/null +++ b/etc/systemd/user/docker_check.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run docker_check + +[Timer] +OnCalendar=*-*-* *:30:00 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/etc/systemd/user/systemd_check.service b/etc/systemd/user/systemd_check.service new file mode 100644 index 0000000..8874e05 --- /dev/null +++ b/etc/systemd/user/systemd_check.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run systemd_check + +[Service] +Type=oneshot +ExecStart=systemd_check + +[Install] +WantedBy=multi-user.target diff --git a/etc/systemd/user/systemd_check.timer b/etc/systemd/user/systemd_check.timer new file mode 100644 index 0000000..5481315 --- /dev/null +++ b/etc/systemd/user/systemd_check.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run systemd_check + +[Timer] +OnCalendar=*-*-* *:15:00 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/sync b/sync new file mode 100755 index 0000000..1a4b7ab --- /dev/null +++ b/sync @@ -0,0 +1,113 @@ +# !/usr/bin/env bash +# +# Be careful what you are doing with this script. You may overwrite files. +# cd into the repository before executing the script! +set -e; + +usage() { + echo "Script to synchronize dotfiles or common scripts"; + echo "---"; + echo "- Press 1 to sync the git folder's '/etc' with system's '/etc'"; + echo ""; + echo "- Press 2 to sync the git folder's '/etc' with all command line users with a certain shell"; + echo ""; + echo "- Press 3 to sync the git folder's '/etc' ('/etc/skel') with a specific user's home folder" + echo ""; + echo "- Press 4 to list command line users for sync"; + echo ""; + echo "- Press 5 to sync the git folder's '/usr/local/bin' with system's '/usr/local/bin'"; + echo "---"; + echo "Press 0 or [CTRL+C] to exit"; +} + +menu() { + usage; + read -n 1 -e -p ">" menuInput + echo ""; + + if [ "$menuInput" = "1" ]; then + sudo rsync -r -t -v --chown=root:root --progress $(pwd)/etc/ /etc/ + echo ""; + elif [ "$menuInput" = "2" ]; then + # require root + [[ $UID -eq 0 ]] || (>&2 echo "ERROR: need to be root!" && exit 1) + + # determine shell + echo "Which shell (shortname, e.g. zsh)?"; + read -e -p ">" shellInput + echo ""; + + # shell users + users=$(cat /etc/passwd|grep $shellInput| awk -F':' '{ print $1 }'); + + # stage + tmpDir=/tmp/dotfiles-system-sync/; + mkdir -p $tmpDir; + srcDir=$(pwd)/etc/skel/. + cp -R $srcDir $tmpDir; + + # copy + for user in $users; do + exists=$(id -u $user); + primaryGroup=$(id -gn $user); + chown -R $user:$primaryGroup $tmpDir; + trgDir=$(eval echo ~$user/); + echo "Copy from $tmpDir to $trgDir"; + cp -Rp $tmpDir/. $trgDir/; + done + + # remove stage + rm -rf $tmpDir; + elif [ "$menuInput" = "3" ]; then + # determine shell + echo "Which user?"; + read -e -p ">" user + echo ""; + + # stage + tmpDir=/tmp/dotfiles-system-sync/; + mkdir -p $tmpDir; + srcDir=$(pwd)/etc/skel/. + cp -R $srcDir $tmpDir; + + # copy + exists=$(id -u $user); + + if [ $UID -ne $exists ] && [ $UID -ne 0 ]; then + echo "ERROR: need to be $user or root!"; + exit 1; + fi + + primaryGroup=$(id -gn $user); + chown -R $user:$primaryGroup $tmpDir; + trgDir=$(eval echo ~$user/); + echo "Copy from $tmpDir to $trgDir"; + cp -Rp $tmpDir/. $trgDir/; + + # remove stage + rm -rf $tmpDir; + + elif [ "$menuInput" = "4" ]; then + echo "Which shell (shortname, e.g. zsh)?"; + read -e -p ">" shellInput + echo ""; + + # shell users + users=$(cat /etc/passwd|grep $shellInput| awk -F':' '{ print $1 }'); + echo "Users with shell $shellInput" + + for u in $users; do + echo $u; + done + elif [ "$menuInput" = "5" ]; then + echo "Yes"; + elif [ "$menuInput" = "0" ]; then + sudo rsync -r -t -v --chown=root:root --progress $(pwd)/usr/local/bin/ /usr/local/bin/ + echo ""; + exit 0; + fi + + menu; +} + +menu; \ No newline at end of file diff --git a/sync.sh b/sync.sh deleted file mode 100755 index b411c1d..0000000 --- a/sync.sh +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env bash -# Be careful what you are doing with this script. You may overwrite dotfiles. -# cd into the repository before executing the script! -set -e; - -usage() { - echo "Press 1 to sync git folder to '/etc'"; - echo "Press 2 to sync git repository folder 'etc/skel' with all command line users"; - echo "Press 3 to sync git repository folder 'etc/skel' with a specific user's home folder" - echo "Press 4 to list command line users for sync"; - echo "---"; - echo "Hit CTRL+C to exit"; -} - -menu() { - usage; - read -n 1 -e -p ">" menuInput - echo ""; - - if [ "$menuInput" = "1" ]; then - sudo rsync -r -t -v --chown=root:root --progress $(pwd)/etc/ /etc/ - echo ""; - elif [ "$menuInput" = "2" ]; then - # require root - [[ $UID -eq 0 ]] || (>&2 echo "ERROR: need to be root!" && exit 1) - - # determine shell - echo "Which shell (shortname, e.g. zsh)?"; - read -e -p ">" shellInput - echo ""; - - # shell users - users=$(cat /etc/passwd|grep $shellInput| awk -F':' '{ print $1 }'); - - # stage - tmpDir=/tmp/dotfiles-system-sync/; - mkdir -p $tmpDir; - srcDir=$(pwd)/etc/skel/. - cp -R $srcDir $tmpDir; - - # copy - for user in $users; do - exists=$(id -u $user); - primaryGroup=$(id -gn $user); - chown -R $user:$primaryGroup $tmpDir; - trgDir=$(eval echo ~$user/); - echo "Copy from $tmpDir to $trgDir"; - cp -Rp $tmpDir/. $trgDir/; - done - - # remove stage - rm -rf $tmpDir; - elif [ "$menuInput" = "3" ]; then - # determine shell - echo "Which user?"; - read -e -p ">" user - echo ""; - - # stage - tmpDir=/tmp/dotfiles-system-sync/; - mkdir -p $tmpDir; - srcDir=$(pwd)/etc/skel/. - cp -R $srcDir $tmpDir; - - # copy - exists=$(id -u $user); - - if [ $UID -ne $exists ] && [ $UID -ne 0 ]; then - echo "ERROR: need to be $user or root!"; - exit 1; - fi - - primaryGroup=$(id -gn $user); - chown -R $user:$primaryGroup $tmpDir; - trgDir=$(eval echo ~$user/); - echo "Copy from $tmpDir to $trgDir"; - cp -Rp $tmpDir/. $trgDir/; - - # remove stage - rm -rf $tmpDir; - - elif [ "$menuInput" = "4" ]; then - echo "Which shell (shortname, e.g. zsh)?"; - read -e -p ">" shellInput - echo ""; - - # shell users - users=$(cat /etc/passwd|grep $shellInput| awk -F':' '{ print $1 }'); - echo "Users with shell $shellInput" - - for u in $users; do - echo $u; - done - - fi - - menu; -} - -menu; diff --git a/usr/local/bin/borgwrapper b/usr/local/bin/borgwrapper new file mode 100755 index 0000000..b76ca74 --- /dev/null +++ b/usr/local/bin/borgwrapper @@ -0,0 +1,185 @@ +#!/usr/bin/env bash + +# called before backup +borgwrapper_pre_backup() { + return; +} + +# called after backup +borgwrapper_post_backup() { + return; +} + +# usage +usage() { +USAGE=$(cat <'. +EOF +) + echo "$USAGE"; +} + +set -e; + +# vars and defaults +BORGWRAPPER_BACKUP_PASSWORD='' +BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT=$(date "+%s") +BORGWRAPPER_BACKUP_PRUNE=true +BORGWRAPPER_BACKUP_KEEP_IN_DAYS=60 +BORGWRAPPER_BACKUP_CHECK=true +BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS=604800 +BORGWRAPPER_BACKUP_CHECK_FILE="$HOME/.borg-backup-$BORGWRAPPER_BACKUP_NAME.check" +BORGWRAPPER_BACKUP_LOG=true +BORGWRAPPER_BACKUP_LOG_PRUNE=true +BORGWRAPPER_BACKUP_LOG_FILE="$HOME/.borg-backup-$BORGWRAPPER_BACKUP_NAME.log" +BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL=false +BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS="" +BORGWRAPPER_BACKUP_NOTIFY_UI=false + +BORGWRAPPER_BORG_BINARY=$(which borg); +BORGWRAPPER_BORG_INIT_PARAMS="--encryption=none"; +BORGWRAPPER_BORG_CREATE_PARAMS="-v -s -p -C lz4"; +BORGWRAPPER_BORG_CHECK_PARAMS="-v"; +BORGWRAPPER_BORG_PRUNE_PARAMS="-v -s -d $BORGWRAPPER_BACKUP_KEEP_IN_DAYS"; + +# check for config file +source_config() { + local config=$1; + local configFallback=$2; + + if [[ ! -f "$config" ]]; then + if [[ ! -f "$backupConfigFileFallback" ]]; then + echo "No config file specified and could not find default in '$configFallback'!"; + echo ""; + usage; + exit 1; + else + config=$configFallback; + fi + fi + + set -a; + source "$config"; + set +a; +} +source_config "$1" "$HOME/.borgwrapper.conf" + +# start and info +echo "Using $BORGWRAPPER_BORG_BINARY as binary" + +borgwrapper_pre_backup; + +# export passphrase if not blank +if [ ! -z "$BORGWRAPPER_BACKUP_PASSWORD" ]; then + export BORG_PASSPHRASE=$BORGWRAPPER_BACKUP_PASSWORD +fi + +# init hint +echo "Hint: If you haven't, you need to create the borg repository manually before first run if it doesn't exist:"; +echo "$BORGWRAPPER_BORG_BINARY init $BORGWRAPPER_BORG_INIT_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME"; + +# notify UI +if [ "$BORGWRAPPER_BACKUP_NOTIFY_UI" = true ]; then + notify-send "Starting backup" $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT; +fi + +# create +$BORGWRAPPER_BORG_BINARY create $BORGWRAPPER_BORG_CREATE_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT "${BORGWRAPPER_BACKUP_FILES[@]}"; + +# check +if [ "$BORGWRAPPER_BACKUP_CHECK" = true ]; then + + # use for first time creation + doCheck=false + + if [ ! -f "$BORGWRAPPER_BACKUP_CHECK_FILE" ]; then + touch $BORGWRAPPER_BACKUP_CHECK_FILE + doCheck=true + fi + + # compare date + lastMod=$(date -r $BORGWRAPPER_BACKUP_CHECK_FILE +%s) + age=$(($BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT - $lastMod)) + + if [ "$age" -gt "$BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS" ]; then + doCheck=true + fi + + + if [ "$doCheck" = true ]; then + $BORGWRAPPER_BORG_BINARY check $BORGWRAPPER_BORG_CHECK_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME; + touch $BORGWRAPPER_BACKUP_CHECK_FILE + fi +fi + +# prune +if [ "$BORGWRAPPER_BACKUP_PRUNE" = true ]; then + $BORGWRAPPER_BORG_BINARY prune $BORGWRAPPER_BORG_PRUNE_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME; +fi + +# log +if [ "$BORGWRAPPER_BACKUP_LOG" = true ]; then + + if [ "$BORGWRAPPER_BACKUP_LOG_PRUNE" = true ] && [ -f "$BORGWRAPPER_BACKUP_LOG_FILE" ]; then + rm $BORGWRAPPER_BACKUP_LOG_FILE; + fi + + touch $BORGWRAPPER_BACKUP_LOG_FILE; + $BORGWRAPPER_BORG_BINARY list -v $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME >> $BORGWRAPPER_BACKUP_LOG_FILE; + echo "---" >> $BORGWRAPPER_BACKUP_LOG_FILE; + $BORGWRAPPER_BORG_BINARY info $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT >> $BORGWRAPPER_BACKUP_LOG_FILE; + + if [ "$BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL" = true ]; then + cat $BORGWRAPPER_BACKUP_LOG_FILE|mailx -Ssendwait -s "[backup $BORGWRAPPER_BACKUP_NAME]" $BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS; + fi +fi + +# notify UI +if [ "$BORGWRAPPER_BACKUP_NOTIFY_UI" = true ]; then + notify-send "Finished backup" $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT; +fi + +borgwrapper_post_backup; + diff --git a/usr/local/bin/checkupdates b/usr/local/bin/checkupdates new file mode 100755 index 0000000..a5c3a85 --- /dev/null +++ b/usr/local/bin/checkupdates @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +# usage +usage() { +USAGE=$(cat <'. +EOF +) + echo "$USAGE"; +} + +set -e; + +HOSTNAME=$(hostname) +UPDATES=$(checkupdates) +UPDATES_AMOUNT=$(checkupdates|wc -l) +CHECKUPDATES_MAIL_ADDRESS=""; + +# check for config file +source_config() { + local config=$1; + local configFallback=$2; + + if [[ ! -f "$config" ]]; then + if [[ ! -f "$backupConfigFileFallback" ]]; then + echo "No config file specified and could not find default in '$configFallback'!"; + echo ""; + usage; + exit 1; + else + config=$configFallback; + fi + fi + + set -a; + source "$config"; + set +a; +} +source_config "$1" "$HOME/.checkupdates.conf" + +if [ "$UPDATES_AMOUNT" -gt "0" ]; then + mailx -s "[updates $HOSTNAME]" $CHECKUPDATES_MAIL_ADDRESS << EOF +There are $UPDATES_AMOUNT updates available on $HOSTNAME. + +$UPDATES +EOF +fi diff --git a/usr/local/bin/disk_space_alert b/usr/local/bin/disk_space_alert new file mode 100755 index 0000000..1e61fd7 --- /dev/null +++ b/usr/local/bin/disk_space_alert @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +# usage +usage() { +USAGE=$(cat <'. +EOF +) + echo "$USAGE"; +} + +set -e; + +HOSTNAME=$(hostname) +DISK_SPACE_ALERT_MAIL_ADDRESS=""; +DISK_SPACE_ALERT_THRESHOLD=93 +DISK_SPACE_ALERT_MOUNTPOINTS=("/") + +# check for config file +source_config() { + local config=$1; + local configFallback=$2; + + if [[ ! -f "$config" ]]; then + if [[ ! -f "$backupConfigFileFallback" ]]; then + echo "No config file specified and could not find default in '$configFallback'!"; + echo ""; + usage; + exit 1; + else + config=$configFallback; + fi + fi + + set -a; + source "$config"; + set +a; +} +source_config "$1" "$HOME/.disk_space_alert.conf" + +for point in "${DISK_SPACE_ALERT_MOUNTPOINTS[@]}" +do + CURRENT=$(df $point | grep $point | awk '{ print $5}' | sed 's/%//g') + CURRENT_SPACE=$(df $point | grep $point | awk '{ print $5}' | sed 's/%//g') + CURRENT_INODES=$(df -i $point | grep $point | awk '{ print $5}' | sed 's/%//g') + + if [ "$CURRENT" -gt "$DISK_SPACE_ALERT_THRESHOLD" ] ; then + mailx -s "[disk $HOSTNAME] $point" $DISK_SPACE_ALERT_MAIL_ADDRESS << EOF +Your $point partition remaining free space is critically low. Used space: $CURRENT_SPACE%. Used inodes: $CURRENT_INODES%. +EOF +fi +done diff --git a/usr/local/bin/docker_check b/usr/local/bin/docker_check new file mode 100755 index 0000000..4defb36 --- /dev/null +++ b/usr/local/bin/docker_check @@ -0,0 +1,152 @@ +#!/usr/bin/env bash + +# usage +usage() { +USAGE=$(cat <> $HOME/.docker_check.list) +DOCKER_CHECK_NOTIFY_LEVELS=("UNKNOWN" "WARNING" "CRITICAL") // when to notify/output/send mail. possible: "UNKNOWN" "WARNING" "CRITICAL" "INFO", should at least be () +DOCKER_CHECK_MAIL_ENABLED=false // send email (address has to be set), values: false|true +DOCKER_CHECK_MAIL_ADDRESS="" // mail.rc has to be configured + +You can copy this script to '/usr/local/bin' and use create a custom CONFIG_FILE as user. Examples can be found in '/usr/share/doc/'. +EOF +) + echo "$USAGE"; +} + +set -e; + +HOSTNAME=$(hostname) +DOCKER_CHECK_LIST_FILE="$HOME/.docker_check.list" +DOCKER_CHECK_NOTIFY_LEVELS=("UNKNOWN" "WARNING" "CRITICAL") +DOCKER_CHECK_MAIL_ENABLED=false +DOCKER_CHECK_MAIL_ADDRESS="" +CONTAINERS=() + +# check for config file +source_config() { + local config=$1; + local configFallback=$2; + + if [[ ! -f "$config" ]]; then + if [[ ! -f "$backupConfigFileFallback" ]]; then + echo "No config file specified and could not find default in '$configFallback'!"; + echo ""; + usage; + exit 1; + else + config=$configFallback; + fi + fi + + set -a; + source "$config"; + set +a; +} +source_config "$1" "$HOME/.docker_check.conf" + +if [ ! -f $DOCKER_CHECK_LIST_FILE ]; then + echo "$DOCKER_CHECK_LIST_FILE doesn't exist. Add a file which holds one docker container per line."; + echo ""; + usage; + exit 1; +fi + +echo "Scanning $DOCKER_CHECK_LIST_FILE for containers..."; +while IFS='' read -r line || [[ -n "$line" ]]; do + CONTAINERS+=("$line") + echo "-> Found $line"; +done < "$DOCKER_CHECK_LIST_FILE" + +# check if current status is in array of DOCKER_CHECK_NOTIFY_LEVELS +function shouldLog() { + local VALUE=$1; + + for i in "${DOCKER_CHECK_NOTIFY_LEVELS[@]}"; do + if [ "$i" == "$VALUE" ] ; then + echo true; + return; + fi + done + + echo false; +} + +# log and send mail if enabled +function log() { + local CONTAINER=$1; + local STATUS=$2; + local MESSAGE=$3; + + local SHOULD_LOG=$(shouldLog "$STATUS"); + if [ "$SHOULD_LOG" = "true" ]; then + local SUBJECT="[docker $HOSTNAME] $STATUS $CONTAINER"; + echo "$SUBJECT: $MESSAGE"; + + if [ "$DOCKER_CHECK_MAIL_ENABLED" = true ]; then + echo $MESSAGE|mailx -Ssendwait -s "$SUBJECT" $DOCKER_CHECK_MAIL_ADDRESS; + fi + fi +} + +# get status for a docker container +function checkContainer() { + local CONTAINER=$1; + + if [ "x${CONTAINER}" == "x" ]; then + log "$CONTAINER" "UNKNOWN" "Container ID or Friendly Name Required" + return; + fi + + if [ "x$(which docker)" == "x" ]; then + log "$CONTAINER" "UNKNOWN" "Missing docker binary" + return; + fi + + docker info > /dev/null 2>&1 + if [ $? -ne 0 ]; then + log "$CONTAINER" "UNKNOWN" "Unable to talk to the docker daemon" + return; + fi + + RUNNING=$(docker inspect --format="{{.State.Running}}" $CONTAINER 2> /dev/null) + + if [ $? -eq 1 ]; then + log "$CONTAINER" "UNKNOWN" "$CONTAINER does not exist." + return; + fi + + if [ "$RUNNING" == "false" ]; then + log "$CONTAINER" "CRITICAL" "$CONTAINER is not running." + return; + fi + + RESTARTING=$(docker inspect --format="{{.State.Restarting}}" $CONTAINER) + + if [ "$RESTARTING" == "true" ]; then + log "$CONTAINER" "WARNING" "$CONTAINER state is restarting." + return; + fi + + #STARTED=$(docker inspect --format="{{.State.StartedAt}}" $CONTAINER) + #NETWORK=$(docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $CONTAINER) + #log "$CONTAINER" "INFO" "$CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED" + + log "$CONTAINER" "INFO" "$CONTAINER is running." + + return; +} + +# execute check for defined containers +for container in "${CONTAINERS[@]}"; do + echo "Checking $container..."; + checkContainer "$container" +done \ No newline at end of file diff --git a/usr/local/bin/docker_housekeeping b/usr/local/bin/docker_housekeeping new file mode 100755 index 0000000..725e9b7 --- /dev/null +++ b/usr/local/bin/docker_housekeeping @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +echo "Doing docker housekeeping for containers, images and volumes"; + +yes | docker container prune +yes | docker image prune +yes | docker volume prune \ No newline at end of file diff --git a/usr/local/bin/memory_usage_alert b/usr/local/bin/memory_usage_alert new file mode 100755 index 0000000..13e4fda --- /dev/null +++ b/usr/local/bin/memory_usage_alert @@ -0,0 +1,64 @@ +#!/usr/bin/env bash + +# usage +usage() { +USAGE=$(cat <'. +EOF +) + echo "$USAGE"; +} + +set -e; + +HOSTNAME=$(hostname) +SUBJECT="[memory $HOSTNAME] memory is low" +MEMORY_USAGE_ALERT_MAIL_ADDRESS=""; +MEMORY_USAGE_ALERT_THRESHOLD=128; + +# check for config file +source_config() { + local config=$1; + local configFallback=$2; + + if [[ ! -f "$config" ]]; then + if [[ ! -f "$backupConfigFileFallback" ]]; then + echo "No config file specified and could not find default in '$configFallback'!"; + echo ""; + usage; + exit 1; + else + config=$configFallback; + fi + fi + + set -a; + source "$config"; + set +a; +} +source_config "$1" "$HOME/.memory_usage_alert.conf" + +# DO NOT TOUCH BELOW +free=$(free -mt | grep Total | awk '{print $4}') + +if [[ "$free" -le $MEMORY_USAGE_ALERT_THRESHOLD ]]; then + ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/tmp/top_proccesses_consuming_memory.txt + + file=/tmp/top_proccesses_consuming_memory.txt + echo -e "Memory on $HOSTNAME is running low ($MEMORY_USAGE_ALERT_THRESHOLD MB is the remaining threshold)!\n\nFree memory: $free MB" | mailx -a "$file" -s "$SUBJECT" "$MEMORY_USAGE_ALERT_MAIL_ADDRESS" +fi + +exit 0 diff --git a/usr/local/bin/smartctl_tests_long b/usr/local/bin/smartctl_tests_long new file mode 100755 index 0000000..97c9920 --- /dev/null +++ b/usr/local/bin/smartctl_tests_long @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +# usage +usage() { +USAGE=$(cat <'. +EOF +) + echo "$USAGE"; +} + +set -e; + +HOSTNAME=$(hostname) +SMARTCTL_TESTS_LONG_MAIL_ADDRESS=""; +SMARTCTL_TESTS_LONG_DEVICES=(sda) + +# check for config file +source_config() { + local config=$1; + local configFallback=$2; + + if [[ ! -f "$config" ]]; then + if [[ ! -f "$backupConfigFileFallback" ]]; then + echo "No config file specified and could not find default in '$configFallback'!"; + echo ""; + usage; + exit 1; + else + config=$configFallback; + fi + fi + + set -a; + source "$config"; + set +a; +} +source_config "$1" "$HOME/.smartctl_tests_long.conf" + +echo "Starting long tests for ..."; + +for d in ${SMARTCTL_TESTS_LONG_DEVICES[@]}; do + DEVICE="/dev/"$d; + echo $DEVICE; + smartctl -t long $DEVICE; + mailx -s "[smartd $HOSTNAME] queued long test" $SMARTCTL_TESTS_LONG_MAIL_ADDRESS << EOF +Queued a smartd long test for $DEVICE. +EOF +done + diff --git a/usr/local/bin/smartdnotify b/usr/local/bin/smartdnotify new file mode 100755 index 0000000..df489af --- /dev/null +++ b/usr/local/bin/smartdnotify @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +HOSTNAME=$(hostname) +echo "$SMARTD_MESSAGE" | mail -s "[smartd $HOSTNAME] $SMARTD_FAILTYPE" "$SMARTD_ADDRESS" +wall "$SMARTD_MESSAGE" \ No newline at end of file diff --git a/usr/local/bin/systemd_check b/usr/local/bin/systemd_check new file mode 100755 index 0000000..908445a --- /dev/null +++ b/usr/local/bin/systemd_check @@ -0,0 +1,94 @@ +#!/usr/bin/env bash + +# usage +usage() { +SYSTEMD_CHECK_USAGE=$(cat <'. +EOF +) + echo "$SYSTEMD_CHECK_USAGE"; +} + +set -e; + +# vars and defaults +SYSTEMD_CHECK_LIST_FILE="$HOME/.systemd_check.list" +SYSTEMD_CHECK_MAIL_ENABLED=fase +SYSTEMD_CHECK_MAIL_ADDRESS="" +SYSTEMD_CHECK_HOSTNAME=$(hostname) +SYSTEMD_CHECK_USER=$(whoami) +SYSTEMD_CHECK_SERVICES=() + +# check for config file +systemdCheckConfigFile=$1; +systemdCheckConfigFileFallback=$HOME/.systemd_check.conf; + +if [[ ! -f "$systemdCheckConfigFile" ]]; then + if [[ ! -f "$systemdCheckConfigFileFallback" ]]; then + echo "No config file specified and could not find default in '$systemdCheckConfigFileFallback'!"; + echo ""; + usage; + exit 1; + else + systemdCheckConfigFile=$systemdCheckConfigFileFallback; + fi +fi + +set -a; +source "$systemdCheckConfigFile"; +set +a; + +if [ ! -f $SYSTEMD_CHECK_LIST_FILE ]; then + echo "$SYSTEMD_CHECK_LIST_FILE doesn't exist. Add a file which holds one systemd service or timer per line."; + exit 1; +fi + +echo "Scanning $SYSTEMD_CHECK_LIST_FILE for services..."; +while IFS='' read -r line || [[ -n "$line" ]]; do + SYSTEMD_CHECK_SERVICES+=("$line") + echo "-> Found $line"; +done < "$SYSTEMD_CHECK_LIST_FILE" + +# get status for a service +function checkService() { + local SERVICE=$1; + local MESSAGE="$SERVICE is not running for user $SYSTEMD_CHECK_USER"; + local COMMAND="systemctl is-active --quiet $SERVICE"; + + if [ "$(id -u)" != "0" ]; then + local COMMAND="systemctl --user is-active --quiet $SERVICE"; + fi + + if ! $($COMMAND); then + local SUBJECT="[systemd $SYSTEMD_CHECK_HOSTNAME for $SYSTEMD_CHECK_USER] CRITICAL $SERVICE"; + echo "-> $SUBJECT: $MESSAGE"; + + if [ "$SYSTEMD_CHECK_MAIL_ENABLED" = true ]; then + echo $MESSAGE|mailx -Ssendwait -s "$SUBJECT" $SYSTEMD_CHECK_MAIL_ADDRESS; + fi + fi + + return; +} + +# execute check for defined SYSTEMD_CHECK_SERVICES +for service in "${SYSTEMD_CHECK_SERVICES[@]}"; do + echo "Checking $service..."; + checkService "$service" +done diff --git a/usr/share/doc/borgwrapper/.borgwrapper.conf.example b/usr/share/doc/borgwrapper/.borgwrapper.conf.example new file mode 100644 index 0000000..ecfdfdf --- /dev/null +++ b/usr/share/doc/borgwrapper/.borgwrapper.conf.example @@ -0,0 +1,15 @@ +BORGWRAPPER_BACKUP_NAME="backup-name" +BORGWRAPPER_BACKUP_REPOSITORY="/mnt/myBackups/" +BORGWRAPPER_BACKUP_FILES=(\ +'/etc/' \ +'$HOME/' \ +) +BORGWRAPPER_BORG_INIT_PARAMS="--encryption=repokey-blake2" + +borgwrapper_pre_backup() { + return; +} + +borgwrapper_post_backup() { + return; +} diff --git a/usr/share/doc/checkupdates/.checkupdates.conf.example b/usr/share/doc/checkupdates/.checkupdates.conf.example new file mode 100644 index 0000000..f14b4b5 --- /dev/null +++ b/usr/share/doc/checkupdates/.checkupdates.conf.example @@ -0,0 +1 @@ +CHECKUPDATES_MAIL_ADDRESS="alias@domain.tld" \ No newline at end of file diff --git a/usr/share/doc/disk_space_alert/.disk_space_alert.conf.example b/usr/share/doc/disk_space_alert/.disk_space_alert.conf.example new file mode 100644 index 0000000..0da8dc6 --- /dev/null +++ b/usr/share/doc/disk_space_alert/.disk_space_alert.conf.example @@ -0,0 +1,3 @@ +DISK_SPACE_ALERT_MAIL_ADDRESS="alias@domain.tld" +#DISK_SPACE_ALERT_THRESHOLD=99 +#DISK_SPACE_ALERT_MOUNTPOINTS=("/", "/home") \ No newline at end of file diff --git a/usr/share/doc/docker_check/.docker_check.conf.example b/usr/share/doc/docker_check/.docker_check.conf.example new file mode 100644 index 0000000..6446555 --- /dev/null +++ b/usr/share/doc/docker_check/.docker_check.conf.example @@ -0,0 +1,4 @@ +#DOCKER_CHECK_LIST_FILE="/root/other/directory/prefix_docker_check.list" +#DOCKER_CHECK_NOTIFY_LEVELS=("UNKNOWN" "WARNING" "CRITICAL") +#DOCKER_CHECK_MAIL_ENABLED=true +#DOCKER_CHECK_MAIL_ADDRESS="alias@domain.tld" \ No newline at end of file diff --git a/usr/share/doc/memory_usage_alert/.memory_usage_alert.conf.example b/usr/share/doc/memory_usage_alert/.memory_usage_alert.conf.example new file mode 100644 index 0000000..e091676 --- /dev/null +++ b/usr/share/doc/memory_usage_alert/.memory_usage_alert.conf.example @@ -0,0 +1,2 @@ +MEMORY_USAGE_ALERT_MAIL_ADDRESS="alias@domain.tld" +#MEMORY_USAGE_ALERT_THRESHOLD=512 \ No newline at end of file diff --git a/usr/share/doc/smartctl_tests_long/.smartctl_tests_long.conf.example b/usr/share/doc/smartctl_tests_long/.smartctl_tests_long.conf.example new file mode 100644 index 0000000..f8fd472 --- /dev/null +++ b/usr/share/doc/smartctl_tests_long/.smartctl_tests_long.conf.example @@ -0,0 +1,2 @@ +SMARTCTL_TESTS_LONG_MAIL_ADDRESS="alias@domain.tld" +#SMARTCTL_TESTS_LONG_DEVICES=512 \ No newline at end of file diff --git a/usr/share/doc/systemd_check/.systemd_check.conf.example b/usr/share/doc/systemd_check/.systemd_check.conf.example new file mode 100644 index 0000000..6b1639f --- /dev/null +++ b/usr/share/doc/systemd_check/.systemd_check.conf.example @@ -0,0 +1,3 @@ +SYSTEMD_CHECK_MAIL_ENABLED=true +SYSTEMD_CHECK_MAIL_ADDRESS="alias@domain.tld" +SYSTEMD_CHECK_LIST_FILE="$HOME/.systemd_check.list" \ No newline at end of file diff --git a/usr/share/doc/systemd_check/.systemd_check.list.example b/usr/share/doc/systemd_check/.systemd_check.list.example new file mode 100644 index 0000000..46da6ff --- /dev/null +++ b/usr/share/doc/systemd_check/.systemd_check.list.example @@ -0,0 +1 @@ +demo.service \ No newline at end of file