From 75fc420e1cf2b2e45fdd3d2ad6d0f851c7642857 Mon Sep 17 00:00:00 2001 From: Michael Grote Date: Thu, 18 Mar 2021 08:29:50 +0100 Subject: [PATCH] first commit --- Daten_kopieren_irantu.ps1 | 87 ++++++++++++++++++++++++++++ fileserver2_sichern.ps1 | 65 +++++++++++++++++++++ fileserver2_sichern_dryrun.ps1 | 63 ++++++++++++++++++++ replikation_dry_ag.ps1 | 93 +++++++++++++++++++++++++++++ replikation_dry_mg.ps1 | 103 +++++++++++++++++++++++++++++++++ 5 files changed, 411 insertions(+) create mode 100644 Daten_kopieren_irantu.ps1 create mode 100644 fileserver2_sichern.ps1 create mode 100644 fileserver2_sichern_dryrun.ps1 create mode 100644 replikation_dry_ag.ps1 create mode 100644 replikation_dry_mg.ps1 diff --git a/Daten_kopieren_irantu.ps1 b/Daten_kopieren_irantu.ps1 new file mode 100644 index 0000000..b94a045 --- /dev/null +++ b/Daten_kopieren_irantu.ps1 @@ -0,0 +1,87 @@ +############################################################################# +# Michael Grote - 21.11.2019 # +# Kopierscript für Daueraufgaben # +# Script "mirrored" bis zu 6 komplette Ordner # +# Es wird jeweils ein Log pro Kopiervorgang angelegt # +# nicht benötigt Ordner MÜSSEN auskommentiert werden # +# - sowohl bei den Variablen als auch de Kopiervorgängen selber # +# Programme können auch auskommentiert werden wenn nciht benötigt # +############################################################################# + + +#Variablen +#Variablen setzen +#variablendeklaration pfade +#scriptpfad ermitteln +#funktion get-scriptdirectory definieren +function Get-ScriptDirectory { + $Invocation = (Get-Variable MyInvocation -Scope 1).Value + Split-Path $Invocation.MyCommand.Path +} +#funktion an variable zuweisen +$scriptpfad = Get-ScriptDirectory +#hostname +$hostname_ist = $env:computername +$hostname_soll = "irantu" +#ziele/quellen +$quelle1 = "C:\VM" +$quelle2 = "C:\Portable_Programme" +$quelle3 = "C:\Users\mg\Desktop" +$ziel1="$scriptpfad\$hostname_ist\VM" +$ziel2="$scriptpfad\$hostname_ist\Portable_Programme" +$ziel3="$scriptpfad\$hostname_ist\Desktop" +# Hostname prüfen +if ($hostname_ist -eq $hostname_soll) + { + Write-Host "Host = $hostname_ist" + } + else + { + Write-Host "Falscher Host(SOLL: '$hostname_soll' IST:$hostname_ist)" + start-sleep 5 + Exit 1 + } + + + +# Prüfungen Ordner existend und kopieren +if(Test-Path $ziel1) + { + robocopy $quelle1 $ziel1 /MIR /ETA /R:3 /W:10 + } +else + { + New-Item "$ziel1" -ItemType Directory + robocopy $quelle1 $ziel1 /MIR /ETA /R:3 /W:10 + } +if(Test-Path $ziel2) + { + robocopy $quelle2 $ziel2 /MIR /ETA /R:3 /W:10 + } +else + { + New-Item "$ziel2" -ItemType Directory + robocopy $quelle2 $ziel2 /MIR /ETA /R:3 /W:10 + } +if(Test-Path $ziel3) + { + robocopy $quelle3 $ziel3 /MIR /ETA /R:3 /W:10 + } +else + { + New-Item "$ziel3" -ItemType Directory + robocopy $quelle3 $ziel3 /MIR /ETA /R:3 /W:10 + } +#Rückgabecoce robocopy auswerten +#robocopy exit codes +#https://support.microsoft.com/en-us/help/954404/return-codes-that-are-used-by-the-robocopy-utility-in-windows-server-2 +if ($lastexitcode -eq 1) + { + write-host "Keine Fehler!" + } +else + { + #`n ist newline + write-host `n`n`n`n`n`n`n`n`n`n`n`n"Es sind Fehler aufgetreten!"`n`n`n`n`n`n`n`n`n`n`n`n + start-sleep 43200 + } diff --git a/fileserver2_sichern.ps1 b/fileserver2_sichern.ps1 new file mode 100644 index 0000000..b9d778b --- /dev/null +++ b/fileserver2_sichern.ps1 @@ -0,0 +1,65 @@ +#funktion get-scriptdirectory definieren um den scriptpfad ermitteln +function Get-ScriptDirectory { + $Invocation = (Get-Variable MyInvocation -Scope 1).Value + Split-Path $Invocation.MyCommand.Path +} +#funktion an variable zuweisen +$scriptpfad = Get-ScriptDirectory +#hostname +$hostname_ist = $env:computername +$hostname_soll = "irantu" +#ziele/quellen +$quelle1 ="\\fileserver2.grote.lan\mg\" +$ziel1="$scriptpfad\backup\mg" +$quelle2 ="\\fileserver2.grote.lan\Musik\" +$ziel2="$scriptpfad\backup\Musik" +$quelle3 ="\\fileserver2.grote.lan\amd\" +$ziel3="$scriptpfad\backup\amd" +$quelle4 ="\\fileserver2.grote.lan\backup\" +$ziel4="$scriptpfad\backup\backup" +$quelle5 ="\\fileserver2.grote.lan\Videos\Filme - ueberragende" +$ziel5="$scriptpfad\backup\Videos\Filme - ueberragende" +$quelle6 ="\\fileserver2.grote.lan\Videos\Serien - überragende" +$ziel6="$scriptpfad\backup\Videos\Serien - ueberragende" +$quelle7 ="\\fileserver2.grote.lan\bilder" +$ziel7="$scriptpfad\backup\bilder" +$quelle8 ="" +$ziel8="" +$quelle9 ="\\fileserver2.grote.lan\proxmox" +$ziel9="$scriptpfad\backup\proxmox" + +# Hostname prüfen +if ($hostname_ist -eq $hostname_soll) + { + Write-Host "Host = $hostname_ist" + } + else + { + Write-Host "Falscher Host(SOLL: '$hostname_soll' IST:$hostname_ist)" + start-sleep 5 + Exit 1 + } + +robocopy $quelle1 $ziel1 /MIR /R:3 /W:10 /XD "\\fileserver2.grote.lan\mg\Programme\Spiele - nicht gesichert" +robocopy $quelle2 $ziel2 /MIR /R:3 /W:10 +robocopy $quelle3 $ziel3 /MIR /R:3 /W:10 +robocopy $quelle4 $ziel4 /MIR /R:3 /W:10 +robocopy $quelle5 $ziel5 /MIR /R:3 /W:10 +robocopy $quelle6 $ziel6 /MIR /R:3 /W:10 +robocopy $quelle7 $ziel7 /MIR /R:3 /W:10 +robocopy $quelle8 $ziel8 /MIR /R:3 /W:10 +robocopy $quelle9 $ziel9 /MIR /R:3 /W:10 + +#Rückgabecoce robocopy auswerten +#robocopy exit codes +#https://support.microsoft.com/en-us/help/954404/return-codes-that-are-used-by-the-robocopy-utility-in-windows-server-2 +if ($lastexitcode -eq 1) + { + write-host "Keine Fehler!" + } +else + { + #`n ist newline + write-host `n`n`n`n`n`n`n`n`n`n`n`n"Es sind Fehler aufgetreten!"`n`n`n`n`n`n`n`n`n`n`n`n + start-sleep 43200 + } diff --git a/fileserver2_sichern_dryrun.ps1 b/fileserver2_sichern_dryrun.ps1 new file mode 100644 index 0000000..c3f6b38 --- /dev/null +++ b/fileserver2_sichern_dryrun.ps1 @@ -0,0 +1,63 @@ +#funktion get-scriptdirectory definieren um den scriptpfad ermitteln +function Get-ScriptDirectory { + $Invocation = (Get-Variable MyInvocation -Scope 1).Value + Split-Path $Invocation.MyCommand.Path +} +#funktion an variable zuweisen +$scriptpfad = Get-ScriptDirectory +#hostname +$hostname_ist = $env:comput +ername +$hostname_soll = "irantu" +#ziele/quellen +$quelle1 ="\\fileserver2.grote.lan\mg\" +$ziel1="$scriptpfad\backup\mg" +$quelle2 ="\\fileserver2.grote.lan\Musik\" +$ziel2="$scriptpfad\backup\Musik" +$quelle3 ="\\fileserver2.grote.lan\amd\" +$ziel3="$scriptpfad\backup\amd" +$quelle4 ="\\fileserver2.grote.lan\backup\" +$ziel4="$scriptpfad\backup\backup" +$quelle5 ="\\fileserver2.grote.lan\Videos\Filme - ueberragende" +$ziel5="$scriptpfad\backup\Videos\Filme - ueberragende" +$quelle6 ="\\fileserver2.grote.lan\Videos\Serien - überragende" +$ziel6="$scriptpfad\backup\Videos\Serien - ueberragende" +$quelle7 ="\\fileserver2.grote.lan\bilder" +$ziel7="$scriptpfad\backup\bilder" +$quelle8 ="\\fileserver2.grote.lan\videos\Youtube" +$ziel8="$scriptpfad\backup\Videos\youtube" + +# Hostname prüfen +if ($hostname_ist -eq $hostname_soll) + { + Write-Host "Host = $hostname_ist" + } + else + { + Write-Host "Falscher Host(SOLL: '$hostname_soll' IST:$hostname_ist)" + start-sleep 5 + Exit 1 + } + +robocopy $quelle1 $ziel1 /L /MIR /R:3 /W:10 /XD "\\fileserver2.grote.lan\mg\Programme\Spiele - nicht gesichert" +robocopy $quelle2 $ziel2 /L /MIR /R:3 /W:10 +robocopy $quelle3 $ziel3 /L /MIR /R:3 /W:10 +robocopy $quelle4 $ziel4 /L /MIR /R:3 /W:10 +robocopy $quelle5 $ziel5 /L /MIR /R:3 /W:10 +robocopy $quelle6 $ziel6 /L /MIR /R:3 /W:10 +robocopy $quelle7 $ziel7 /L /MIR /R:3 /W:10 +robocopy $quelle8 $ziel8 /L /MIR /R:3 /W:10 + +#Rückgabecoce robocopy auswerten +#robocopy exit codes +#https://support.microsoft.com/en-us/help/954404/return-codes-that-are-used-by-the-robocopy-utility-in-windows-server-2 +if ($lastexitcode -eq 1) + { + write-host "Keine Fehler!" + } +else + { + #`n ist newline + write-host `n`n`n`n`n`n`n`n`n`n`n`n"Es sind Fehler aufgetreten!"`n`n`n`n`n`n`n`n`n`n`n`n + start-sleep 43200 + } diff --git a/replikation_dry_ag.ps1 b/replikation_dry_ag.ps1 new file mode 100644 index 0000000..56e1a98 --- /dev/null +++ b/replikation_dry_ag.ps1 @@ -0,0 +1,93 @@ +# Variablen +# Quellen als Array, Reihenfolge in beiden Arrays muss übereinstimmen +# also wenn Quelle Nr. 1 Musik bei mir ist, muss das Ziiel Nr. 1 Musik bei Papa sein +$quelle =@( + "${hostname_ag}\musik", + "${hostname_ag}\bilder", + "${hostname_ag}\dokumente", + "${hostname_ag}\backup", + "${hostname_ag}\rest" + ) + +# Ziele als Array, Reihenfolge in beiden Arrays muss übereinstimmen +$ziel =@( + "${hostname_mg}\musik", + "${hostname_mg}\bilder", + "${hostname_mg}\dokumente", + "${hostname_mg}\backup", + "${hostname_mg}\rest" + ) +# restliche Variablen +$pfad_ag = "\\NAS.XXXXX\replikation" # wo soll beim jeweils anderen alls DARIN gespeichert werden +$pfad_mg = "\\fileserver2.grote.lan\replikation" +$zeit_countdown = 2 # ohne Anführungszeichen, sonst ist es ein String +$timestamp = get-timestamp # Ruft Funktion get-timestamp auf +$scriptpfad = Get-ScriptDirectory # setzt $scriptpfad auf den Wert von Get-ScriptDirectory +$bandwidth_ipg = 64 + +# Funktion: speichert den aktuellen PFad(vgl. pwd) in Get-ScriptDirectory +function Get-ScriptDirectory { + $Invocation = (Get-Variable MyInvocation -Scope 1).Value + Split-Path $Invocation.MyCommand.Path +} + +# Funktion: countdown, zählt von einem Wert herunter bis 0 +# der Wert $laufzeit muss übergeben werden, muss int sein, gibt länge des Countdowns an +# Bsp.: set-countdown -laufzeit $zeit_countdown oder set-countdown -laufzeit 5 +function set-countdown([int]$laufzeit){ + for ($laufzeit; $laufzeit -gt 0; $laufzeit--){ + Start-Sleep -s 1 # wartet 1 Sekunde + write-host -ForegroundColor RED $laufzeit + } +} + +# Funktion: setze Timestamp, mit Rückgabewert +function get-timestamp{ + $timestamp = (get-date -Format yyyy_MM_dd__HH_mm_ss) + return $timestamp +} + +# Ausgabe Pfade +write-host "====================================" +write-host "Pfad_mg: " +write-host -ForegroundColor Yellow $pfad_mg +write-host "Pfad_ag: " +write-host -ForegroundColor Yellow $pfad_ag +write-host -ForegroundColor RED "Wenn die Pfade nicht passen hier abbrechen! ("$zeit_countdown"sec/Strg+C))" +write-host "====================================" +set-countdown -laufzeit $zeit_countdown # ruft Funktion Countdown auf + +# Prüfe ob Quellordner existieren +write-host "Prüfe Quellordner" +write-host "====================================" +for ($i=0; $i -lt $quelle.length; $i++){ + if((Test-Path $quelle[$i]) -eq $false){ + write-host -ForegroundColor RED "Quellordner:" $quelle[$i] "existiert nicht!" + Exit # beendet Script + +# Zielordner erstellen +# -Force gibt keine Fehler aus falls der Ordner schon existiert(vgl. mkdir -p) +write-host "Erstelle Zielordner" +write-host "====================================" +for ($i=0; $i -lt $ziel.length; $i++){ + New-Item -ItemType "directory" -Force -Path $ziel[$i] +} + +# Ruft Robocopy auf +for ($i=0; $i -lt $quelle.length; $i++){ + for ($i=0; $i -lt $ziel.length; $i++){ + write-host "`n====================================" + write-host "Von: " $quelle[$i]"`nNach:" $ziel[$i] + write-host "====================================" + robocopy $quelle[$i] $ziel[$i] /L /MIR /R:3 /W:10 /DST /TEE /IPG:$bandwidth_ipg /UNILOG+:"$scriptpfad\$timestamp.log" + # Robocopy Params + # /DST = Kompensiert Zeitunterschiede von einer Stunde aufgrund der Sommerzeit. + # /tee = Schreibt die Status Ausgabe in das Konsolenfenster sowie in die Protokolldatei. + # /L = dry-run + # /IPG $bandwidth_ipg https://www.deviousweb.com/2018/01/09/robocopy-throttle-bandwidth/ + } +} + + +# ToDo +# [ ] Meldung Fehler + Abbruch diff --git a/replikation_dry_mg.ps1 b/replikation_dry_mg.ps1 new file mode 100644 index 0000000..6a824a0 --- /dev/null +++ b/replikation_dry_mg.ps1 @@ -0,0 +1,103 @@ +# Quellen als Array, Reihenfolge in beiden Arrays muss übereinstimmen +# also wenn Quelle Nr. 1 Musik bei mir ist, muss das Ziiel Nr. 1 Musik bei Papa sein +$quelle =@( + # ich + "${pfad_mg}\amd", + "${pfad_mg}\bilder", + "${pfad_mg}\mg", + "${pfad_mg}\musik", + "${pfad_mg}\hm", + "${pfad_mg}\proxmox", + "${pfad_mg}\tmp", + "${pfad_mg}\backup" + ) + +# Ziele als Array, Reihenfolge in beiden Arrays muss übereinstimmen +$ziel =@( + #ich + "${pfad_ag}\amd", + "${pfad_ag}\bilder", + "${pfad_ag}\mg", + "${pfad_ag}\musik", + "${pfad_ag}\hm", + "${pfad_ag}\proxmox", + "${pfad_ag}\tmp", + "${pfad_ag}\backup" + ) + + +# Funktion: speichert den aktuellen Pfad(vgl. pwd) in Get-ScriptDirectory +function Get-ScriptDirectory { + $Invocation = (Get-Variable MyInvocation -Scope 1).Value + Split-Path $Invocation.MyCommand.Path +} + +# Funktion: countdown, zählt von einem Wert herunter bis 0 +# der Wert $laufzeit muss übergeben werden, muss int sein, gibt länge des Countdowns an +# Bsp.: set-countdown -laufzeit $zeit_countdown oder set-countdown -laufzeit 5 +function set-countdown([int]$laufzeit){ + for ($laufzeit; $laufzeit -gt 0; $laufzeit--){ + Start-Sleep -s 1 # wartet 1 Sekunde + write-host -ForegroundColor RED $laufzeit + } +} + +# Funktion: setze Timestamp, mit Rückgabewert +function get-timestamp{ + $timestamp = (get-date -Format yyyy_MM_dd__HH_mm_ss) + return $timestamp +} + +# restliche Variablen +$pfad_ag = "\\192.168.3.108\replikation" # wo soll beim jeweils anderen alls DARIN gespeichert werden +$pfad_mg = "\\fileserver2.grote.lan" +$zeit_countdown = 5 # ohne Anführungszeichen, sonst ist es ein String +$timestamp = get-timestamp # Ruft Funktion get-timestamp auf +$scriptpfad = Get-ScriptDirectory # setzt $scriptpfad auf den Wert von Get-ScriptDirectory +$bandwidth_ipg = 128 +# Ausgabe Pfade +write-host "====================================" +write-host "Pfad_mg: " +write-host -ForegroundColor Yellow $pfad_mg +write-host "Pfad_ag: " +write-host -ForegroundColor Yellow $pfad_ag +write-host -ForegroundColor RED "Wenn die Pfade nicht passen hier abbrechen! ("$zeit_countdown"sec/Strg+C))" +write-host "====================================" +set-countdown -laufzeit $zeit_countdown # ruft Funktion Countdown auf + +# Prüfe ob Quellordner existieren +write-host "Prüfe Quellordner" +write-host "====================================" +for ($i=0; $i -lt $quelle.length; $i++){ + if((Test-Path $quelle[$i]) -eq $false){ + write-host -ForegroundColor RED "Quellordner:" $quelle[$i] "existiert nicht!" + Exit # beendet Script + } + } + +# Zielordner erstellen +# -Force gibt keine Fehler aus falls der Ordner schon existiert(vgl. mkdir -p) +write-host "Erstelle Zielordner" +write-host "====================================" +for ($i=0; $i -lt $ziel.length; $i++){ + New-Item -ItemType "directory" -Force -Path $ziel[$i] +} + +# Ruft Robocopy auf +for ($i=0; $i -lt $quelle.length; $i++){ + for ($i=0; $i -lt $ziel.length; $i++){ + write-host "`n====================================" + write-host "Von: " $quelle[$i]"`nNach:" $ziel[$i] + write-host "====================================" + robocopy $quelle[$i] $ziel[$i] /L /MIR /R:3 /W:10 /DST /TEE /IPG:$bandwidth_ipg /UNILOG+:"$scriptpfad\$timestamp.log" + # Robocopy Params + # /DST = Kompensiert Zeitunterschiede von einer Stunde aufgrund der Sommerzeit. + # /tee = Schreibt die Status Ausgabe in das Konsolenfenster sowie in die Protokolldatei. + # /L = dry-run + # /IPG $bandwidth_ipg https://www.deviousweb.com/2018/01/09/robocopy-throttle-bandwidth/ + } +} + + +# ToDo +# [ ] Meldung Fehler + Abbruch