-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneDrive-Backup.ps1
134 lines (116 loc) · 3.92 KB
/
OneDrive-Backup.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
126
127
128
129
130
131
132
133
134
# Log
$logFile = "$HOME\Scripts\OneDrive-Backup\OneDrive-BackupLog.txt"
$emailLog = "$HOME\Scripts\OneDrive-Backup\OneDrive-EmailLog.txt"
# Drive locations
$oneDrive = "$HOME\OneDrive"
$netDrive = "D:\DadOneDriveBackup"
# Write-Log appends log service messages to file
function Write-Log {
param(
[string]$message
)
$currentTime = Get-Date
$logMessage = "$currentTime : $message"
Add-Content $logFile $logMessage
Add-Content $emaillog $logMessage
}
# Log
function Log-This {
param(
[string]$message
)
Write-Host $message
Write-Log $message
}
# Send-Email sends email
function Send-Email {
param(
[string]$subject,
[string]$body
)
$one = New-Object -ComObject Outlook.Application
$mail = $one.CreateItem(0)
$mail.to = "[email protected]"
$mail.Subject = $subject
$mail.Body = $body
$mail.Send()
$one.Quit()
}
function Get-DriveContents {
$message = "Verifying updated files from OneDrive"
Log-This $message
$message = "Inspecting OneDrive Content"
Log-This $message
$oneDriveContents = Get-ChildItem -LiteralPath $oneDrive -Recurse
$message = "Inspecting Network Drive Content"
Log-This $message
$netDriveContents = Get-ChildItem -LiteralPath $netDrive -Recurse
return $oneDriveContents, $netDriveContents
}
# Update older files on network drive using write time differences
function Update-BackupDrive {
$oneDriveContents, $netDriveContents = Get-DriveContents
# Keep track
$memory = @{}
# Save files from one drive to memory
ForEach ($file in $oneDriveContents) {
# Add file to memory
$memory[$file.Name] = $file.FullName, $file.LastWriteTime
# Trim C:\ Root path
$cutPath = $file.FullName.Remove(0, $oneDrive.Length+1)
# Add D:\ Root Path
$filePath = $netDrive + "\" + $cutPath
$testPath = Test-Path -LiteralPath $filePath
if (!$testPath) {
# Copy file to network drive
$message = "Copying to Backup Drive: $filePath"
Log-This $message
if ($file.PSIsContainer) {
Copy-Item -Path $file.FullName -Destination $filePath -Recurse -Force
} else {
Copy-Item -Path $file.FullName -Destination $filePath -Force
}
}
}
# Save files from one drive to network drive
ForEach ($file in $netDriveContents) {
# if one drive last write time -gt network drive last write time
if (!$file.PSIsContainer -and $memory.ContainsKey($file.Name) -and $memory[$file.Name][1] -gt $file.LastWriteTime) {
# Copy file from one drive to network drive
$message = "Updating: ", $file.FullName
Log-This $message
Copy-Item -Path $memory[$file.Name][0] -Destination $file.FullName -Force
}
# Trim D:\ Root path
$cutPath = $file.FullName.Remove(0, $netDrive.Length+1)
# Add C:\ Root Path
$filePath = $oneDrive + "\" + $cutPath
$testPath = Test-Path -LiteralPath $filePath
if (!$testPath) {
# Delete file from network drive
$message = "Deleting from Backup Drive: " + $file.FullName
Log-This $message
if ($file.PSIsContainer) {
Remove-Item -Path $file.FullName -Recurse -Force
} else {
Remove-Item -Path $file.FullName -Force
}
}
}
Log-This "Update Complete..."
}
# Main
try {
Update-BackupDrive
$date = Get-Date
$body = Get-Content -Path $emailLog -Encoding UTF8 -Raw
Send-Email -subject "Log File: $date" -body "$body"
Clear-Content -Path $emailLog
} catch {
$message = $_
Log-This $message
$date = Get-Date
$body = Get-Content -Path $emailLog -Encoding UTF8 -Raw
Send-Email -subject "Log File: $date" -body "$body"
Clear-Content -Path $emailLog
}