#!/bin/bash

source=$(sed -n '1p' /srv/scripts/config/backupcont)

destiny=$(sed -n '2p' /srv/scripts/config/backupcont)

cd "$source"
datetime=$(date +"%d_%m_%y")

function checkA {
  [ "$EUID" -ne 0 ] && {
    echo "Run this script as Root!"
    exit
    }
}

function checkB {
  # Delete files older than 3 days; Do not forget about the SLASH!
  find "$destiny"/ -type f -mtime +1 -delete
}

function checkpi {
  for logfile in /srv/containers/*pihole*/log/pihole.log; do
    if [ -f "$logfile" ]; then
        tail -n 500 "$logfile" | tee "${logfile}.tmp" && mv "${logfile}.tmp" "$logfile"
    fi
  done
}

function dockerctrl {
  docker "$1" $(docker ps -a -q)
}

function backup {
  cp -r /srv/scripts "$destiny"
  cp /srv/*.yaml "$destiny"

  for dir in $(find * -maxdepth 0)
    do
      tar -I 'lz4 -1 -c -' -cpf "$destiny"/"$dir"-"$datetime".tar.lz4 "$dir"
    done
}

checkA
checkB
dockerctrl stop &&
checkpi
backup
dockerctrl start &&

exit 0

#for dir in $(find * -maxdepth 0 ! -path "*oraclexe21c*")
