From bf31d7e8462bf71c19438454bae77a2be6e3f3d8 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Tue, 17 Jul 2018 20:35:23 +0200 Subject: [PATCH] change: display delete old backup Loesche alte Backups und zeige diese an. Code optimierung durch neue Funktion LoescheVerzeich- nisse und BackupTypOfVMDisc --- backup | 72 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/backup b/backup index 24cae34..60b322a 100755 --- a/backup +++ b/backup @@ -28,10 +28,13 @@ ReadArguments() CreateBackupFolder() { local dayAnzahl=1 - backDir="$(date +%Y-%m-%d)"_"$(printf "%02d" $dayAnzahl)"_"$backname" + local fullnum=$(printf "%02d" $dayAnzahl) + + backDir="$(date +%Y-%m-%d)"_"$fullnum"_"$backname" while [ -d $backpath/$backDir ]; do ((dayAnzahl++)) - backDir="$(date +%Y-%m-%d)"_"$(printf "%02d" $dayAnzahl)"_"$backname" + fullnum=$(printf "%02d" $dayAnzahl) + backDir="$(date +%Y-%m-%d)"_"$fullnum"_"$backname" done tmpordner=$(find $backpath -maxdepth 1 -type d \ @@ -65,6 +68,26 @@ printfStripLine() # Backup VMs ################################################################################ +BackupTypOfVMDisc() +{ + if [ "$DiskType" == "file" ]; then + rsync --archive --verbose --sparse --acls --hard-links \ + --relative --progress --human-readable \ + --log-file=$logFile $hardlinkopt $remoteLogin:$Disk \ + $backRemotePCPath + elif [ "$DiskType" == "block" ]; then + local readonly DiskName=$(echo $Disk | sed 's/^\///' \ + | sed 's/\//-/g') + local readonly DiskSize=$(ssh $remoteLogin fdisk --list $Disk \ + | head --lines=1 | awk \ + --field-separator="," '{print $2}' \ + | sed 's/[ \| Bbytes]//g') + + ssh $remoteLogin dd bs=1M if=$Disk | pv --size $DiskSize \ + | gzip --stdout | dd of=$backRemotePCPath/$DiskName.gz + fi +} + BackupVMDiscs() { local readonly Disks=$(ssh $remoteLogin virsh domblklist $VM --details \ @@ -80,25 +103,7 @@ BackupVMDiscs() | grep Type | awk '{print $2}') logFile="$backRemotePCPath/backup-$(echo $Disk \ | sed 's/\//-/g').log" - - if [ "$DiskType" == "file" ]; then - rsync --archive --verbose --sparse --acls --hard-links \ - --relative --progress --human-readable \ - --log-file=$logFile \ - $hardlinkopt \ - $remoteLogin:$Disk $backRemotePCPath - elif [ "$DiskType" == "block" ]; then - local readonly DiskName=$(echo $Disk \ - | sed 's/^\///' | sed 's/\//-/g') - local readonly DiskSize=$(ssh $remoteLogin fdisk --list \ - $Disk | head --lines=1 | awk \ - --field-separator="," '{print $2}' \ - | sed 's/[ \| Bbytes]//g') - - ssh $remoteLogin dd bs=1M if=$Disk \ - | pv --size $DiskSize | gzip --stdout \ - | dd of=$backRemotePCPath/$DiskName.gz - fi + BackupTypOfVMDisc done } @@ -155,12 +160,9 @@ BackupDirectoryPath() printfStripLine printf "Rechner %s; Pfad %s wird gesichert\n\n" "$remoterechner" "$pfad" - rsync --archive --verbose --sparse --acls \ - --hard-links --relative --human-readable \ - --log-file=$logFile \ - --filter="- *.qcow2" --filter="- *.raw" \ - $hardlinkopt \ - $remoteLogin:$pfad \ + rsync --archive --verbose --sparse --acls --hard-links --relative \ + --human-readable --log-file=$logFile --filter="- *.qcow2" \ + --filter="- *.raw" $hardlinkopt $remoteLogin:$pfad \ $backRemotePCPath printfStripLine } @@ -208,6 +210,15 @@ BackupRemotePC() done } +LoescheVerzeichnisse() +{ + for i in $(ls --directory $backpath/*/ | grep "_$backname" \ + --max-count=$loeshanzahl); do + printf "Lösche %s\n" $i + rm --recursive $i + done +} + CleanUpBackups() { backupanzahl=$(ls --directory $backpath/*/ \ @@ -222,11 +233,8 @@ CleanUpBackups() printf "\n" printf "Es sind %i Sicherungen vorhanden.\n" $backupanzahl printf "Es werden %i Sicherungen gelöscht.\n\n" $loeschanzahl - - ls --directory $backpath/*/ \ - | grep "_$backname" --max-count=$loeschanzahl \ - | xargs rm --recursive - + LoescheVerzeichnisse + printf "\n" printfStripLine else printfStripLine -- 2.39.5