-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiPathRemoteToLocalSync-Old.ps1
366 lines (288 loc) · 12.1 KB
/
MultiPathRemoteToLocalSync-Old.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<#
.SYNOPSIS
WinSCP - MultiPath Remote to Local Sync
v1.0.0
.DESCRIPTION
Modified version of KeepLocalUpToDate.WinSCPextension.ps1 script which is provided in the extension folder of WinSCP 5.9.2+.
Instead of having to use the file mask option to include/exclude subfolders based off a root folder, this script allows for
input of both multiple remote and local folders that individually synchronized. The remote and local paths are labeled as
Primary, Secondary, and Tertiary.
.PARAMETER localPathPrimary
Primary local path
.PARAMETER remotePathPrimary
Primary remote path
.PARAMETER localPathPrimary
Secondary local path
.PARAMETER remotePathPrimary
Secondary remote path
.PARAMETER localPathPrimary
Tertiary local path
.PARAMETER remotePathPrimary
Tertiary remote path
.PARAMETER sessionLogPath
Path and file of the log that is by default disabled if not defined at the time of running the script. The log output is the verbose of all files found on both the local and remote sources.
.PARAMETER interval
Set time that the script will pause and wait before starting over. Sleep timer is in seconds and during the wait period the
script can be canceled by pressing Ctrl-C. It has a default value set to 30 seconds, but can be adjusted at the time of
running the script.
.EXAMPLE
.\MultiPathRemoteToLocalSync.ps1 -localPathPrimary "E:\Backup\PrimaryVolume\" -remotePathPrimary "/Vol_Pri/" -localPathSecondary "E:\Backup\SecondaryVolume\" -remotePathSecondary "/Vol_Sec/" -localPathTertiary "E:\Backup\TertiaryVolume\" -remotePathTertiary "/Vol_Ter/" -sessionLogPath "E:\Backup\SyncLog.txt" -interval "300" -delete -continueOnError
.\MultiPathRemoteToLocalSync.ps1 -localPathPrimary "E:\Backup\PrimaryVolume\" -remotePathPrimary "/Vol_Pri/" -localPathSecondary "E:\Backup\SecondaryVolume\" -remotePathSecondary "/Vol_Sec/" -localPathTertiary "E:\Backup\TertiaryVolume\" -remotePathTertiary "/Vol_Ter/"
.NOTES
With the initial version of this script it setup for 3 remote/local folders that are manually set through parameters. The
original WinSCP script and documentation - https://winscp.net/eng/docs/library_example_keep_local_directory_up_to_date
.LINK
https://github.com/delta911turbo/MultiPathRemoteToLocalSync
#>
param (
[Parameter(Mandatory = $True)]
$localPathPrimary,
[Parameter(Mandatory = $True)]
$remotePathPrimary,
[Parameter(Mandatory = $True)]
$localPathSecondary,
[Parameter(Mandatory = $True)]
$remotePathSecondary,
[Parameter(Mandatory = $True)]
$localPathTertiary,
[Parameter(Mandatory = $True)]
$remotePathTertiary,
$sessionLogPath = $Null,
$interval = 30,
[switch]
$delete
[switch]
$continueOnError
)
function SyncPrimary ($localPathPrimary, $remotePathPrimary, $sessionLogPath, $interval, $delete, $continueOnError) {
Add-Type -Path ("..\WinSCPnet.dll")
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.example.com"
UserName = "exampleuser"
Password = "examplepassword"
}
$session = New-Object WinSCP.Session
# Optimization
# (do not waste time enumerating files, if you do not need to scan for deleted files)
if ($delete)
{
$localFilesPrimary = Get-ChildItem -Recurse -Path $localPathPrimary
}
$session.SessionLogPath = $sessionLogPath
Write-Host "Connecting..."
$session.Open($sessionOptions)
Write-Host "Synchronizing changes between $localPathPrimary and $remotePathPrimary"
$result = $session.SynchronizeDirectories([WinSCP.SynchronizationMode]::Local, $localPathPrimary, $remotePathPrimary, $delete)
$changed = $False
if (!$result.IsSuccess)
{
if ($continueOnError)
{
Write-Host ("Error: {0}" -f $result.Failures[0].Message)
$changed = $True
}
else
{
$result.Check()
}
}
# Print updated files
foreach ($download in $result.Downloads)
{
Write-Host ("{0} <= {1}" -f $download.Destination, $download.FileName)
$changed = $True
}
if ($delete)
{
# scan for removed local files (the $result does not include them)
$localFilesPrimary2 = Get-ChildItem -Recurse -Path $localPathPrimary
if ($localFilesPrimary)
{
$changes = Compare-Object -DifferenceObject $localFilesPrimary2 -ReferenceObject $localFilesPrimary
$removedFiles =
$changes |
Where-Object -FilterScript { $_.SideIndicator -eq "<=" } |
Select-Object -ExpandProperty InputObject
# Print removed local files
foreach ($removedFile in $removedFiles)
{
Write-Host ("{0} deleted" -f $removedFile)
$changed = $True
}
}
$localFilesPrimary = $localFilesPrimary2
}
if ($changed)
{
}
else
{
Write-Host "No change."
}
write-host "Disconnecting..."
# Disconnect, clean up
$session.Dispose()
}
function SyncSecondary ($localPathSecondary, $remotePathSecondary, $sessionLogPath, $interval, $delete, $continueOnError) {
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.example.com"
UserName = "exampleuser"
Password = "examplepassword"
}
$session = New-Object WinSCP.Session
# Optimization
# (do not waste time enumerating files, if you do not need to scan for deleted files)
if ($delete)
{
$localFilesSecondary = Get-ChildItem -Recurse -Path $localPathSecondary
}
$session.SessionLogPath = $sessionLogPath
Write-Host "Connecting..."
$session.Open($sessionOptions)
Write-Host "Synchronizing changes between $localPathSecondary and $remotePathSecondary"
$result = $session.SynchronizeDirectories([WinSCP.SynchronizationMode]::Local, $localPathSecondary, $remotePathSecondary, $delete)
$changed = $False
if (!$result.IsSuccess)
{
if ($continueOnError)
{
Write-Host ("Error: {0}" -f $result.Failures[0].Message)
$changed = $True
}
else
{
$result.Check()
}
}
# Print updated files
foreach ($download in $result.Downloads)
{
Write-Host ("{0} <= {1}" -f $download.Destination, $download.FileName)
$changed = $True
}
if ($delete)
{
# scan for removed local files (the $result does not include them)
$localFilesSecondary2 = Get-ChildItem -Recurse -Path $localPathSecondary
if ($localFilesSecondary)
{
$changes = Compare-Object -DifferenceObject $localFilesSecondary2 -ReferenceObject $localFilesSecondary
$removedFiles =
$changes |
Where-Object -FilterScript { $_.SideIndicator -eq "<=" } |
Select-Object -ExpandProperty InputObject
# Print removed local files
foreach ($removedFile in $removedFiles)
{
Write-Host ("{0} deleted" -f $removedFile)
$changed = $True
}
}
$localFilesSecondary = $localFilesSecondary2
}
if ($changed)
{
}
else
{
Write-Host "No change."
}
Write-Host
# Disconnect, clean up
$session.Dispose()
}
function SyncTertiary ($localPathTertiary, $remotePathTertiary, $continueOnError, $sessionLogPath, $interval, $delete, $continueOnError) {
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.example.com"
UserName = "exampleuser"
Password = "examplepassword"
}
$session = New-Object WinSCP.Session
# Optimization
# (do not waste time enumerating files, if you do not need to scan for deleted files)
if ($delete)
{
$localFilesTertiary = Get-ChildItem -Recurse -Path $localPathTertiary
}
$session.SessionLogPath = $sessionLogPath
Write-Host "Connecting..."
$session.Open($sessionOptions)
Write-Host "Synchronizing changes between $localPathTertiary and $remotePathTertiary"
$result = $session.SynchronizeDirectories([WinSCP.SynchronizationMode]::Local, $localPathTertiary, $remotePathTertiary, $delete)
$changed = $False
if (!$result.IsSuccess)
{
if ($continueOnError)
{
Write-Host ("Error: {0}" -f $result.Failures[0].Message)
$changed = $True
}
else
{
$result.Check()
}
}
# Print updated files
foreach ($download in $result.Downloads)
{
Write-Host ("{0} <= {1}" -f $download.Destination, $download.FileName)
$changed = $True
}
if ($delete)
{
# scan for removed local files (the $result does not include them)
$localFilesTertiary2 = Get-ChildItem -Recurse -Path $localPathTertiary
if ($localFilesTertiary)
{
$changes = Compare-Object -DifferenceObject $localFilesTertiary2 -ReferenceObject $localFilesTertiary
$removedFiles =
$changes |
Where-Object -FilterScript { $_.SideIndicator -eq "<=" } |
Select-Object -ExpandProperty InputObject
# Print removed local files
foreach ($removedFile in $removedFiles)
{
Write-Host ("{0} deleted" -f $removedFile)
$changed = $True
}
}
$localFilesTertiary = $localFilesTertiary2
}
if ($changed)
{
if ($beep)
{
[System.Console]::Beep()
}
}
else
{
Write-Host "No change."
}
Write-Host
# Disconnect, clean up
$session.Dispose()
}
# PowerShell While Loop
$i =9
While ($i -gt 8) {
SyncPrimary $sessionUrl $localPathPrimary $remotePathPrimary $delete $beep $continueOnError $sessionLogPath
SyncSecondary $sessionUrl $localPathSecondary $remotePathSecondary $delete $beep $continueOnError $sessionLogPath
SyncTertiary $sessionUrl $localPathTertiary $remotePathTertiary $delete $beep $continueOnError $sessionLogPath
Write-Host "Waiting for $interval seconds, press Ctrl+C to abort..."
$wait = [int]$interval
# Wait for 1 second in a loop, to make the waiting breakable
while ($wait -gt 0)
{
Start-Sleep -Seconds 1
$wait--
}
}
# Never exits cleanly
exit 1