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"
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()
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()
remoterechner=$(echo $rech | sed 's/.*@\(.*\):.*/\1/')
remoteport=$(echo $rech | sed 's/.*@.*:\(.*\)/\1/')
- CreateBackupFolderRemotePC
- GetHardLinkOpts
-
backRemotePCPath="$backpath/$backDir/$remoterechner"
remoteLogin="$remoteuser@$remoterechner"
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
## @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"
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')
"$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