Repository navigation
Expand file tree
/
Copy pathBackupRestoreManager.ps1
More file actions
155 lines (136 loc) · 5.91 KB
/
Copy pathBackupRestoreManager.ps1
File metadata and controls
155 lines (136 loc) · 5.91 KB
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
# Backup and Restore Management Script
# This script provides automated backup and restore functionality for Windows Server
# Import required modules
Import-Module ActiveDirectory
Import-Module ServerManager
# Configuration
$backupConfig = @{
BackupPath = "D:\Backups"
RetentionDays = 30
LogPath = "C:\Logs\Backup"
BackupTypes = @("SystemState", "AD", "Files")
}
# Create necessary directories
New-Item -Path $backupConfig.BackupPath -ItemType Directory -Force -ErrorAction SilentlyContinue
New-Item -Path $backupConfig.LogPath -ItemType Directory -Force -ErrorAction SilentlyContinue
# Function to create backup
function Start-Backup {
param (
[string]$BackupType
)
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm"
$logFile = Join-Path $backupConfig.LogPath "backup_${BackupType}_${timestamp}.log"
try {
switch ($BackupType) {
"SystemState" {
Write-Host "Starting System State backup..." -ForegroundColor Yellow
$backupPath = Join-Path $backupConfig.BackupPath "SystemState_$timestamp"
wbadmin start systemstatebackup -backupTarget:$backupPath -quiet
}
"AD" {
Write-Host "Starting Active Directory backup..." -ForegroundColor Yellow
$backupPath = Join-Path $backupConfig.BackupPath "AD_$timestamp"
ntdsutil "activate instance ntds" "ifm" "create full $backupPath" "quit" "quit"
}
"Files" {
Write-Host "Starting File System backup..." -ForegroundColor Yellow
$backupPath = Join-Path $backupConfig.BackupPath "Files_$timestamp"
$sourcePaths = @(
"C:\Shares",
"C:\ImportantData"
)
foreach ($source in $sourcePaths) {
if (Test-Path $source) {
$destination = Join-Path $backupPath (Split-Path $source -Leaf)
Copy-Item -Path $source -Destination $destination -Recurse -Force
}
}
}
}
"Backup completed successfully at $(Get-Date)" | Out-File -FilePath $logFile -Append
Write-Host "Backup completed successfully!" -ForegroundColor Green
}
catch {
$errorMessage = "Backup failed: $_"
$errorMessage | Out-File -FilePath $logFile -Append
Write-Host $errorMessage -ForegroundColor Red
}
}
# Function to restore from backup
function Start-Restore {
param (
[string]$BackupType,
[string]$BackupDate
)
$logFile = Join-Path $backupConfig.LogPath "restore_${BackupType}_$(Get-Date -Format 'yyyy-MM-dd_HH-mm').log"
try {
switch ($BackupType) {
"SystemState" {
Write-Host "Starting System State restore..." -ForegroundColor Yellow
$backupPath = Get-ChildItem -Path $backupConfig.BackupPath -Filter "SystemState_$BackupDate*" | Select-Object -First 1
if ($backupPath) {
wbadmin start systemstaterecovery -backupTarget:$backupPath.FullName -quiet
}
}
"AD" {
Write-Host "Starting Active Directory restore..." -ForegroundColor Yellow
$backupPath = Get-ChildItem -Path $backupConfig.BackupPath -Filter "AD_$BackupDate*" | Select-Object -First 1
if ($backupPath) {
ntdsutil "activate instance ntds" "ifm" "restore full $($backupPath.FullName)" "quit" "quit"
}
}
"Files" {
Write-Host "Starting File System restore..." -ForegroundColor Yellow
$backupPath = Get-ChildItem -Path $backupConfig.BackupPath -Filter "Files_$BackupDate*" | Select-Object -First 1
if ($backupPath) {
$sourcePaths = @(
"C:\Shares",
"C:\ImportantData"
)
foreach ($source in $sourcePaths) {
$backupSource = Join-Path $backupPath.FullName (Split-Path $source -Leaf)
if (Test-Path $backupSource) {
Copy-Item -Path $backupSource -Destination $source -Recurse -Force
}
}
}
}
}
"Restore completed successfully at $(Get-Date)" | Out-File -FilePath $logFile -Append
Write-Host "Restore completed successfully!" -ForegroundColor Green
}
catch {
$errorMessage = "Restore failed: $_"
$errorMessage | Out-File -FilePath $logFile -Append
Write-Host $errorMessage -ForegroundColor Red
}
}
# Function to clean up old backups
function Remove-OldBackups {
$cutoffDate = (Get-Date).AddDays(-$backupConfig.RetentionDays)
Get-ChildItem -Path $backupConfig.BackupPath -Recurse | Where-Object {
$_.LastWriteTime -lt $cutoffDate
} | Remove-Item -Recurse -Force
Write-Host "Cleaned up backups older than $($backupConfig.RetentionDays) days" -ForegroundColor Yellow
}
# Function to list available backups
function Get-BackupList {
$backups = Get-ChildItem -Path $backupConfig.BackupPath -Recurse | Group-Object {
if ($_.Name -match "SystemState_") { "SystemState" }
elseif ($_.Name -match "AD_") { "AD" }
elseif ($_.Name -match "Files_") { "Files" }
}
foreach ($group in $backups) {
Write-Host "`n$($group.Name) Backups:" -ForegroundColor Cyan
$group.Group | Sort-Object LastWriteTime -Descending | ForEach-Object {
Write-Host " $($_.Name) - $($_.LastWriteTime)"
}
}
}
# Example usage:
# Start-Backup -BackupType "SystemState"
# Start-Backup -BackupType "AD"
# Start-Backup -BackupType "Files"
# Start-Restore -BackupType "SystemState" -BackupDate "2024-03-20"
# Remove-OldBackups
# Get-BackupList