From ece6a7c5fb8a3b83a8320c820e8b6536a9c354c3 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 24 May 2025 13:52:33 +0200 Subject: [PATCH] add last backup dir ref --- backup | 191 +++++++++------------------------------------------------ 1 file changed, 29 insertions(+), 162 deletions(-) diff --git a/backup b/backup index 8589236..47ccea8 100755 --- a/backup +++ b/backup @@ -39,24 +39,6 @@ SizeHumanReadable() printf "%s %s\n" "$size" "${einheit[$count]}" } -# TODO: delete method -CreateBackupFolder() -{ - local dayAnzahl=1 - local fullnum=$(printf "%02d" $dayAnzahl) - - backDir="$(date +%F)"_"$fullnum"_"$backname" - while [ -d $backpath/$backDir ]; do - ((dayAnzahl++)) - fullnum=$(printf "%02d" $dayAnzahl) - backDir="$(date +%F)"_"$fullnum"_"$backname" - done - - tmpordner=$(find $backpath -maxdepth 1 -type d -name "*_"$backname"" \ - | sort | tail --lines=1) - lastBackDir=${tmpordner##*/} -} - get_backup_dir() { local backup_path="$1" @@ -74,11 +56,19 @@ get_backup_dir() echo $backup_dir } -## @fn CreateBackupFolderRemotePC() -## @brief Erstellung des Backupordners fuer den zu sichernden Rechner -CreateBackupFolderRemotePC() +get_last_backup_dir_path() { - mkdir --parents $backpath/$backDir/$remoterechner + local backup_path="$1" + local backup_name="$2" + + local last_backup_dir_path=$(find $backup_path \ + -maxdepth 1 \ + -type d \ + -name "*_""$backup_name" \ + | sort \ + | tail --lines=1) + + echo "$last_backup_dir_path" } ## @fn GetHardLinkOpts() @@ -102,122 +92,6 @@ printfStripLine() printf "####################\n" } -################################################################################ -# Backup VMs -################################################################################ - -## @fn BackupTypOfVMDisc() -## @brief Erstellt ein VM Backup mit dem Typ file oder block -BackupTypOfVMDisc() -{ - if [ "$DiskType" == "File" ]; then - rsync --archive --verbose --sparse --acls --hard-links \ - --relative --progress --human-readable \ - --log-file=$logFile $hardlinkopt \ - -rsh "ssh -p $remoteport" $remoteLogin:$Disk \ - $backRemotePCPath - elif [ "$DiskType" == "Block" ]; then - local readonly DiskName=$(echo $Disk | sed 's/^\///' \ - | sed 's/\//-/g') - local readonly DiskSize=$(ssh -p $remoteport $remoteLogin \ - fdisk --list $Disk \ - | head --lines=1 | awk \ - --field-separator="," '{print $2}' \ - | sed 's/[ \| Bbytes]//g') - local readonly humanSize=$(SizeHumanReadable "$DiskSize") - - printf "DiskSize: %s\n\n" "$humanSize" - ssh -p $remoteport $remoteLogin dd bs=1M if=$Disk \ - | pv --size $DiskSize \ - | gzip --stdout | dd of=$backRemotePCPath/$DiskName.gz - printf "Erstelle %s/%s.gz.sha256\n" $backRemotePCPath $DiskName - sha256sum $backRemotePCPath/$DiskName.gz \ - > $backRemotePCPath/$DiskName.gz.sha256 - fi -} - -## @fn BackupVMDiscs() -## @brief Ermittlung von Virtuellen Festplatten -BackupVMDiscs() -{ - local readonly Disks=$(ssh -p $remoteport $remoteLogin \ - virsh domblklist $VM --details \ - | grep disk \ - | awk '{print $4}') - local logFile="" - local DiskType="" - - for Disk in $Disks; do - printf "Disk ${YELLOW}%s${RESET} von " "$Disk" - printf "${LIGHTBLUE}%s${RESET} wird gesichert\n\n" "$VM" - - DiskType=$(ssh -p $remoteport $remoteLogin virsh vol-info \ - $Disk | grep Typ | awk '{print $2}') - logFile="$backRemotePCPath/backup-$(echo $Disk \ - | sed 's/\//-/g').log" - BackupTypOfVMDisc - done -} - -## @fn WaitVMShutdown() -## @brief wartet bis die VM heruntergefahren ist -WaitVMShutdown() -{ - while [ "$(ssh $remoteLogin virsh list --name | grep $VM)" != "" ]; do - sleep 2; - done -} - -## @fn BackupOnlineVMs() -## @brief Erstellung eines Offline Backups von einer Online VM -## @details VMs die Online sind werden heruntergefahren und dann gesichert. -## Danach werden die VMs wieder gestartet. -BackupOnlineVMs() -{ - onlineVM=$(ssh -p $remoteport $remoteLogin virsh list --name) - - for VM in $onlineVM; do - printfStripLine - printf "Rechner ${LIGHTPURPLE}%s${RESET}; " "$remoterechner" - printf "${LIGHTBLUE}%s${RESET} ist " "$VM" - printf "${LIGHTGREEN}online${RESET}\n" - printf "Fahre %s herunter\n\n" "$VM" - ssh -p $remoteport $remoteLogin virsh shutdown $VM \ - | WaitVMShutdown - BackupVMDiscs - printf "\nFahre %s hoch\n" "$VM" - ssh -p $remoteport $remoteLogin virsh start $VM - printfStripLine - done -} - -## @fn BackupOfflineVMs() -## @brief Erstellung eines Backups von Offline VMs -BackupOfflineVMs() -{ - offlineVM=$(ssh -p $remoteport $remoteLogin virsh list --all \ - | grep 'ausschalten' | awk '{print $2}') - - for VM in $offlineVM; do - printfStripLine - printf "Rechner ${LIGHTPURPLE}%s${RESET}; " "$remoterechner" - printf "${LIGHTBLUE}%s${RESET} ist " "$VM" - printf "${LIGHTRED}offline${RESET}\n\n" - BackupVMDiscs - printfStripLine - done -} - -## @fn BackupVMs() -## @brief Erstellung Backups von den VMs -BackupVMs() -{ - BackupOnlineVMs - BackupOfflineVMs -} - -################################################################################ - ## @fn BackupDirectoryPath() ## @brief Erstellung eines Backups von einem Verzeichniss BackupDirectoryPath() @@ -293,9 +167,6 @@ BackupRemotePC() remoterechner=$(echo $rech | sed 's/.*@\(.*\):.*/\1/') remoteport=$(echo $rech | sed 's/.*@.*:\(.*\)/\1/') - CreateBackupFolderRemotePC - GetHardLinkOpts - backRemotePCPath="$backpath/$backDir/$remoterechner" remoteLogin="$remoteuser@$remoterechner" @@ -306,8 +177,13 @@ BackupRemotePC() backup_remotes() { local backup_path="$1" + local last_backup_dir_path="$2" + local hardlinkopt= local machines=$(cat config.yaml | yq -r '.machines | keys[]') + [ -n "$last_backup_dir_path" ] \ + && hardlinkopt="--link-dest=$last_backup_dir_path" + for machine in $machines; do echo "ℹ️ INFO: create direcotry $backup_path/$machine" mkdir -p $backup_path/$machine @@ -439,6 +315,10 @@ IsBackupOld() ## @brief Hauptablauf des Skriptes main() { + local only_check="$1" + + [ "$only_check" == "check" ] && CheckBackupDate && return 0 + YELLOW="\e[0;33m" LIGHTBLUE="\e[1;34m" LIGHTRED="\e[1;31m" @@ -457,24 +337,6 @@ main() echo mode="incremental" - if [ -n "$1" ]; then - case "$1" in - "full") - mode="$1" - ;; - "check") - CheckBackupDate - exit 0 - ;; - "-h"|"--help"|"help") - usage - exit 0 - ;; - *) - mode="incremental" - ;; - esac - fi backup_path=$(cat config.yaml | yq -r '.backup_path') backup_name=$(cat config.yaml | yq -r '.backup_name') @@ -501,10 +363,15 @@ main() "$backup_path" \ "$backup_name") echo "ℹ️ INFO: backup folder $backup_dir" - local backup_path=$backup_path/$backup_dir - echo "ℹ️ INFO: create folder $backup_path" + local backup_full_path=$backup_path/$backup_dir + echo "ℹ️ INFO: create folder $backup_full_path" + + local last_backup_dir_path=$(get_last_backup_dir_path \ + "$backup_path" \ + "$backup_name") + echo "ℹ️ INFO: last backup dir path $last_backup_dir_path" - backup_remotes "$backup_path" + backup_remotes "$backup_path" "$last_backup_dir_path" return BackupRemotePC -- 2.39.5