From 23ea2161577b7099c6bfff48722a724d609940ed Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 24 May 2025 14:33:31 +0200 Subject: [PATCH] add backup with hardlinks --- backup | 175 +++++++++++++++++---------------------------------------- 1 file changed, 52 insertions(+), 123 deletions(-) diff --git a/backup b/backup index 47ccea8..e59c178 100755 --- a/backup +++ b/backup @@ -10,21 +10,6 @@ errlog() return $lastexit } -## @fn usage() -## @brief Anzeige von der Hilfe -usage() -{ - echo "backup [full | check | help]" - echo - echo "full: Backup ohne Hardlinks auf das vorherige Backup" - echo "check: Information ueber die letzen Backups" - echo "help: zeigt diese Hilfe an" - echo -} - -## @fn SizeHumanReadable() -## @brief Berechnung lesbare Groesseneinheiten -## @param Dateiegroesse SizeHumanReadable() { local size=$1 @@ -71,106 +56,54 @@ get_last_backup_dir_path() echo "$last_backup_dir_path" } -## @fn GetHardLinkOpts() -## @brief rsync Option ob Hardlinks verwendet werden -## @details Hardlink Option ist default an. Nur bei dem Konsolenargument -## full wird die Hardlink Option ignoriert -GetHardLinkOpts() -{ - if [ "$mode" == "incremental" ]; then - hardlinkopt="--link-dest=$backpath/$lastBackDir/$remoterechner" - fi -} - -## @fn printStripLine() -## @brief Ausgabe eines Trennstreifens auf der Konsole -printfStripLine() -{ - printf "####################" - printf "####################" - printf "####################" - printf "####################\n" -} - -## @fn BackupDirectoryPath() -## @brief Erstellung eines Backups von einem Verzeichniss -BackupDirectoryPath() -{ - local readonly logFile="$backRemotePCPath/backup-$(echo $pfad \ - | sed 's/\//-/g').log" - - printfStripLine - printf "Rechner ${LIGHTPURPLE}%s${RESET}; " "$remoterechner" - printf "Pfad ${YELLOW}%s${RESET} wird gesichert\n\n" "$pfad" - - rsync --archive --verbose --sparse --acls --hard-links --relative \ - --human-readable --log-file=$logFile --filter="- *.qcow2" \ - --filter="- *.raw" $hardlinkopt --rsh "ssh -p $remoteport" \ - $remoteLogin:$pfad $backRemotePCPath - printfStripLine -} - -## @fn BackupPackages() -## @brief Erstellung eines Backups von der Paketliste von apt -BackupPackages() +print_line() { - local readonly packPath="$backRemotePCPath/packages" - - printfStripLine - printf "Rechner ${LIGHTPURPLE}%s${RESET}; " "$remoterechner" - printf "${YELLOW}Packages${RESET} wird gesichert \n\n" - - mkdir --parents $packPath - ssh -p $remoteport $remoteLogin dpkg --get-selections \ - > $packPath/package.list - rsync --archive --verbose --log-file=$packPath/aptsources.log \ - --rsh "ssh -p $remoteport" $remoteLogin:/etc/apt $packPath/ - printfStripLine -} - -## @fn BackupPathes -## @brief Durchfuehrung des Backups -## @details Durch eine Textdatei mit den jeweils aufgelisteten Verzeichnispfaden -## oder Schluesselworter (virsh, packages) wird das Backkup durchgefuehrt -BackupPathes() -{ - if [ ! -f "pfad-$remoterechner" ]; then - printf "FEHLER: pfad-%s existiert nicht!\n" "$remoterechner" - fi - - for pfad in $(cat pfad-$remoterechner); do - if [ "$pfad" == "virsh" ]; then - BackupVMs - elif [ "$pfad" == "packages" ]; then - BackupPackages - else - BackupDirectoryPath - fi + for i in {1..80}; do + printf "#" done + + printf "\n" } -## @fn BackupRemotePC() -## @brief Erstellung des Backupverzeichnisses fuer den Remote-PC -## @details Anhand einer rechner Datei wird fuer den jeweiligen Rechner ein -## Backup durchgefuehrt -BackupRemotePC() +backup_remote_pathes() { - if [ ! -f "rechner" ]; then - printf "FEHLER: Datei rechner existiert nicht!\n" - exit 1 - fi - - local rech="" - - for rech in $(cat rechner); do - remoteuser=$(echo $rech | sed 's/\(.*\)@.*/\1/') - remoterechner=$(echo $rech | sed 's/.*@\(.*\):.*/\1/') - remoteport=$(echo $rech | sed 's/.*@.*:\(.*\)/\1/') - - backRemotePCPath="$backpath/$backDir/$remoterechner" - remoteLogin="$remoteuser@$remoterechner" - - BackupPathes + local backup_path="$1" + local machine="$2" + local last_backup_dir_path="$3" + local hardlinkopt= + local remote_user=$(cat config.yaml \ + | yq -r ".machines.\"$machine\".remote_user") + local host=$(cat config.yaml | yq -r ".machines.\"$machine\".host") + local port=$(cat config.yaml | yq -r ".machines.\"$machine\".port") + local pathes=$(cat config.yaml | yq -r ".machines.\"$machine\".pathes[]") + + local YELLOW="\e[0;33m" + local LIGHTPURPLE="\e[1;35m" + local RESET="\e[0m" + + for path in $pathes; do + print_line + printf "ℹ️ INFO: machine ${LIGHTPURPLE}%s${RESET}; " "$machine" + printf "path ${YELLOW}%s${RESET}\n\n" "$path" + + [ -n "$last_backup_dir_path" ] \ + && hardlinkopt="--link-dest=$last_backup_dir_path/$machine" + + rsync --archive \ + --verbose \ + --sparse \ + --acls \ + --hard-links \ + --relative \ + --human-readable \ + $hardlinkopt \ + --rsh "ssh -p $port" \ + $remote_user@$host:$path \ + $backup_path/$machine \ + || errlog "could not backup $machine with path $path" \ + || return $? + + print_line done } @@ -181,12 +114,16 @@ backup_remotes() 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 + mkdir -p $backup_path/$machine \ + || errlog "could not create directory $backup_path/$machine" \ + || return $? + + backup_remote_pathes "$backup_path" \ + "$machine" \ + "$last_backup_dir_path" \ + || return $? done } @@ -319,12 +256,6 @@ main() [ "$only_check" == "check" ] && CheckBackupDate && return 0 - YELLOW="\e[0;33m" - LIGHTBLUE="\e[1;34m" - LIGHTRED="\e[1;31m" - LIGHTPURPLE="\e[1;35m" - LIGHTGREEN="\e[1;32m" - RESET="\e[0m" echo "⭐ START: backup" @@ -371,11 +302,9 @@ main() "$backup_name") echo "ℹ️ INFO: last backup dir path $last_backup_dir_path" - backup_remotes "$backup_path" "$last_backup_dir_path" + backup_remotes "$backup_full_path" "$last_backup_dir_path" return - BackupRemotePC - MakeHistory CheckBackupDate -- 2.47.3