-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp_backup.ps1
executable file
·325 lines (258 loc) · 7.89 KB
/
sftp_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
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
# Inspired by, some parts taken from https://winscp.net/eng/docs/library_powershell#example
# SFTP-Backup script using the WinSCP .NET module by Thomas Erbesdobler <t.erbesdobler@team103.com>
# parameters
[CmdletBinding()]
Param(
[switch]$test_emails
)
# Configuration
$hostname = "host"
$username = "user"
$password = "password"
$hostkey = "ssh-rsa 2048 fingerprint"
[string] $remote_source = "/backup"
[string[]] $backup_directories = "test1", "test2"
[string] $local_target = "D:\backup"
[float] $min_age_copy_hours = 6
[int] $min_count_keep = 2
[int] $max_count_transfer_workday = 1
[int] $max_count_transfer_weekend = 10
# EMail notification
$email_enabled=$True
$custom_subject_prefix='subject'
$mail_from='sender address'
$mail_to = @()
$rcpt1 = 'receiver', 'smtp server', 25
$mail_to = $rcpt1,$null
# Variable initialization
$email_message = ""
# Functions
function log($msg, $err=$False) {
if ($err) {
write-host -ForegroundColor Red $msg
} else {
write-host $msg
}
if ($email_enabled) {
$script:email_message += "`n"
if ($err) {
$script:email_message += "ERROR: $($msg)`n"
} else {
$script:email_message += $msg
}
}
}
function log_exit($exit_code) {
if ($email_enabled)
{
$script:email_message += "`n`nEnding at $(Get-Date) "
if ($exit_code -ne 0) {
$pr=[System.Net.Mail.MailPriority]::High
$subject = "$($custom_subject_prefix)failed"
$script:email_message += "with failures."
} else {
$pr=[System.Net.Mail.MailPriority]::Normal
$subject = "$($custom_subject_prefix)succeeded"
$script:email_message += "successfully."
}
foreach ($receiver in $mail_to) {
if($receiver)
{
$params = @{
"From" = $mail_from
"To" = $receiver[0]
"Priority" = $pr
"Subject" = $subject
"Body" = $email_message
"Encoding" = [System.Text.Encoding]::UTF8
"SmtpServer" = $receiver[1]
"Port" = $receiver[2]
"DeliveryNotificationOption" = [System.Net.Mail.DeliveryNotificationOptions]::None
}
$null = send-mailmessage @params
}
}
}
exit $exit_code
}
function ensure_dir([string]$dir) {
if (!(Test-Path -Path $dir)) {
$null = New-Item -ItemType directory -Path $dir
}
}
function is_weekend([System.Datetime]$date) {
if ($date.DayOfWeek -eq [System.DayOfWeek]::Friday -and
$date.Hour -gt 18) {
return $True
}
if ($date.DayOfWeek -eq [System.DayOfWeek]::Saturday) {
return $True
}
if ($date.DayOfWeek -eq [System.DayOfWeek]::Sunday -and
$date.Hour -lt 6) {
return $True
}
return $False
}
function backup($session, [string]$dir)
{
$ret_code = 0
[System.DateTime] $now = Get-Date
$remote = "$remote_source/$dir"
$local = (Join-Path $local_target $dir)
# Retrieve files in remote directory
$remote_dir = $session.ListDirectory($remote)
$remote_files = @()
foreach ($fileinfo in $remote_dir.Files)
{
if ($fileinfo.Name -ne '.' -and $fileinfo.Name -ne '..') {
$remote_files += $fileinfo
}
}
# Find all files that could be deleted and all that could be copied
$remote_files = ($remote_files | Sort-Object {-$_.LastWriteTime},{$_.Name})
$to_delete = @()
$to_copy = @()
$element_counter = 0
foreach($file in $remote_files)
{
$element_counter += 1
if (($now - $file.LastWriteTime).TotalHours -ge $min_age_copy_hours) {
$to_copy += $file
}
if ($element_counter -gt $min_count_keep) {
$to_delete += $file
}
}
# Create local directory if it does not exist
$ld = "$local\"
ensure_dir $ld
# Remove files from copy list that were already copied
$local_files = Get-ChildItem -File -Path $local
$tmp = $to_copy
$to_copy = @()
# Only add as many files as we are allowed to copy. Prefer newer files
# (this is ensured by the arrays' sorting).
$counter = 0
if (is_weekend $now) {
$max_count = $max_count_transfer_weekend
} else {
$max_count = $max_count_transfer_workday
}
foreach ($rf in $tmp)
{
$found = $False
foreach ($lf in $local_files) {
if ($lf.Name -eq $rf.Name -and $lf.LastWriteTime -ge $rf.LastWriteTime) {
$found = $True
break
}
}
if (!$found) {
if ($counter -lt $max_count) {
$to_copy += $rf
$counter += 1
} else {
break
}
}
}
# Copy files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::On
foreach ($file in $to_copy) {
$rf = "$remote/$($file.Name)"
log " Get ""$rf"" -> ""$ld"""
$transferResult = $session.GetFiles(
$rf,
$ld,
$False,
$transferOptions)
if (-not $transferResult.isSuccess) {
$ret_code = 1
foreach($err in $transferResult.Failures) {
log $err $True
}
}
}
# Remove files from delete list that must still be retained
# Those are all files that we don't have locally yet.
$local_files = Get-ChildItem -File -Path $local
$tmp = $to_delete
$to_delete = @()
foreach ($df in $tmp)
{
$found = $False
foreach ($lf in $local_files) {
if ($lf.Name -eq $df -and $lf.LastWriteTime -ge $df.LastWriteTime) {
$found = $True
break
}
}
if ($found) {
$to_delete += $df
}
}
# Delete files
foreach ($file in $to_delete) {
$rf = "$remote/$($file.Name)"
log " rm ""$rf"""
$removalResult = $session.RemoveFiles($rf)
if (-not $removalResult.isSuccess) {
$ret_code = 1
foreach($err in $removalResult.Failures) {
log $err $True
}
}
}
return $ret_code
}
# Program flow starts here
if ($test_emails) {
$email_enabled = $True
$email_message += "Test EMail from backup script at $(Get-Date)."
log_exit 0
}
if ($email_enabled) {
$email_message += "I began to backup files at $(Get-Date):`n"
$email_message += "From: $($hostname):$remote_source`n"
$email_message += "To: $local_target`n"
}
try
{
# Load WinSCP .NET assembly
Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $hostname
UserName = $username
Password = $password
SshHostKeyFingerprint = $hostkey
}
$session = New-Object WinSCP.Session
$exit_code = 0
try
{
# Connect
$session.Open($sessionOptions)
foreach ($d in $backup_directories)
{
if ((backup $session $d) -ne 0) {
$exit_code = 1
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
log_exit $exit_code
}
catch
{
log "Error: $($_.Exception.Message)"
log_exit 1
}