backup-Script/backup-check.sh

117 lines
7.5 KiB
Bash
Executable File

#!/bin/bash
# Funktion zum Senden der Pushover-Benachrichtigung
function send_pushover_notification() {
local user_token="uo2sf2pmrtjvt8auu786fviabimimr"
local app_token="agse9w3be5wf2stc9u725masjjdqsz"
local message="$@"
curl -s \
--form-string "token=$app_token" \
--form-string "user=$user_token" \
--form-string "title=$title" \
--form-string "message=$message" \
https://api.pushover.net/1/messages.json
}
while IFS= read -r line
do
if [[ $line == *pbs:* ]]; then
output=$(echo "$line" | grep -oP '(?<=pbs:).*'| tr -d '[:space:]')
for i in $output;
do
storage=$i
passwort=$(grep -o $i': .*' /root/Scripte/password_7z.txt | awk -F ': ' '{print $2}')
export PBS_PASSWORD=2LStkRwtmhYCF3Lu
export PBS_REPOSITORY=localbac01@pbs@backup.stines.de:$i
lastdate=$(date +"%Y-%m-%d" -d "1 day ago")
remove_date=$(date +"%Y-%m-%d" -d "8 day ago")
HDD=HDD_1_2
if $storage == "tnp-Invest-GmbH"; then
HDD=HDD_2_2
fi
rm -r /mnt/$HDD/restore/$storage/$remove_date
mkdir -p /mnt/$HDD/restore/$storage/$lastdate
image=$(pvesm list $storage | grep $(date +"%Y-%m-%d" -d "1 day ago")| cut -d ' ' -f1 )
for f in $image; do
path=$(echo "$f" | cut -d ':' -f 2- | sed 's/^backup\///')
echo $path
if [ -n "$f" ]; then
start_time=$(date +%s)
id=$(echo "$f" | cut -d ':' -f 2- | sed 's/^backup\///' | cut -d'/' -f2)
qm unlock $id
qm stop $id
qm destroy $id
rm -r /mnt/$HDD/images/$id
qmrestore $f $id --storage $HDD
qm set $id -delete cdrom
qm set $id -delete ide0
for ((net=0; net<=10; net++))
do
qm set $id -delete net$net
done
qm set $id --net10 e1000,bridge=vmbr1
qm set $id --agent enabled=1,type=virtio
qm set $id --net9 virtio,bridge=vmbr1
qm start $id
end_time=$(date +%s)
elapsed_seconds=$((end_time - start_time))
elapsed_minutes=$((elapsed_seconds / 60))
sleep 60
status=$(qm status $id | grep "running")
if [ "$status" == "status: running" ]; then
echo "---------------- Starttime: $(date +'%Y-%m-%d %H:%M:%S') -------------------------" >> /root/$lastdate-backup-restore.log
hostname=$(qm agent $id get-host-name | grep host-name)
hostname=${hostname//\"/}
mountpoint=$(qm agent $id get-fsinfo | grep mountpoint)
mountpoint=${mountpoint//\"/}
message="$storage - VM $f was Restored and Running! (Dauer $elapsed_minutes/min)" >> /root/$lastdate-backup-restore.log
echo "$(echo -e "$message\n${hostname#"${hostname%%[![:space:]]*}"}\n${mountpoint#"${mountpoint%%[![:space:]]*}"}")" >> /root/$lastdate-backup-restore.log
echo "----------------------------------------------------------------------------" >> /root/$lastdate-backup-restore.log
echo "---------------- Endtime: $(date +'%Y-%m-%d %H:%M:%S') -------------------------" >> /root/$lastdate-backup-restore.log
qm stop $id
else
echo "VM $f was Restored but not Running! (Dauer $elapsed_minutes/min)" >> /root/$lastdate-backup-restore.log
echo "----------------------------------------------------------------------------" >> /root/$lastdate-backup-restore.log
fi
echo $id
rm -r /mnt/$HDD/images/$id/qemu-server.conf
proxmox-backup-client restore $path qemu-server.conf /mnt/$HDD/images/$id/qemu-server.conf --keyfile /root/Scripte/$i.keyfile
else
continue
fi
done
for id in /mnt/$HDD/images/*;
do
echo $id
name=$(cat $id/qemu-server.conf | grep name | awk -F ': ' '{print $2}')
echo "---------------- Starttime: $(date +'%Y-%m-%d %H:%M:%S') -------------------------" >> /root/Scripte/7z_restore_$lastdate.log
7z a -mmt=on -mfb=48048 -mx=1 -md=256m -p$passwort /mnt/$HDD/restore/$storage/$lastdate/"$name".7z $id/* >> /root/Scripte/7z_restore_$lastdate.log
qm destroy $(basename $id)
echo "---------------- Endtime: $(date +'%Y-%m-%d %H:%M:%S') -------------------------" >> /root/Scripte/7z_restore_$lastdate.log
done
done
fi
done < /etc/pve/storage.cfg
NEXTCLOUD_URL="https://cloudstorage.stines.de/"
USERNAME="stinesupload"
PASSWORD="H9f4SFGtzwdbhokLBpd@nLYRP*ds3X^wY55g79pWk2WdJr7s8wSJzb6TJ6p@T"
# Pfad zur lokalen Datei, die hochgeladen werden soll
LOCAL_FILE="/root/$lastdate-backup-restore.log"
# Zielpfad in Nextcloud, wo die Datei gespeichert werden soll
REMOTE_PATH="/02.%20Server-Logs/01.%20Backup-Server/1.%20Zugriffe/2.%20Integritaetschecks/$(date +"%Y-%M-%D_%H:%M:%S")-Check-Backup.txt"
# Upload-URL für die WebDAV-API
UPLOAD_URL="$NEXTCLOUD_URL/remote.php/dav/files/$USERNAME$REMOTE_PATH"
# Datei mit curl hochladen
curl -u "$USERNAME:$PASSWORD" -X PUT -T "$LOCAL_FILE" "$UPLOAD_URL"