From: Bastian Dehn Date: Sat, 25 Nov 2017 10:38:56 +0000 (+0100) Subject: refactor backup X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=3cbf7077577f1647b98053d263d5ca885ec1be39;p=simple-backup.git refactor backup --- diff --git a/backup b/backup index 00f1a87..829cc6c 100755 --- a/backup +++ b/backup @@ -3,103 +3,187 @@ ### macht Hardlinks auf unveränderte Daten zum ### vorhaerigen Backup -# Informationen fuer den Sicherungsserver -sicherungspfad="/images/backup" -sicherungsname="sicherung" -wunschanzahl=20 -mode="incremental" - -if [ -n "$1" ]; then - case "$1" in - "full") - mode="$1" - ;; - *) - mode="incremental" - ;; - esac -fi - -# Sicherungsordnernamen generieren -# und Vorsicherungsordnername ermitteln -sicherungsordner="$(date +%Y-%m-%d)"_"$sicherungsname" -vorsicherungsordner="$(date -d "1 Day ago" +%Y-%m-%d)"_"$sicherungsname" -if [ ! -d $sicherungspfad/$vorsicherungsordner ]; then - tmpordner=$(find $sicherungspfad -maxdepth 1 -type d -name "*_"$sicherungsname"" | sort | tail --lines=1) - vorsicherungsordner=${tmpordner##*/} -fi - -for remoterechner in $(cat rechner.txt); do - remoteuser="root" - # Sicherungsordner fuer den Remote-Host erstellen +SetDefaultVariables() { + sicherungspfad="/images/backup" + sicherungsname="sicherung" + wunschanzahl=20 + mode="incremental" +} + +ReadArguments() { + if [ -n "$1" ]; then + case "$1" in + "full") + mode="$1" + ;; + *) + mode="incremental" + ;; + esac + fi +} + +CreateBackupFolder() { + sicherungsordner="$(date +%Y-%m-%d)"_"$sicherungsname" + vorsicherungsordner="$(date -d "1 Day ago" +%Y-%m-%d)"_"$sicherungsname" + + if [ ! -d $sicherungspfad/$vorsicherungsordner ]; then + tmpordner=$(find $sicherungspfad -maxdepth 1 -type d \ + -name "*_"$sicherungsname"" | sort | tail --lines=1) + vorsicherungsordner=${tmpordner##*/} + fi +} + +CreateBackupFolderRemotePC() { mkdir --parents $sicherungspfad/$sicherungsordner/$remoterechner - # Setze hardlinkopt ob full Sicherung oder incremental +} + +GetHardLinkOpts() { if [ "$mode" == "incremental" ]; then hardlinkopt="--link-dest=$sicherungspfad/$vorsicherungsordner/$remoterechner" fi +} + +printfStripLine() { + printf "####################" + printf "####################" + printf "####################" + printf "####################\n" +} + +################################################################################ +# Backup VMs +################################################################################ + +BackupVMDiscs() { + for Disk in $Disks; do + printf "Disk %s von %s wird gesichert\n\n" "$Disk" "$VM" + rsync --archive --verbose --sparse --acls --hard-links --relative \ + --progress --human-readable \ + --log-file=$backupRemoteRechnerPath/backup-$(echo $Disk | sed 's/\//-/g').log \ + $hardlinkopt \ + $remoteLogin:$Disk $backupRemoteRechnerPath + done +} + +WaitVMShutdown() { + while [ "$(ssh $remoteLogin virsh list --name | grep $VM)" != "" ]; do + sleep 2; + done +} + +BackupOnlineVMs() { + onlineVM=$(ssh $remoteLogin virsh list --name) + + for VM in $onlineVM; do + printfStripLine + printf "Rechner %s; %s ist online\n" "$remoterechner" "$VM" + printf "Fahre %s herunter\n\n" "$VM" + + ssh $remoteLogin virsh shutdown $VM | WaitVMShutdown + Disks=$(ssh $remoteLogin virsh domblklist $VM --details \ + | grep disk \ + | awk '{print $4}') + BackupVMDiscs + + printf "\nFahre %s hoch\n" "$VM" + ssh $remoteLogin virsh start $VM + printfStripLine + done +} + +BackupOfflineVMs() { + offlineVM=$(ssh $remoteLogin virsh list --all | grep 'shut off' | awk '{print $2}') + + for VM in $offlineVM; do + printfStripLine + printf "Rechner %s; %s ist offline\n\n" "$remoterechner" "$VM" - # Sicherung fuer die jeweils angegebenen Pfade - # in pfad.txt erstellen inkl. logs - # bzw. KVM HD Sicherung erstellen + Disks=$(ssh $remoteLogin virsh domblklist $VM --details | grep disk | awk '{print $4}') + BackupVMDiscs + printfStripLine + done +} + +BackupVMs() { + BackupOnlineVMs + BackupOfflineVMs +} + +################################################################################ + +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=$backupRemoteRechnerPath/backup-$(echo $pfad | sed 's/\//-/g').log \ + --filter="- *.qcow2" --filter="- *.raw" \ + $hardlinkopt \ + $remoteLogin:$pfad \ + $backupRemoteRechnerPath + printfStripLine +} + +BackupPathes() { for pfad in $(cat pfad-$remoterechner.txt); do if [ "$pfad" == "virsh" ]; then - # Sicherung von KVM virtuellen Maschinen - # Online VMs werden heruntergefahren, gesichert und dann wieder hochgefahren - # Offline VMs werden direkt gesichert - onlineVM=$(ssh $remoteuser@$remoterechner virsh list --name) - offlineVM=$(ssh $remoteuser@$remoterechner virsh list --all | grep 'shut off' | awk '{print $2}') - for VM in $onlineVM; do - printf "################################################################\n" - printf "Rechner %s; %s ist online\n" "$remoterechner" "$VM" - printf "Fahre %s herunter\n\n" "$VM" - ssh $remoteuser@$remoterechner virsh shutdown $VM | while [ "$(ssh $remoteuser@$remoterechner virsh list --name | grep $VM)" != "" ]; do sleep 2 ; done - Disks=$(ssh $remoteuser@$remoterechner virsh domblklist $VM --details | grep disk | awk '{print $4}') - for Disk in $Disks; do - printf "Disk %s von %s wird gesichert\n\n" "$Disk" "$VM" - rsync --archive --verbose --sparse --acls --hard-links --relative --progress --human-readable --log-file=$sicherungspfad/$sicherungsordner/$remoterechner/backup-$(echo $Disk | sed 's/\//-/g').log $hardlinkopt $remoteuser@$remoterechner:$Disk $sicherungspfad/$sicherungsordner/$remoterechner - done - printf "\nFahre %s hoch\n" "$VM" - ssh $remoteuser@$remoterechner virsh start $VM - printf "################################################################\n" - done - for VM in $offlineVM; do - printf "################################################################\n" - printf "Rechner %s; %s ist offline\n\n" "$remoterechner" "$VM" - Disks=$(ssh $remoteuser@$remoterechner virsh domblklist $VM --details | grep disk | awk '{print $4}') - for Disk in $Disks; do - printf "Disk %s von %s wird gesichert\n\n" "$Disk" "$VM" - rsync --archive --verbose --sparse --acls --hard-links --relative --progress --human-readable --log-file=$sicherungspfad/$sicherungsordner/$remoterechner/backup-$(echo $Disk | sed 's/\//-/g').log $hardlinkopt $remoteuser@$remoterechner:$Disk $sicherungspfad/$sicherungsordner/$remoterechner - done - printf "################################################################\n" - done + BackupVMs else - # Dateipfade werden gesichert - # Es werden Dateien mit *.qcow2 und *.raw ingnoriert - printf "################################################################\n" - printf "Rechner %s; Pfad %s wird gesichert\n\n" "$remoterechner" "$pfad" - rsync --archive --verbose --sparse --acls --hard-links --relative --human-readable --log-file=$sicherungspfad/$sicherungsordner/$remoterechner/backup-$(echo $pfad | sed 's/\//-/g').log --filter="- *.qcow2" --filter="- *.raw" $hardlinkopt $remoteuser@$remoterechner:$pfad $sicherungspfad/$sicherungsordner/$remoterechner - printf "################################################################\n" + BackupDirectoryPath fi done -done - -# Die Anzahl der Backups wird ermittelt -# Alte Backups werden geloesch -# falls mehr als die Wunschanzahl vorhanden sind -backupanzahl=$(ls --directory $sicherungspfad/*/ | grep "_$sicherungsname" | sort | wc --lines) -loeschanzahl=$(($backupanzahl - $wunschanzahl)) - -if [ $backupanzahl -gt $wunschanzahl ] -then - printf "################################################################\n\n" - printf "Es sind %i Sicherungen vorhanden.\n" $backupanzahl - printf "Es werden %i Sicherungen gelöscht.\n\n" $loeschanzahl - ls -d $sicherungspfad/*/ | grep "_$sicherungsname" --max-count=$loeschanzahl | xargs rm --recursive - printf "################################################################\n" -else - printf "################################################################\n\n" - printf "Es wird %i Sicherungen vorhanden.\n\n" $backupanzahl - printf "################################################################\n" -fi +} + +BackupRemotePC() { + remoteuser="root" + + for remoterechner in $(cat rechner.txt); do + CreateBackupFolderRemotePC + GetHardLinkOpts + + backupRemoteRechnerPath="$sicherungspfad/$sicherungsordner/$remoterechner" + remoteLogin="$remoteuser@$remoterechner" + + BackupPathes + done +} + +CleanUpBackups() { + backupanzahl=$(ls --directory $sicherungspfad/*/ | grep "_$sicherungsname" | sort | wc --lines) + loeschanzahl=$(($backupanzahl - $wunschanzahl)) + + if [ $backupanzahl -gt $wunschanzahl ] + then + printfStripLine + printf "\n" + printf "Es sind %i Sicherungen vorhanden.\n" $backupanzahl + printf "Es werden %i Sicherungen gelöscht.\n\n" $loeschanzahl + + ls -d $sicherungspfad/*/ \ + | grep "_$sicherungsname" --max-count=$loeschanzahl \ + | xargs rm --recursive + + printfStripLine + else + printfStripLine + printf "\n" + printf "Es wird %i Sicherungen vorhanden.\n\n" $backupanzahl + printfStripLine + fi +} + +main() { + SetDefaultVariables + ReadArguments + + CreateBackupFolder + + BackupRemotePC + + CleanUpBackups +} +main $* exit 0