PowerShell Drivesnapshot Backup Script
Da ich mittlerweile ein Fan von der Imaging Software Drivesnapshot bin habe ich mich mal hingesetzt um das ganze zu Automatisieren.
Rausgekommen ist ein kleines feines PowerShell Script das natürlich an die lokalen Gegebenheiten angepasst werden muss.
Das Script erstellt im Ziel Pfad zuerst einen Ordner mit der Kalenderwoche, in diesem landet ein Voll Image und an den darauffolgenden Tagen jeweils ein Differentielles.
Wenn im Ziel Pfad Images älter als 2 Wochen gefunden werden, löscht das Script diese Automatisch um ein Voll laufen des Datenträgers zu verhindern.
Hier der Code:
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
#Drivesnapshot Backup Script by Michael Reischl http://www.gamorz.de /-/ Alpha 0.2
#This Script takes Images per Week, in every Week Folder you´ll find one full Image and 6 Differential Images.
#The Hash file is Stored directly on C Drive
#Images Older than 2 Weeks got Deleted in Standard Setting
#Base Informations
$date = Get-Date -Format dddd.d.MM.yyyy #Get the Date in a easy Readable Form.
$time = Get-Date -uFormat %T #Get Time when Backup Starts
$week = get-date -uFormat %V #Get Week Number
$destbackup = "\\path\to\your\backup\folder\$week" #Backup Destination Folder with Week Number
$destbackupmk = "\\path\to\your\backup\folder\" #Backup Folder for creating Week Number Folders and Deleting old Images
$Source = "C:" #Source Drives, more than one? then write "C:+D:+E:"
$Logfile = "\\path\to\your\logfile\destination\image_$date.log" #The place for the Drivesnapshot Log Files
$Log = "--Logfile:" #Enable Logging
$vss = "--usevss" #VSS Options are --novss - don´t use vss: --usevss - use vss if available: --forcevss - use vss, if not available exit with error
$EmailFrom = "[email protected]" #Summary Email Sender Address
$EmailTo = "[email protected]" #Summary Email Receiver
$EmailSubject = "Computername Image Completed" #Summary Email Subject
$SMTPServer = "192.168.0.1" #Mail Server Address
$delold = "1" #Delete Images older than 14 Days, set to "0" if you want to keep it.
#Create the Backup Destination Folder Based on Week Number, if the folder exists go further.
if (!(Test-Path -path $destbackupmk\$week))
{
New-Item $destbackupmk\$week -type directory
}
#Folder Check if its empty, yes = create full image no = create differential image
$check = (Get-Childitem $destbackup | Measure-Object).count
#Take the Image and Store the Hash file on C Drive for Diff backups!
if ($check -le 0)
{
c:\snapshot $source $destbackup\`$Disk_`$Type_`$Date_`$Computername.sna -oc:ihash_`$Disk.hsh $vss $Log$Logfile
}
else
{
c:\snapshot $source $destbackup\`$Disk_`$Type_`$Date_`$Computername.sna -hc:ihash_`$Disk.hsh $vss --LogFile:$Logfile
}
#Create the Email Body related to exit Code Success/Failed
if ($lastexitcode -eq 0)
{
$html = ("<tt>Drivesnapshot Image Completed.<br>" +
"<br>Start time : &nbsp;&nbsp;" + [string]$time +
"<br>Source : &nbsp;&nbsp;" + "Computername" +
"<br>Destination : &nbsp;" + [string]$destbackup +
"`n<pre>")
}
else
{
$EmailSubject = "Drivesnapshot Image Failed!!"
$html = ("<tt>Drivesnapshot Image Failed. See the Logs for Details<br>" +
"<br>Start time : &nbsp;&nbsp;" + [string]$time +
"<br>Source : &nbsp;&nbsp;" + "Computername" +
"<br>Destination : &nbsp;" + [string]$destbackup +
"`n<pre>")
}
#New Mail Sending on Powershell v2
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $html -BodyAsHtml -Attachments $Logfile -SmtpServer $SMTPServer
#Check if there are Images older than 2 Weeks and Delete it
if ($delold -eq 1) {$Today = Get-Date
Get-Childitem $destbackupmk -recurse | Where-Object {($Today - $_.LastWriteTime).Days -gt 14} | Remove-Item -recurse}
und hier als File
drivesnapshot_bck_pub
Das Script ist sicherlich nicht perfekt aber ein guter Ansatz auf dem man Aufbauen kann.
Zum schluss möchte ich noch meinen Dank an Tom Ehlert aussprechen der dieses Geniale Stück Software Programmiert hat!
http://www.drivesnapshot.de