PowerShell Robocopy Backup Script

Wie versprochen hier nun das PowerShell Robocopy Backup Script mit den implementieren Funktionen Export-PSCredential und Import-PSCredential.
Da Robocopy in der Windows Welt das wahrscheinlich mächtigste Copy Tool ist lassen sich mit diesem Script auch viele verschiedene Anforderungs Szenarien abbilden.
Robocopy selbst ist im Microsoft Resource Kit enthalten bzw. ab Windows Vista / Server 2008 wird Robocopy standardmäßig mit installiert.
Download Windows 2003 Resource Kit

Hier nun das PowerShell Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#Robocopy Backup Script by Michael Reischl /-/ http://www.gamorz.de /-/ V 1.0
#Following Part is for Connection to an Share with Login Credentials
#If you want to use that you have to generate an Password File with the Export-PSCredential function
#Start a Powershell session and Load the Function with ". .c:\path-to\ecred.ps1"
#Generate the file in the Powershell with command "Export-PSCredential", enter the correct login information and Press ok.
#After that you´ll get the password file "credentials.enc.xml" this have to be set in the $dcredfile for destination or $scredfile for source share.
#Import functions
$funcpathex = "c:\path\to\ecred.ps1"
$funcpathim = "c:\path\to\icred.ps1"
. $funcpathex
. $funcpathim
#Create aliases for the new Functions for easier use
new-item -path alias:ecred -value Export-PSCredential |out-null
new-item -path alias:icred -value Import-PSCredential |out-null
#Network Credentials
$dcredfile = "c:\path\to\credentials_dest.enc.xml"                      #Path to Destination Credential File
$scredfile = "c:\path\to\credentials_source.enc.xml"                   #Path to Source Credential File
$scred = icred $scredfile
$dcred = icred $dcredfile
$destination = "\\path\to\destinaton\folder"                         #Backup Destination Share
$source = "\\path\to\source\folder"                               #Backup Source Share
#Map the Network Shares with Login Credentials into the Powershell Session
$map = New-Object -ComObject WScript.Network
$map.MapNetworkDrive("",$destination,0,$dcred.UserName,$dcred.GetNetworkCredential().Password)
$map.MapNetworkDrive("",$source,0,$scred.UserName,$scred.GetNetworkCredential().Password)
#Base Informations for Robocopy
$date = Get-Date -Format dddd.d.MM.yyyy                               #Get the Date in a easy Readable Form.
$destbackup = "$destination\$date"                                  #Backup Destination Folder inside the Share
$SourceFolder = "$source\"                                        #Backup Source Folder inside the Share
$retrys = 3                                                    #RoboCopy Switch for how often retrys Robocopy to copy a File
$wait = 100                                                    #Wait time between Retrys in ms
$minage = 1                                                    #Only files older than $minage (in days) are be moved/copied - Remove Option on Robocopy Executable Part if you dont want to use this filter.
$opt = "MOV"   #Parameter MOV/MOVE/COPY/COPYALL and so on, a list is avaible at http://technet.microsoft.com/de-de/library/cc733145%28v=ws.10%29.aspx
$Logfile = "\\path\where\the\logfile\should\stored\robocopy_$date.log"        #The place for the Robocopy Log Files
$EmailFrom = "[email protected]"                                #Summary Email Sender Address
$EmailTo = "[email protected]"                                     #Summary Email Receiver
$EmailSubject = "Robocopy Backup Completed"                      #Summary Email Subject
$SMTPServer = "192.168.0.1"                                      #Mail Server Address
#$SMTPPort = "25"                                              #Uncomment and change if your Mail Server uses Another SMTP Port as 25
#Create the Backup Destination Folder Based on Date Information
mkdir $destbackup
#Testing - Available Return Codes from Robocopy to use in Control the Script Flow using Error Codes
$RoboCopyExitCodes = @{0 = "No Errors, no copying"; 1 = "No Errors, new Files"; 2 = "Warning, extra
Files"
; 4 = "Warning, mismatching Files"; 8 = "Errors, copy errors";16 = "ERROR, no copy"}
#Copy/Move Folder/Files with Robocopy and Create Logfile + Put Log to a Variable //  /L switch is RoboCopy Test Mode!!
#You can fit here the Robocopy Parameters as you want.
Robocopy $SourceFolder $destbackup /$opt /R:$retrys /W:$wait /NP /TEE /log:$Logfile | Tee-Object -Variable RoboLog
#Get the Robocopy Exit Code for later Identifying Job Status
$ExitCode = $LastExitCode
#Create Variables for Reading Robocpoy Logfile
$null,$StartBegin,$StartEnd,$StopBegin = $RoboLog | Select-String  "----" |% {$_.linenumber}
#Create an Object to put in all Different Informations
$RoboStatus = New-Object object
#Begin of Parsing Log File / in Testing
$robolog[$StartBegin..$StartEnd] | % {
Switch -regex ($_) {
'Started :(.*)' {
Add-Member -InputObject $RoboStatus -Name StartTime `
-Value ($matches[1].trim()) -MemberType NoteProperty
}
'Source :(.*)' {
Add-Member -InputObject $RoboStatus -Name Source `
-Value ($matches[1].trim()) -MemberType NoteProperty
}
'Dest :(.*)' {
Add-Member -InputObject $RoboStatus -Name Destination `
-Value ($matches[1].trim()) -MemberType NoteProperty
}
'Files :(.*)' {
Add-Member -InputObject $RoboStatus -Name FileName `
-Value ($matches[1].trim()) -MemberType NoteProperty
}
'Options :(.*)' {
Add-Member -InputObject $RoboStatus -Name Options `
-Value ($matches[1].trim()) -MemberType NoteProperty
}
}
}
#End of Parsing Log File
#Create the Email Body
$html = ("<tt>Robocopy completed.<br>" +
"<br>Start time :   " + [string]$RoboStatus.StartTime +
"<br>Source :   " + [string]$RoboStatus.Source +
"<br>Destination :  " + [string]$RoboStatus.Destination +
"<br><br>Robocopy return code: <B>$ExitCode</B><br><br><br>" +
"<br>0 = <B>No Errors and no Copies</B>" +
"<br>1 = <B>No Errors and new Files Copied</B>" +
"<br>2 = <B>Warning! Extra Files in Dest. Folder</B>" +
"<br>4 = <B>Mismatching Files</B>" +
"<br>8 = <B>Errors on Copying</B>" +
"<br>16 = <B>Error no Copy</B>" +
"`n<pre>")
# Send E-Mail to Admin / Works only under Powershell v2
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $html -BodyAsHtml -Attachments $Logfile -SmtpServer $SMTPServer
#Unmap Backup Folders
$map.RemoveNetworkDrive($destination)
$map.RemoveNetworkDrive($source)

PowerShell Robocopy Backup Script Robocopy Backup Script
Downloads: 0
Version: 1.0


Oracle RMAN Backups

Sollte jemand verbesserungs Vorschläge haben oder Fragen zu dem Script immer her damit!


wallpaper-1019588
Akiba Pass Festa Fall Edition: Attack on Titan Panel am Sonntag
wallpaper-1019588
[Comic] Black Hammer: Colonel Weird – Cosmagog
wallpaper-1019588
Maden in der Küche: So befreien Sie Ihre Küche von den Maden
wallpaper-1019588
Beste Gaming-Stühle mit Fußstütze