-
Notifications
You must be signed in to change notification settings - Fork 0
/
robocoper.ps1
125 lines (110 loc) · 5.18 KB
/
robocoper.ps1
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
115
116
117
118
119
120
121
122
123
124
125
$Backups = @(
, @('First Backup', 'C:\Images', 'D:\Backup\First', '0', '')
, @('Second Backup', 'C:\Documents', 'D:\Backup\Documents', '4', '/MT:8 /E /NDL /NP /R:1 /W:1')
# , @('Third Backup', 'C:\Bases', 'D:\Backup\Bases', '4', '')
, @('Fourth Backup', 'C:\Music', 'D:\Backup\Music', '0', '')
<# , @('Fifth Backup', 'C:\PDFs', 'D:\Backup\PDFs', '10', '/MT:8 /E /NDL /NP /R:1 /W:1')
, @('Sixth Backup', 'C:\XXX', 'D:\Backup\Sixth', '4', '') #>
)
$DefaultParameters = "/MT:8 /E /NDL /NP /R:1 /W:1" # Default robocopy parameters
$SendEmail = $true
$SaveLog = $true
if ($SaveLog) {
$LogDir = "D:\Backup\logs"
}
$str = "$($PSStyle.Foreground.BrightYellow)*$($PSStyle.Reset)" * 80 # Separation line for better log view
$LogTmpFile = (New-TemporaryFile).FullName | Rename-Item -NewName { $_ -replace 'tmp$', 'txt' } –PassThru
Start-Transcript -Append $LogTmpFile -UseMinimalHeader
$TotalTime = Measure-Command {
$BackupTable = @()
$Backups | ForEach-Object {
$RobocopyName = $_[0]
$RobocopySource = $_[1]
$RobocopyDestination = $_[2]
$RobocopyDaysToLive = $_[3]
$RobocopyParameters = $_[4]
Write-Host "$str`n[$($Backups.IndexOf($_) + 1)/$($Backups.Count)] Backup: '$RobocopyName'"
$StartTime = Get-Date -UFormat "%y/%m/%d-%H:%M:%S"
$BackupTime = Measure-Command {
if ($RobocopyParameters -eq "") {
$RobocopyParameters = $DefaultParameters
}
if ($RobocopyDaysToLive -ne 0) {
$BackupFolder = $RobocopyDestination
$RobocopyDestination = "$RobocopyDestination\$($RobocopyName.Replace(" ","_"))_$(Get-Date -UFormat '%y%m%d_%H%M%S')"
}
Write-Host "robocopy $RobocopySource $RobocopyDestination $RobocopyParameters"
robocopy $RobocopySource $RobocopyDestination $RobocopyParameters.split(' ') | Write-Host
}
if ($LASTEXITCODE -le 3) {
$Result = "Success"
if ($RobocopyDaysToLive -ne 0) {
Write-Host "`nDelete files older than $RobocopyDaysToLive days:"
$DeleteFromDate = (Get-Date).AddDays(-$RobocopyDaysToLive)
$WhatToDelete = Get-ChildItem -Path $BackupFolder | Where-Object { ($_.CreationTime -le $DeleteFromDate) -and ($_.LastWriteTime -le $DeleteFromDate) }
if ($WhatToDelete.Count -ne 0) {
$WhatToDelete | Remove-Item -Recurse -Force -Verbose
}
else {
Write-Host "Nothing to delete"
}
}
}
else {
$Result = "Failed"
}
$number = $Backups.IndexOf($_) + 1
$BackupTable += [PSCustomObject] @{
'Number' = "$(([string]$number).PadLeft(2,' '))/$($Backups.Count)"
'Name' = $RobocopyName
'Result' = "$Result ($LASTEXITCODE)"
'Backup time' = $BackupTime.ToString("hh\:mm\:ss")
'DaysToLive' = $RobocopyDaysToLive
'Source' = $RobocopySource
'Destination' = $RobocopyDestination
'Start' = $StartTime
'End' = Get-Date -UFormat '%y/%m/%d-%H:%M:%S'
}
}
}
if ($SaveLog) {
Write-Host "$str`nCompress Log file '$LogTmpFile' to '$LogDir\log_$(Get-Date -UFormat "%y%m%d_%H%M%S").zip'"
}
Stop-Transcript
Compress-Archive -Path $LogTmpFile -DestinationPath $LogDir\log_$(Get-Date -UFormat "%y%m%d_%H%M%S").zip
$BackupTable = $BackupTable | Format-Table -Property @{n = "Number"; e = { $_.Number }; a = "center" },
@{n = "Name"; e = { $_.'Name' }; a = "center" },
@{n = "Result"; e = { $_.'Result' }; a = "center" },
@{n = "Backup time"; e = { $_.'Backup time' }; a = "center" },
@{n = "DTL"; e = { $_.DaysToLive }; a = "center" },
@{n = "Source"; e = { $_.'Source' }; a = "left" },
@{n = "Destination"; e = { $_.'Destination' }; a = "left" },
@{n = "Start time"; e = { $_.'Start' }; a = "center" },
@{n = "End time"; e = { $_.'End' }; a = "center" } -AutoSize | Out-String -Width 512
if ($SendEmail) {
$body = Get-Content -Path $LogTmpFile | Out-String
$body = "Total backup time: $($TotalTime.ToString('hh\:mm\:ss'))`n$($BackupTable)" + $body
$email = get-content -Path $PSScriptRoot\email.json -Raw | ConvertFrom-Json
$EmailSubject = "robocoper"
$secpasswd = ConvertTo-SecureString $email.EmailPassword -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($email.EmailFrom, $secpasswd)
$encoding = [System.Text.Encoding]::UTF8
[System.Net.ServicePointManager]::SecurityProtocol = "Tls, TLS11, TLS12" # Uncomment this line for TLS not SSL
Send-MailMessage `
-To $email.EmailTo `
-Subject $EmailSubject `
-Body $body `
-SmtpServer $email.EmailSmtpServer `
-Credential $mycreds `
-Port $email.EmailSmtpPort `
-UseSsl `
-from "$($env:COMPUTERNAME) <$($email.EmailFrom)>" `
-Encoding $encoding `
-WarningAction:SilentlyContinue
Write-Host "Send e-mail to $($email.EmailTo)"
}
$str
Remove-Item -Path $LogTmpFile -Force -Verbose
$str
$BackupTable
Write-Host "Total backup time: $($TotalTime.ToString('hh\:mm\:ss'))"