-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup-Application.ps1
138 lines (101 loc) · 5.32 KB
/
Backup-Application.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
################################################################################
#.SYNOPSIS
# Backs up an application directory and cleans up old copies
#
#.DESCRIPTION
# Backup an application directory by creating a zip archive appended with the
# current date and time and moving it to a specified backup folder.
# Checks the specified backup folder and only keeps the latest 2 copies older than 2 weeks.
# .PARAMETER sourceDirectoryPath
# The path of the directory that you want to backup
# .PARAMETER backupDirectoryPath
# The path of the backup folder
# .PARAMETER applicationName
# The name of the application that you want to back up. This correlates to the folder name of the application.
# .EXAMPLE (No spaces in directory names)
# PS D:\Temp\Backup> .\Backup-Application "D:\EntityCentralWwwRoot\EntityCentral" "D:\Temp\Backups" "EntityCentral"
# .EXAMPLE (Spaces in directory names)
# PS D:\Temp\Backup> .\Backup-Application "D:\Entity Central WwwRoot\Entity Central" "D:\Temp\Backup Directory" "Entity Central"
################################################################################
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[string] $sourceDirectoryPath,
[Parameter(Mandatory = $true)]
[string] $backupDirectoryPath,
[Parameter(Mandatory = $true)]
[string] $applicationName
)
# Strip out whitespaces for applicaiton name if any.
$applicationName = $applicationName.Replace(" ", "")
# Exit program when source directory doesn't exist
if (-Not (Test-Path -Path $sourceDirectoryPath)) {
Write-Output("Source directory {0} does not exist, exiting..." -f $sourceDirectoryPath)
exit
}
# Create backup directory when it doesn't exist
if (-Not (Test-Path -Path $backupDirectoryPath)) {
Write-Output("Backup directory {0} does not exist, creating..." -f $backupDirectoryPath)
New-Item -Path $backupDirectoryPath -ItemType directory | Out-Null
}
#### Debug ####
Write-Debug ("Source Directory Path = {0}" -f $sourceDirectoryPath)
Write-Debug ("Destination Directory Path = {0}" -f $backupDirectoryPath)
Write-Debug ("Application Name = {0}" -f $applicationName)
#### Debug ####
$currentDate = Get-Date -UFormat %Y%m%d-%H%M # e.g. 20190311-0939
$root = $backupDirectoryPath + "\"
$destinationFileName = $root + $applicationName + "-" + $currentDate + ".zip"
#### Debug ####
Write-Debug ("Current Date = {0}" -f $currentDate)
Write-Debug ("Destination Directory with trailing slash = {0}" -f $root)
Write-Debug ("Destination Filename = {0}" -f $destinationFileName)
#### Debug ####
Write-Verbose("Starting the backup process...")
If (Test-Path $destinationFilename) {
Write-Verbose ("Destination file '{0}' already exists, deleting..." -f $destinationFileName)
Remove-item $destinationFilename
}
Write-Output ("Beginning backup process...")
Write-Output ("Compressing files from '{0}' and moving to '{1}'..." -f $sourceDirectoryPath, $backupDirectoryPath)
Add-Type -AssemblyName "system.io.compression.filesystem"
Write-Verbose ("Creating zip file from '{0}' and saving it to '{1}'" -f $sourceDirectoryPath, $destinationFilename)
[io.compression.zipfile]::CreateFromDirectory($sourceDirectoryPath, $destinationFilename)
Write-Verbose ("Getting list of current backup files for '{0}' from '{1}' older than 2 weeks..." -f $applicationName, $backupDirectoryPath)
$cutoffDate = (Get-Date).AddDays(-14);
$backupFiles = Get-ChildItem $root | Where-Object { $_.Name -match $applicationName + "-" -and $_.LastWriteTime.Date -le $cutoffDate.Date } | Sort-Object -Property LastWriteTime
$count = ($backupFiles | Measure-Object).Count
Write-Verbose ("{0} backup file(s) exists in '{1}' older than 2 weeks:" -f $count, $backupDirectoryPath)
foreach ($file in $backupFiles) {
Write-Verbose ($file)
}
Write-Output("Cleaning up backup files older than 2 weeks from '{0}'..." -f $backupDirectoryPath)
If ($count -gt 2) {
Write-Output ("There are more than 2 backup files older than 2 weeks, cleaning up to only keep the latest 2...")
$currentCount = $count
$files = $backupFiles
while ($currentCount -gt 2) {
$firstIndex = 0
$nextIndex = 1
$modifiedDateOfFirstFile = [datetime]$files[$firstIndex].LastWriteTime
$modifiedDateOfNextFile = [datetime]$files[$nextIndex].LastWriteTime
Write-Verbose ("Checking to see if the modified date of '{0} ({1})' is equal to or before {2} ({3})" -f $files[$firstIndex], $modifiedDateOfFirstFile, $files[$nextIndex], $modifiedDateOfNextFile)
If ($modifiedDateOfFirstFile -le $modifiedDateOfNextFile) {
$filePath = $root + $files[$firstIndex]
Write-Verbose ("The modified date is equal to or before, deleting {0}" -f $files[$firstIndex])
Remove-Item $filePath
}
# Get the list of files again and update the current count
$files = Get-ChildItem $root | Where-Object { $_.Name -match $applicationName + "-" -and $_.LastWriteTime -le $cutoffDate } | Sort-Object -Property LastWriteTime
$currentCount = ($files | Measure-Object).Count
Write-Verbose ("There are currently {0} backup files older than 2 weeks:" -f $currentCount)
foreach ($file in $files) {
Write-Verbose ($file)
}
}
Write-Output ("Backup complete!")
}
Else {
Write-Output ("There are not more than 2 backup files older than 2 weeks!")
Write-Output ("Backup complete!")
}