first commit
This commit is contained in:
83
backup.ps1
Normal file
83
backup.ps1
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
$sqlInstance = "sqlfcsql\TESTINST"
|
||||||
|
#$directory = "H:\Backup"
|
||||||
|
$directory = "C:\Rubrik\mount"
|
||||||
|
$fullBackupDay = 'Wednesday'
|
||||||
|
$cleanupTime = 24
|
||||||
|
$checkCluster = $false
|
||||||
|
|
||||||
|
$fullFlag = $directory + "\last_full.flag"
|
||||||
|
$diffFlag = $directory + "\last_diff.flag"
|
||||||
|
$today = (Get-Date).Date
|
||||||
|
$logFile = "C:\Rubrik\backup.log"
|
||||||
|
|
||||||
|
function FlagTakenToday($flagPath) {
|
||||||
|
if (Test-Path $flagPath) {
|
||||||
|
$flagDate = (Get-Content $flagPath | Out-String).Trim()
|
||||||
|
return ($flagDate -eq $today.ToString("yyyy-MM-dd"))
|
||||||
|
}
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
|
||||||
|
function Write-Log($message) {
|
||||||
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||||
|
$logEntry = "$timestamp $message"
|
||||||
|
Add-Content -Path $logFile -Value $logEntry
|
||||||
|
Write-Host $logEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($checkCluster) {
|
||||||
|
# Check if SQL instance is running locally
|
||||||
|
$localNode = $env:COMPUTERNAME
|
||||||
|
$instanceName = $sqlInstance.Split('\')[1]
|
||||||
|
$clusterInstance = Get-ClusterResource | Where-Object { $_.ResourceType -eq "SQL Server" -and $_.Name -eq "SQL Server ($instanceName)" }
|
||||||
|
if ($clusterInstance) {
|
||||||
|
$ownerNode = $clusterInstance.OwnerNode.Name
|
||||||
|
if ($ownerNode -ne $localNode) {
|
||||||
|
Write-Log "SQL instance '$sqlInstance' is not running on local node '$localNode'. Exiting script."
|
||||||
|
exit 1
|
||||||
|
} else {
|
||||||
|
Write-Log "SQL instance '$sqlInstance' is running on local node '$localNode'. Proceeding."
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Write-Log "ERROR: SQL instance '$sqlInstance' not found in cluster resources."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Write-Log "INFO: Cluster check is disabled. Proceeding without verification."
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((Get-Date).DayOfWeek -eq $fullBackupDay) {
|
||||||
|
if (-not (FlagTakenToday $fullFlag)) {
|
||||||
|
$backupType = "FULL"
|
||||||
|
Set-Content $fullFlag $today.ToString("yyyy-MM-dd")
|
||||||
|
Write-Log "Selected FULL backup. Flag updated."
|
||||||
|
} else {
|
||||||
|
$backupType = "LOG"
|
||||||
|
Write-Log "FULL backup already taken today. Selected LOG backup."
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (-not (FlagTakenToday $diffFlag)) {
|
||||||
|
$backupType = "DIFF"
|
||||||
|
Set-Content $diffFlag $today.ToString("yyyy-MM-dd")
|
||||||
|
Write-Log "Selected DIFF backup. Flag updated."
|
||||||
|
} else {
|
||||||
|
$backupType = "LOG"
|
||||||
|
Write-Log "DIFF backup already taken today. Selected LOG backup."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = "EXECUTE [dbo].[DatabaseBackup] @Databases = 'ALL_DATABASES', @Directory = '$directory', @BackupType = '$backupType', @Verify = 'N', @CleanupTime = $cleanupTime, @CheckSum = 'Y', @LogToTable = 'Y'"
|
||||||
|
Write-Log "Executing backup type: $backupType"
|
||||||
|
|
||||||
|
$sqlcmdOutput = & sqlcmd -S $sqlInstance -Q $query 2>&1
|
||||||
|
$sqlcmdExitCode = $LASTEXITCODE
|
||||||
|
|
||||||
|
if ($sqlcmdExitCode -eq 0) {
|
||||||
|
foreach ($line in $sqlcmdOutput) {
|
||||||
|
Write-Log $line
|
||||||
|
}
|
||||||
|
Write-Log "$backupType Backup execution completed."
|
||||||
|
} else {
|
||||||
|
Write-Log "ERROR: Backup execution failed. Exit code: $sqlcmdExitCode. Output: $sqlcmdOutput"
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user