#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Import Config File
config=/usr/local/bin/config.cfg
source $config
# Check if Already Running
for pid in $(pidof -x SmartStore.sh); do
if [ $pid != $$ ]; then
echo "[$(date)] : SmartStore.sh : Process is already running with PID $pid"
exit 1
fi
done
# Get Zpool Status
zpool status
ZP=$(zpool status | grep degraded)
if [ -n "$ZP" ]; then
echo ACHTUNG: ${ZP}
fi
# Get Filesystem Usage-Percentage
FS=$(df -h | grep -E '([7-9][0-9]|100)%')
echo ${FS}
if [ -n "$FS" ]; then
echo ACHTUNG: ${FS}
fi
# Get Machine Count
pctList=$(pct list | wc -l)
if [ "$pctList" -ne "0" ]; then
pctList=$(($pctList - 1))
fi
qmList=$(qm list | wc -l)
if [ "$qmList" -ne "0" ]; then
qmList=$(($qmList - 1))
fi
NUMBER=$(($pctList + $qmList))
if [ -z "$mountPath" ]; then
mountPath=$backupPath
fi
if [ -z "$mountPersistent" ]; then
umount ${mountPath}
fi
mkdir -p ${mountPath}
folder=$(date +%A)
# Loop over UUIDs
for uuid in "${UUIDS[@]}"
do
mount UUID=$uuid ${mountPath}
mountpoint -q ${mountPath} || continue
echo Sichere UUID $uuid
echo "Backup ausgeführt am: $(date +"%Y-%m-%d-%H:%M")" on UUID $uuid >> "${logPath}"
if [[ " ${BackupType[*]} " =~ " vzdump " ]]; then
#Get Size of last Backups
if [ "$(ls ${backupPath}/*.zst | wc -l)" -eq "0" ] || [ "${NUMBER}" -eq "0" ]; then
backupCount='0'
backupNeed='NA'
backupPuffer='0'
else
backupSize=$(du -s ${backupPath}| cut -f 1)
backupCount=$(($(ls ${backupPath}/*.zst | wc -l) / ${NUMBER}))
backupNeed=$(($backupSize / $backupCount))
doubleNeed=$(($backupNeed * 2))
backupPuffer=$(($backupNeed * 11/10))
fi
backupAvail=$(df $mountPath | awk 'NR==2{print$4}')
echo $backupCount Backup found
echo $MAXFILE Backup in Backup-Retention-Policy
echo $backupNeed Bytes estimated Backup Size
echo $backupPuffer Bytes estimated Backup Size with Puffer
echo $backupAvail Bytes estimated available Backup Space
if [ ${backupPuffer} -ge ${backupAvail} ]; then
if [ "${MAXFILE}" -eq "1" ]; then
echo ACHTUNG: ZU WENIG SPEICHERPLATZ: Größere Backup Festplatte benötigt. Maxfile kann nicht weiter reduziert werden!
else
MAXFILE=$(($MAXFILE-1))
sed -i -e "s/^MAXFILE.*/MAXFILE=$MAXFILE/" ${config}
echo ACHTUNG: ZU WENIG SPEICHERPLATZ: Maxfile = ${MAXFILE}
fi
elif [ ${MAXFILE} == ${backupCount} ] && [ ${backupPuffer} -ge ${doubleNeed} ]; then
MAXFILE=$(($MAXFILE+1))
sed -i -e "s/^MAXFILE.*/MAXFILE=$MAXFILE/" ${config}
echo ACHTUNG: Freier Speicher gefunden, Maxfile erhöht. Maxfile = ${MAXFILE}
fi
vzdump --compress zstd --prune-backups keep-last=$MAXFILE --all --mode snapshot --dumpdir $backupPath
fi
if [[ " ${BackupType[*]} " =~ " rsync " ]]; then
#Get Last Backup Size / Maxfile Value
backupSize=$(du -s ${backupPath}| cut -f 1)
backupNeed=$(($backupSize / 6))
backupPuffer=$(($backupNeed * 11/10))
backupAvail=$(df $mountPath | awk 'NR==2{print$4}')
echo $backupNeed Bytes estimated Backup Size
echo $backupPuffer Bytes estimated Backup Size with Puffer
echo $backupAvail Bytes estimated availlable Backup Space
if [ ${backupPuffer} -ge ${backupAvail} ]; then
echo ACHTUNG: ZU WENIG SPEICHERPLATZ
fi
if [ -d "${backupPath}/__latest" ]; then
rsync -avh --delete --link-dest="${backupPath}/__latest" "$rsyncSource" "${backupPath}/$folder"
else
rsync -avh "$rsyncSource" "${backupPath}/$folder"
fi
rm -f "${backupPath}/__latest"
ln -s "$folder" "${backupPath}/__latest"
fi
if [ -z "$mountPersistent" ]; then
umount ${mountPath}
fi
done