May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
Param(
|
||||
[parameter(Mandatory=$true)]
|
||||
[ValidateSet('CH','PL','SE','NL','DK','NO','DE')]
|
||||
[String] $envCountry
|
||||
)
|
||||
try{
|
||||
Set-DbatoolsInsecureConnection -SessionOnly
|
||||
. .\..\.env\dbadmin.ps1
|
||||
if($envCountry -eq 'CH'){
|
||||
Write-Output "Loading Switzerland Environment Settings"
|
||||
. .\..\.env\Import-envCH.ps1
|
||||
$country = 'Switzerland'
|
||||
}
|
||||
if($envCountry -eq 'SE'){
|
||||
Write-Output "Loading Swidish Environment Settings"
|
||||
. .\.env\Import-envSE.ps1
|
||||
$country = 'Sweden'
|
||||
}
|
||||
if($envCountry -eq 'PL'){
|
||||
Write-Output "Loading Swidish Environment Settings"
|
||||
. .\.env\Import-envPL.ps1
|
||||
$country = 'Poland'
|
||||
}
|
||||
if($envCountry -eq 'NO'){
|
||||
Write-Output "Loading Swidish Environment Settings"
|
||||
. .\.env\Import-envNO.ps1
|
||||
$country = 'Norway'
|
||||
}
|
||||
if($envCountry -eq 'DE'){
|
||||
Write-Output "Loading Germany Environment Settings"
|
||||
. .\.env\Import-envDE.ps1
|
||||
$country = 'Germany'
|
||||
}
|
||||
}
|
||||
catch{
|
||||
Write-Error $_
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
$envCountry = 'CH'
|
||||
|
||||
#identity to connect to environment
|
||||
$DBActiveDirectoryDomain= '%doimain name%'
|
||||
$DBADAccount='%username'
|
||||
$DBAsvcPassword = ConvertTo-SecureString -String '%password ' -AsPlainText -Force
|
||||
|
||||
$DBACredential = New-Object System.Management.Automation.PSCredential("$DBActiveDirectoryDomain\$DBADAccount", $DBAsvcPassword)
|
||||
$linPassword = ConvertTo-SecureString -String '1' -AsPlainText -Force
|
||||
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $linAccount, $linPassword
|
||||
|
||||
# Instances list
|
||||
$servers = @(
|
||||
# Green - test environement
|
||||
@("tpiccard","10.146.11.71","tpiccard","green"),
|
||||
@("bisw12ponldb01","10.41.70.65","bisw12ponldb01","green"),
|
||||
@("CHREPRTDB1NPW01","10.213.64.25","CHREPRTDB1NPW01","blue"),
|
||||
#dev @("BISW19TSQLCLU2","10.41.70.238","BISW19TSQLCLU2","green"),
|
||||
#dev @("BISW19TSQLCLU1","10.213.64.130","BISW19TSQLCLU1","green"),
|
||||
#wait for install SQL@("CHSHAB0DB0PRW01","10.41.70.230","CHSHAB0DB0PRW01","green"),
|
||||
# @("BISW19TMast1","10.213.64.132","BISW19TMast1","green"),brak sql
|
||||
# @("BISW19TMIGR01","10.41.70.122","BISW19TMIGR01","green"),brak sql
|
||||
#decommisioned @("BISW19TTRADE01","10.213.2.200","BISW19TTRADE01","green"),
|
||||
@("CHLOOKUDB0PRW01","10.146.11.11","CHLOOKUDB0PRW01","green"),
|
||||
# Orange - production environement
|
||||
@("CHREPRTDB1PRW01","10.213.2.81","CHREPRTDB1PRW01","orange"),
|
||||
@("BISW12PSQLWC01","10.41.70.205","BISW12PSQLWC01","orange"),
|
||||
@("BISW12PMISDB01.bisnodech.local","10.41.70.32","BISW12PMISDB01","orange"),
|
||||
@("BISW19PNAVDB01.bisnodech.local","10.146.11.103","BISW19PNAVDB01,","orange")
|
||||
@("CHURSHAB.bisnodech.local","10.146.11.60","CHURSHAB","orange"),
|
||||
@("bisw12pbccdb01.bisnodech.local","10.41.70.38","bisw12pbccdb01","orange"),
|
||||
@("bisw12ponldb01.bisnodech.local","10.41.70.65","bisw12ponldb01","orange"),
|
||||
@("bisw12prpsdb01.bisnodech.local","10.146.11.158","bisw12prpsdb01","orange"),
|
||||
@("bisw12psqldb01.bisnodech.local","10.146.11.248","bisw12psqldb01","orange"),
|
||||
@("bisw12psqldb02.bisnodech.local","10.146.11.190","bisw12psqldb02","orange"),
|
||||
@("bisw12pxmldb01.bisnodech.local","10.146.11.194","bisw12pxmldb01","orange"),
|
||||
@("dnbw08plookup1.dnb.bisnodech.local ","10.146.11.11","bisw12pxmldb01","orange"),
|
||||
@("piccard.dnbswitzerland.ch","10.146.11.213","piccard","orange")
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
$ActiveDirectoryDomain= '%domain name%'
|
||||
$ADAccount='%username%'
|
||||
$svcPassword = ConvertTo-SecureString -String '%password to DBAmin instance%' -AsPlainText -Force #Muszę to zmienić ponieważ nie jest bezpiecznie trzymać te informacje w pliku płaskim
|
||||
|
||||
$ADCredential = New-Object System.Management.Automation.PSCredential("$ActiveDirectoryDomain\$ADAccount", $svcPassword)
|
||||
@@ -0,0 +1,5 @@
|
||||
//with rebuild system databases - users and databases are dropped so better to detach user databases before
|
||||
[installation folder]\setup.exe /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=BIS_SQL /SQLSYSADMINACCOUNTS=INFRABIS\administrator /SQLCOLLATION=Polish_CI_AS /SAPWD=StrongPassword
|
||||
|
||||
//without rebuilding system databases - users and user databases stay but it's advised to detach user databases to speed up process
|
||||
sqlservr -m -T4022 -T3659 -s"BIS_SQL" -q"Polish_CI_AS"
|
||||
@@ -0,0 +1,88 @@
|
||||
$envCountry = 'CH'
|
||||
#import Country settings
|
||||
. .\..\.env\Import-EnvironmentSettings.ps1 -envCountry $envCountry
|
||||
# This is minimal dane witch can be inserted to database
|
||||
$minDatedte = Get-Date "1777/01/01"
|
||||
|
||||
$svcPassword = ConvertTo-SecureString -String $ADPassword -AsPlainText -Force
|
||||
$ADCredential = New-Object System.Management.Automation.PSCredential("$ActiveDirectoryDomain\$ADAccount", $svcPassword)
|
||||
|
||||
Write-host 'Processing country ... ' $country
|
||||
|
||||
foreach($server in $servers){
|
||||
|
||||
$serverName = $server[0]
|
||||
|
||||
$serverEnv = $server[3]
|
||||
$instanceIp = $server[1]
|
||||
# jeśli jest na jednym IP wiele instancji należy zbudować odpowiedni connection string i dopiero potem można się podłączyć do bazy
|
||||
if($server[4]) {
|
||||
$instance = $server[1] + '\' + $server[4]
|
||||
} else {
|
||||
$instance = $server[1]
|
||||
}
|
||||
|
||||
$tmpVerInstance = test-DbaBuild -SqlInstance $instance -Latest -SqlCredential $ADCredential -update
|
||||
$System_name = (Get-WmiObject -comp $instanceIp -Credential $ADCredential -class Win32_OperatingSystem ).caption #Version # #caption
|
||||
$System_Version = (Get-WmiObject -comp $instanceIp -Credential $ADCredential -class Win32_OperatingSystem ).Version
|
||||
|
||||
# $IP_Address = $HostNames[$i]
|
||||
# $ServerName = 'Server ' + $serverName
|
||||
$Instance_Name = $tmpVerInstance.SqlInstance
|
||||
Write-host '=> Processing server ... ' $serverName
|
||||
$dba_info = Invoke-DbaQuery -SqlInstance $instanceIp -Query "EXEC sp_server_info" -SqlCredential $ADCredential
|
||||
$Database_Type = $dba_info[0].attribute_value
|
||||
|
||||
$Build = $tmpVerInstance.Build
|
||||
$Server_Name = (Get-WmiObject -comp $instanceIp -Credential $ADCredential -class Win32_OperatingSystem ).CSName
|
||||
$Build = $tmpVerInstance.Build
|
||||
$KBLevel = $tmpVerInstance.KBLevel
|
||||
$Compliant = $tmpVerInstance.Compliant
|
||||
$SPLevel = $tmpVerInstance.SPLevel
|
||||
$SupportedUntil = $tmpVerInstance.SupportedUntil
|
||||
|
||||
$instanceid = Invoke-DbaQuery -SqlInstance $dbadminServer -Database "DBAdmin" -Query "EXEC dbaInstance '$Instance_Name', '$country', '$System_name', '$System_Version', '$instanceIp', '$Database_Type', '$Build', '$KBLevel', '$Compliant', '$SPLevel', '$SupportedUntil', '$serverEnv', '$serverName'" -SqlCredential $DBACredential
|
||||
|
||||
$InsID = $instanceid['InstanceID']
|
||||
$DatabaseInfo = Get-DbaDatabase -SqlInstance $instance -SqlCredential $ADCredential
|
||||
|
||||
foreach($db in $DatabaseInfo){
|
||||
if ($db.LastFullBackup -lt $minDatedte) {
|
||||
$LastFullBackup = $null
|
||||
}
|
||||
else {
|
||||
$LastFullBackup = $db.LastFullBackup
|
||||
}
|
||||
if ($db.LastDiffBackup -lt $minDatedte ) {
|
||||
$LastDiffBackup = $null
|
||||
}
|
||||
else {
|
||||
$LastDiffBackup = $db.LastDiffBackup
|
||||
}
|
||||
if ($db.LastLogBackup -lt $minDatedte) {
|
||||
$LastLogBackup = $null
|
||||
}
|
||||
else {
|
||||
$LastLogBackup = $db.LastLogBackup
|
||||
}
|
||||
$sqlParams = @{
|
||||
_instanceID = $InsID
|
||||
_DatabaseName = $db.Name
|
||||
_DatabaseStatus = $db.Status
|
||||
_IsAccessible = $db.IsAccessible
|
||||
_RecoveryModel = $db.RecoveryModel
|
||||
_LogReuseWaitStatus = $db.LogReuseWaitStatus
|
||||
_Compatibility = $db.Compatibility
|
||||
_Collation = $db.Collation
|
||||
_DBOwner = $db.Owner
|
||||
_IsEncrypted = $db.Encrypted
|
||||
_LastFullBackup = $LastFullBackup
|
||||
_LastDiffBackup = $LastDiffBackup
|
||||
_LastLogBackup = $LastLogBackup
|
||||
}
|
||||
|
||||
Write-host '==> Processing database ... ' $db.Name
|
||||
Invoke-DbaQuery -SqlInstance $dbadminServer -Database "DBAdmin" -Query 'exec dbaDatabase @_instanceID, @_DatabaseName, @_DatabaseStatus, @_IsAccessible, @_RecoveryModel, @_LogReuseWaitStatus, @_Compatibility, @_Collation, @_DBOwner, @_IsEncrypted, @_LastFullBackup, @_LastDiffBackup, @_LastLogBackup ' -SqlParameter $sqlParams -SqlCredential $DBACredential
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
$envCountry = 'CH'
|
||||
#import Country settings
|
||||
. .\..\.env\Import-EnvironmentSettings.ps1 -envCountry $envCountry
|
||||
|
||||
|
||||
$svcPassword = ConvertTo-SecureString -String $ADPassword -AsPlainText -Force #Muszę to zmienić ponieważ nie jest bezpiecznie trzymać te informacje w pliku płaskim
|
||||
$ADCredential = New-Object System.Management.Automation.PSCredential("$ActiveDirectoryDomain\$ADAccount", $svcPassword)
|
||||
|
||||
function Get-Instance {
|
||||
|
||||
foreach ($instance in $servers) {
|
||||
if($instance[4]) {
|
||||
[array]$sqlinstances += $instance[1] + '\' + $instance[4]
|
||||
} else {
|
||||
[array]$sqlinstances += $instance[1]
|
||||
}
|
||||
}
|
||||
if ($null -ne $sqlinstances) {
|
||||
return [array]$sqlinstances
|
||||
}
|
||||
if ($sqlinstances.Length -eq 0) {
|
||||
Write-PSFMessage -Level Warning -Message "You must specify a list of servers"
|
||||
return
|
||||
}
|
||||
else {
|
||||
[array]$sqlinstances
|
||||
}
|
||||
|
||||
}
|
||||
@(Get-Instance).ForEach{
|
||||
$instance = $psitem
|
||||
try {
|
||||
$InstanceSMO = Connect-DbaInstance -SqlInstance $instance -SqlCredential $ADCredential -ErrorAction SilentlyContinue -ErrorVariable errorvar
|
||||
} catch {
|
||||
|
||||
$NotContactable += $instance
|
||||
}
|
||||
if ($NotContactable -contains $instance) {
|
||||
Context "Checking for failed enabled jobs on $instance" {
|
||||
It "Can't Connect to $instance" {
|
||||
$false | Should -BeTrue -Because 'The instance should be available to be connected to!'
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($NotContactable -notcontains $instance) {
|
||||
if ($null -eq $InstanceSMO.version) {
|
||||
$NotContactable += $instance
|
||||
} elseif (($InstanceSMO).Edition -like 'Express Edition*') { }
|
||||
else {
|
||||
BeforeAll {
|
||||
|
||||
}
|
||||
Describe "Failed Jobs" -Tags FailedJob {
|
||||
$maxdays = 100
|
||||
$startdate = (Get-Date).AddDays( - $maxdays)
|
||||
$excludecancelled = $true
|
||||
Context "Checking for failed enabled jobs since $startdate on $instance -> " {
|
||||
@(Get-DbaAgentJob -SqlInstance $instance -SqlCredential $ADCredential | Where-Object Category -eq "Database Maintenance" ).ForEach{
|
||||
|
||||
if ($_.LastRunOutcome -eq 'Unknown') {
|
||||
It -Skip "1. We chose to skip this as $psitem's last run outcome is unknown on $($_.SqlInstance)" {
|
||||
$_.LastRunOutcome | Should -Be 'Succeeded' -Because 'All Agent Jobs should have succeed this one is unknown - you need to investigate the failed jobs'
|
||||
}
|
||||
} elseif (($_.LastRunOutcome -eq 'Cancelled') -and ($excludecancelled -eq $true)) {
|
||||
It -Skip "2. We chose to skip this as $psitem's last run outcome is cancelled on $($_.SqlInstance)" {
|
||||
$_.LastRunOutcome | Should -Be 'Succeeded' -Because 'All Agent Jobs should have succeed this one is unknown - you need to investigate the failed jobs'
|
||||
}
|
||||
} elseif ($_.LastRunOutcome -eq 'Succeeded'){
|
||||
# $a = $_.LastRunOutcome
|
||||
It "$_.LastRunOutcome's last run outcome is $($_.LastRunOutcome) on $($_.SqlInstance)" {
|
||||
$a = 'Succeeded'
|
||||
$a | Should -Be 'Succeeded'
|
||||
}
|
||||
} else {
|
||||
It "$_.LastRunOutcome's last run outcome is $($_.LastRunOutcome) on $($_.SqlInstance)" {
|
||||
$a = 'Failed'
|
||||
$a | Should -Be 'Succeeded'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$NotContactable
|
||||
}
|
||||
+1245
File diff suppressed because it is too large
Load Diff
+2826
File diff suppressed because it is too large
Load Diff
+1245
File diff suppressed because it is too large
Load Diff
+1670
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1395
File diff suppressed because it is too large
Load Diff
+3542
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+1859
File diff suppressed because it is too large
Load Diff
+3682
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+3909
File diff suppressed because it is too large
Load Diff
+2009
File diff suppressed because it is too large
Load Diff
+3962
File diff suppressed because it is too large
Load Diff
+1991
File diff suppressed because it is too large
Load Diff
+4076
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+4131
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,119 @@
|
||||
USE msdb;
|
||||
GO
|
||||
|
||||
WITH BackupData AS (
|
||||
SELECT
|
||||
database_name,
|
||||
CAST(backup_start_date AS DATE) AS backup_date,
|
||||
DAY(backup_start_date) AS backup_day,
|
||||
CASE type
|
||||
WHEN 'D' THEN 'Full Backup'
|
||||
WHEN 'I' THEN 'Differential Backup'
|
||||
WHEN 'L' THEN 'Transaction Log Backup'
|
||||
END AS backup_type,
|
||||
DATEDIFF(MINUTE, backup_start_date, backup_finish_date) AS duration_minutes,
|
||||
cast((compressed_backup_size / 1024 / 1024/1024) as numeric(18,2)) AS size_gb
|
||||
FROM dbo.backupset
|
||||
WHERE
|
||||
backup_start_date >= '2025-06-01'
|
||||
AND backup_start_date < '2025-07-01'
|
||||
)
|
||||
SELECT
|
||||
database_name,
|
||||
backup_type,
|
||||
COUNT(CASE WHEN backup_day = 1 THEN 1 END) AS [Day 1],
|
||||
SUM(CASE WHEN backup_day = 1 THEN duration_minutes ELSE 0 END) AS [Duration 1],
|
||||
SUM(CASE WHEN backup_day = 1 THEN size_gb ELSE 0 END) AS [Size GB 1],
|
||||
COUNT(CASE WHEN backup_day = 2 THEN 1 END) AS [Day 2],
|
||||
SUM(CASE WHEN backup_day = 2 THEN duration_minutes ELSE 0 END) AS [Duration 2],
|
||||
SUM(CASE WHEN backup_day = 2 THEN size_gb ELSE 0 END) AS [Size GB 2],
|
||||
COUNT(CASE WHEN backup_day = 3 THEN 1 END) AS [Day 3],
|
||||
SUM(CASE WHEN backup_day = 3 THEN duration_minutes ELSE 0 END) AS [Duration 3],
|
||||
SUM(CASE WHEN backup_day = 3 THEN size_gb ELSE 0 END) AS [Size GB 3],
|
||||
COUNT(CASE WHEN backup_day = 4 THEN 1 END) AS [Day 4],
|
||||
SUM(CASE WHEN backup_day = 4 THEN duration_minutes ELSE 0 END) AS [Duration 4],
|
||||
SUM(CASE WHEN backup_day = 4 THEN size_gb ELSE 0 END) AS [Size GB 4],
|
||||
COUNT(CASE WHEN backup_day = 5 THEN 1 END) AS [Day 5],
|
||||
SUM(CASE WHEN backup_day = 5 THEN duration_minutes ELSE 0 END) AS [Duration 5],
|
||||
SUM(CASE WHEN backup_day = 5 THEN size_gb ELSE 0 END) AS [Size GB 5],
|
||||
COUNT(CASE WHEN backup_day = 6 THEN 1 END) AS [Day 6],
|
||||
SUM(CASE WHEN backup_day = 6 THEN duration_minutes ELSE 0 END) AS [Duration 6],
|
||||
SUM(CASE WHEN backup_day = 6 THEN size_gb ELSE 0 END) AS [Size GB 6],
|
||||
COUNT(CASE WHEN backup_day = 7 THEN 1 END) AS [Day 7],
|
||||
SUM(CASE WHEN backup_day = 7 THEN duration_minutes ELSE 0 END) AS [Duration 7],
|
||||
SUM(CASE WHEN backup_day = 7 THEN size_gb ELSE 0 END) AS [Size GB 7],
|
||||
COUNT(CASE WHEN backup_day = 8 THEN 1 END) AS [Day 8],
|
||||
SUM(CASE WHEN backup_day = 8 THEN duration_minutes ELSE 0 END) AS [Duration 8],
|
||||
SUM(CASE WHEN backup_day = 8 THEN size_gb ELSE 0 END) AS [Size GB 8],
|
||||
COUNT(CASE WHEN backup_day = 9 THEN 1 END) AS [Day 9],
|
||||
SUM(CASE WHEN backup_day = 9 THEN duration_minutes ELSE 0 END) AS [Duration 9],
|
||||
SUM(CASE WHEN backup_day = 9 THEN size_gb ELSE 0 END) AS [Size GB 9],
|
||||
COUNT(CASE WHEN backup_day = 10 THEN 1 END) AS [Day 10],
|
||||
SUM(CASE WHEN backup_day = 10 THEN duration_minutes ELSE 0 END) AS [Duration 10],
|
||||
SUM(CASE WHEN backup_day = 10 THEN size_gb ELSE 0 END) AS [Size GB 10],
|
||||
COUNT(CASE WHEN backup_day = 11 THEN 1 END) AS [Day 11],
|
||||
SUM(CASE WHEN backup_day = 11 THEN duration_minutes ELSE 0 END) AS [Duration 11],
|
||||
SUM(CASE WHEN backup_day = 11 THEN size_gb ELSE 0 END) AS [Size GB 11],
|
||||
COUNT(CASE WHEN backup_day = 12 THEN 1 END) AS [Day 12],
|
||||
SUM(CASE WHEN backup_day = 12 THEN duration_minutes ELSE 0 END) AS [Duration 12],
|
||||
SUM(CASE WHEN backup_day = 12 THEN size_gb ELSE 0 END) AS [Size GB 12],
|
||||
COUNT(CASE WHEN backup_day = 13 THEN 1 END) AS [Day 13],
|
||||
SUM(CASE WHEN backup_day = 13 THEN duration_minutes ELSE 0 END) AS [Duration 13],
|
||||
SUM(CASE WHEN backup_day = 13 THEN size_gb ELSE 0 END) AS [Size GB 13],
|
||||
COUNT(CASE WHEN backup_day = 14 THEN 1 END) AS [Day 14],
|
||||
SUM(CASE WHEN backup_day = 14 THEN duration_minutes ELSE 0 END) AS [Duration 14],
|
||||
SUM(CASE WHEN backup_day = 14 THEN size_gb ELSE 0 END) AS [Size GB 14],
|
||||
COUNT(CASE WHEN backup_day = 15 THEN 1 END) AS [Day 15],
|
||||
SUM(CASE WHEN backup_day = 15 THEN duration_minutes ELSE 0 END) AS [Duration 15],
|
||||
SUM(CASE WHEN backup_day = 15 THEN size_gb ELSE 0 END) AS [Size GB 15],
|
||||
COUNT(CASE WHEN backup_day = 16 THEN 1 END) AS [Day 16],
|
||||
SUM(CASE WHEN backup_day = 16 THEN duration_minutes ELSE 0 END) AS [Duration 16],
|
||||
SUM(CASE WHEN backup_day = 16 THEN size_gb ELSE 0 END) AS [Size GB 16],
|
||||
COUNT(CASE WHEN backup_day = 17 THEN 1 END) AS [Day 17],
|
||||
SUM(CASE WHEN backup_day = 17 THEN duration_minutes ELSE 0 END) AS [Duration 17],
|
||||
SUM(CASE WHEN backup_day = 17 THEN size_gb ELSE 0 END) AS [Size GB 17],
|
||||
COUNT(CASE WHEN backup_day = 18 THEN 1 END) AS [Day 18],
|
||||
SUM(CASE WHEN backup_day = 18 THEN duration_minutes ELSE 0 END) AS [Duration 18],
|
||||
SUM(CASE WHEN backup_day = 18 THEN size_gb ELSE 0 END) AS [Size GB 18],
|
||||
COUNT(CASE WHEN backup_day = 19 THEN 1 END) AS [Day 19],
|
||||
SUM(CASE WHEN backup_day = 19 THEN duration_minutes ELSE 0 END) AS [Duration 19],
|
||||
SUM(CASE WHEN backup_day = 19 THEN size_gb ELSE 0 END) AS [Size GB 19],
|
||||
COUNT(CASE WHEN backup_day = 20 THEN 1 END) AS [Day 20],
|
||||
SUM(CASE WHEN backup_day = 20 THEN duration_minutes ELSE 0 END) AS [Duration 20],
|
||||
SUM(CASE WHEN backup_day = 20 THEN size_gb ELSE 0 END) AS [Size GB 20],
|
||||
COUNT(CASE WHEN backup_day = 21 THEN 1 END) AS [Day 21],
|
||||
SUM(CASE WHEN backup_day = 21 THEN duration_minutes ELSE 0 END) AS [Duration 21],
|
||||
SUM(CASE WHEN backup_day = 21 THEN size_gb ELSE 0 END) AS [Size GB 21],
|
||||
COUNT(CASE WHEN backup_day = 22 THEN 1 END) AS [Day 22],
|
||||
SUM(CASE WHEN backup_day = 22 THEN duration_minutes ELSE 0 END) AS [Duration 22],
|
||||
SUM(CASE WHEN backup_day = 22 THEN size_gb ELSE 0 END) AS [Size GB 22],
|
||||
COUNT(CASE WHEN backup_day = 23 THEN 1 END) AS [Day 23],
|
||||
SUM(CASE WHEN backup_day = 23 THEN duration_minutes ELSE 0 END) AS [Duration 23],
|
||||
SUM(CASE WHEN backup_day = 23 THEN size_gb ELSE 0 END) AS [Size GB 23],
|
||||
COUNT(CASE WHEN backup_day = 24 THEN 1 END) AS [Day 24],
|
||||
SUM(CASE WHEN backup_day = 24 THEN duration_minutes ELSE 0 END) AS [Duration 24],
|
||||
SUM(CASE WHEN backup_day = 24 THEN size_gb ELSE 0 END) AS [Size GB 24],
|
||||
COUNT(CASE WHEN backup_day = 25 THEN 1 END) AS [Day 25],
|
||||
SUM(CASE WHEN backup_day = 25 THEN duration_minutes ELSE 0 END) AS [Duration 25],
|
||||
SUM(CASE WHEN backup_day = 25 THEN size_gb ELSE 0 END) AS [Size GB 25],
|
||||
COUNT(CASE WHEN backup_day = 26 THEN 1 END) AS [Day 26],
|
||||
SUM(CASE WHEN backup_day = 26 THEN duration_minutes ELSE 0 END) AS [Duration 26],
|
||||
SUM(CASE WHEN backup_day = 26 THEN size_gb ELSE 0 END) AS [Size GB 26],
|
||||
COUNT(CASE WHEN backup_day = 27 THEN 1 END) AS [Day 27],
|
||||
SUM(CASE WHEN backup_day = 27 THEN duration_minutes ELSE 0 END) AS [Duration 27],
|
||||
SUM(CASE WHEN backup_day = 27 THEN size_gb ELSE 0 END) AS [Size GB 27],
|
||||
COUNT(CASE WHEN backup_day = 28 THEN 1 END) AS [Day 28],
|
||||
SUM(CASE WHEN backup_day = 28 THEN duration_minutes ELSE 0 END) AS [Duration 28],
|
||||
SUM(CASE WHEN backup_day = 28 THEN size_gb ELSE 0 END) AS [Size GB 28],
|
||||
COUNT(CASE WHEN backup_day = 29 THEN 1 END) AS [Day 29],
|
||||
SUM(CASE WHEN backup_day = 29 THEN duration_minutes ELSE 0 END) AS [Duration 29],
|
||||
SUM(CASE WHEN backup_day = 29 THEN size_gb ELSE 0 END) AS [Size GB 29],
|
||||
COUNT(CASE WHEN backup_day = 30 THEN 1 END) AS [Day 30],
|
||||
SUM(CASE WHEN backup_day = 30 THEN duration_minutes ELSE 0 END) AS [Duration 30],
|
||||
SUM(CASE WHEN backup_day = 30 THEN size_gb ELSE 0 END) AS [Size GB 30],
|
||||
COUNT(CASE WHEN backup_day = 31 THEN 1 END) AS [Day 31],
|
||||
SUM(CASE WHEN backup_day = 31 THEN duration_minutes ELSE 0 END) AS [Duration 31],
|
||||
SUM(CASE WHEN backup_day = 31 THEN size_gb ELSE 0 END) AS [Size GB 31]
|
||||
FROM BackupData
|
||||
GROUP BY database_name, backup_type
|
||||
ORDER BY database_name, backup_type;
|
||||
@@ -0,0 +1 @@
|
||||
W tym miejscu będziw wszystko co może dotyczyć przestrzeni temporarnej
|
||||
@@ -0,0 +1,28 @@
|
||||
--Tempdb session File usage
|
||||
--sys.dm_db_session_space_usage : Returns the number of pages allocated and deallocated by each session for the database.
|
||||
--sys.dm_exec_sessions: Gives details about the sessions.
|
||||
|
||||
SELECT
|
||||
sys.dm_exec_sessions.session_id AS [SESSION ID],
|
||||
db_name(sys.dm_db_session_space_usage.database_id) AS [DATABASE Name],
|
||||
HOST_NAME AS [System Name],
|
||||
program_name AS [Program Name],
|
||||
login_name AS [USER Name],
|
||||
status,
|
||||
cpu_time AS [CPU TIME (in milisec)],
|
||||
total_scheduled_time AS [Total Scheduled TIME (in milisec)],
|
||||
total_elapsed_time AS [Elapsed TIME (in milisec)],
|
||||
(memory_usage * 8) AS [Memory USAGE (in KB)],
|
||||
(user_objects_alloc_page_count * 8) AS [SPACE Allocated FOR USER Objects (in KB)],
|
||||
(user_objects_dealloc_page_count * 8) AS [SPACE Deallocated FOR USER Objects (in KB)],
|
||||
(internal_objects_alloc_page_count * 8) AS [SPACE Allocated FOR Internal Objects (in KB)],
|
||||
(internal_objects_dealloc_page_count * 8) AS [SPACE Deallocated FOR Internal Objects (in KB)],
|
||||
CASE is_user_process
|
||||
WHEN 1 THEN 'user session'
|
||||
WHEN 0 THEN 'system session'
|
||||
END AS [SESSION Type], row_count AS [ROW COUNT]
|
||||
FROM sys.dm_db_session_space_usage
|
||||
INNER join
|
||||
sys.dm_exec_sessions
|
||||
ON sys.dm_db_session_space_usage.session_id = sys.dm_exec_sessions.session_id
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
--A long running transaction may prevent cleanup of transaction log thus eating up all log space available resulting space crisis for all other applications.
|
||||
|
||||
SELECT
|
||||
transaction_id AS [Transacton ID],
|
||||
[name] AS [TRANSACTION Name],
|
||||
transaction_begin_time AS [TRANSACTION BEGIN TIME],
|
||||
DATEDIFF(mi, transaction_begin_time, GETDATE()) AS [Elapsed TIME (in MIN)],
|
||||
CASE transaction_type
|
||||
WHEN 1 THEN 'Read/write'
|
||||
WHEN 2 THEN 'Read-only'
|
||||
WHEN 3 THEN 'System'
|
||||
WHEN 4 THEN 'Distributed'
|
||||
END AS [TRANSACTION Type],
|
||||
CASE transaction_state
|
||||
WHEN 0 THEN 'The transaction has not been completely initialized yet.'
|
||||
WHEN 1 THEN 'The transaction has been initialized but has not started.'
|
||||
WHEN 2 THEN 'The transaction is active.'
|
||||
WHEN 3 THEN 'The transaction has ended. This is used for read-only transactions.'
|
||||
WHEN 4 THEN 'The commit process has been initiated on the distributed transaction. This is for distributed transactions only. The distributed transaction is still active but further processing cannot take place.'
|
||||
WHEN 5 THEN 'The transaction is in a prepared state and waiting resolution.'
|
||||
WHEN 6 THEN 'The transaction has been committed.'
|
||||
WHEN 7 THEN 'The transaction is being rolled back.'
|
||||
WHEN 8 THEN 'The transaction has been rolled back.'
|
||||
END AS [TRANSACTION Description]
|
||||
FROM sys.dm_tran_active_transactions
|
||||
@@ -0,0 +1,30 @@
|
||||
--Long running Queries
|
||||
|
||||
-- sys.dm_exec_requests : Returns information regarding the requests made to the database server.
|
||||
|
||||
SELECT
|
||||
HOST_NAME AS [System Name],
|
||||
program_name AS [Application Name],
|
||||
DB_NAME(sys.dm_exec_sessions.database_id) AS [DATABASE Name],
|
||||
USER_NAME(USER_ID) AS [USER Name],
|
||||
connection_id AS [CONNECTION ID],
|
||||
sys.dm_exec_requests.session_id AS [CURRENT SESSION ID],
|
||||
blocking_session_id AS [Blocking SESSION ID],
|
||||
start_time AS [Request START TIME],
|
||||
sys.dm_exec_requests.status AS [Status],
|
||||
command AS [Command Type],
|
||||
(SELECT TEXT FROM sys.dm_exec_sql_text(sql_handle)) AS [Query TEXT],
|
||||
wait_type AS [Waiting Type],
|
||||
wait_time AS [Waiting Duration],
|
||||
wait_resource AS [Waiting FOR Resource],
|
||||
sys.dm_exec_requests.transaction_id AS [TRANSACTION ID],
|
||||
percent_complete AS [PERCENT Completed],
|
||||
estimated_completion_time AS [Estimated COMPLETION TIME (in mili sec)],
|
||||
sys.dm_exec_requests.cpu_time AS [CPU TIME used (in mili sec)],
|
||||
(memory_usage * 8) AS [Memory USAGE (in KB)],
|
||||
sys.dm_exec_requests.total_elapsed_time AS [Elapsed TIME (in mili sec)]
|
||||
FROM sys.dm_exec_requests
|
||||
INNER join
|
||||
sys.dm_exec_sessions
|
||||
ON sys.dm_exec_requests.session_id = sys.dm_exec_sessions.session_id
|
||||
WHERE DB_NAME(sys.dm_exec_sessions.database_id) = 'tempdb'
|
||||
@@ -0,0 +1,38 @@
|
||||
-- Determining the Amount of Free Space in tempdb
|
||||
SELECT SUM(unallocated_extent_page_count) AS [free pages],
|
||||
(SUM(unallocated_extent_page_count)*1.0/128) AS [free space in MB]
|
||||
FROM sys.dm_db_file_space_usage;
|
||||
|
||||
-- Determining the Amount Space Used by the Version Store
|
||||
SELECT SUM(version_store_reserved_page_count) AS [version store pages used],
|
||||
(SUM(version_store_reserved_page_count)*1.0/128) AS [version store space in MB]
|
||||
FROM sys.dm_db_file_space_usage;
|
||||
|
||||
-- Determining the Amount of Space Used by Internal Objects
|
||||
SELECT SUM(internal_object_reserved_page_count) AS [internal object pages used],
|
||||
(SUM(internal_object_reserved_page_count)*1.0/128) AS [internal object space in MB]
|
||||
FROM sys.dm_db_file_space_usage;
|
||||
|
||||
-- Determining the Amount of Space Used by User Objects
|
||||
SELECT SUM(user_object_reserved_page_count) AS [user object pages used],
|
||||
(SUM(user_object_reserved_page_count)*1.0/128) AS [user object space in MB]
|
||||
FROM sys.dm_db_file_space_usage;
|
||||
|
||||
-- Obtaining the space consumed by internal objects in all currently running tasks in each session
|
||||
SELECT session_id,
|
||||
SUM(internal_objects_alloc_page_count) AS task_internal_objects_alloc_page_count,
|
||||
SUM(internal_objects_dealloc_page_count) AS task_internal_objects_dealloc_page_count
|
||||
FROM sys.dm_db_task_space_usage
|
||||
GROUP BY session_id;
|
||||
|
||||
|
||||
-- Obtaining the space consumed by internal objects in the current session for both running and completed tasks
|
||||
SELECT R2.session_id,
|
||||
R1.internal_objects_alloc_page_count
|
||||
+ SUM(R2.internal_objects_alloc_page_count) AS session_internal_objects_alloc_page_count,
|
||||
R1.internal_objects_dealloc_page_count
|
||||
+ SUM(R2.internal_objects_dealloc_page_count) AS session_internal_objects_dealloc_page_count
|
||||
FROM sys.dm_db_session_space_usage AS R1
|
||||
INNER JOIN sys.dm_db_task_space_usage AS R2 ON R1.session_id = R2.session_id
|
||||
GROUP BY R2.session_id, R1.internal_objects_alloc_page_count,
|
||||
R1.internal_objects_dealloc_page_count;;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- To check current tempdb size and growth parameters, use the following query:
|
||||
SELECT name AS FileName,
|
||||
size*1.0/128 AS FileSizeinMB,
|
||||
CASE max_size
|
||||
WHEN 0 THEN 'Autogrowth is off.'
|
||||
WHEN -1 THEN 'Autogrowth is on.'
|
||||
ELSE 'Log file grows to a maximum size of 2 TB.'
|
||||
END,
|
||||
growth AS 'GrowthValue',
|
||||
'GrowthIncrement' =
|
||||
CASE
|
||||
WHEN growth = 0 THEN 'Size is fixed.'
|
||||
WHEN growth > 0 AND is_percent_growth = 0
|
||||
THEN 'Growth value is in 8-KB pages.'
|
||||
ELSE 'Growth value is a percentage.'
|
||||
END
|
||||
FROM tempdb.sys.database_files;
|
||||
GO
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#--------------------------------------------
|
||||
# Permissions for MySQL Database Assessments
|
||||
#--------------------------------------------
|
||||
|
||||
# 158.151.178.88 - This is the Imperva Production appliance that performs vulnerability and compliance scans
|
||||
|
||||
# Create imperva user to perform vulnerability and complaince scans
|
||||
|
||||
CREATE USER IF NOT EXISTS 'z-impvuser'@'158.151.178.88' IDENTIFIED BY 'KAl7FCzMNgIoPwAI';
|
||||
|
||||
# The following object permissions are required:
|
||||
|
||||
# The reason granting a select privilege on all tables in all databases is the behavior of the INFORMATION_SCHEMA.
|
||||
# INFORMATION_SCHEMA provides access to database metadata. Each MySQL user has the right to access these tables,
|
||||
# but can see only the rows in the tables that correspond to objects for which the user has the proper access privileges.
|
||||
|
||||
# For MySQL version 8, the following object permissions are required:
|
||||
|
||||
GRANT PROCESS, SELECT, SHOW DATABASES, SHOW VIEW ON *.* TO 'z-impvuser'@'158.151.178.88';
|
||||
|
||||
# Note: There is only one test that requires use of this permissions. It checks usage of encryption for
|
||||
# tables and tablespaces as any sensitive data must be encrypted. Otherwise it is subject to
|
||||
# compromise and unauthorized disclosure.
|
||||
|
||||
FLUSH PRIVILEGES;
|
||||
|
||||
|
||||
|
||||
//inform Imperva
|
||||
email to AwadhwalK@DNB.com
|
||||
@@ -0,0 +1,114 @@
|
||||
DELIMITER $$
|
||||
|
||||
CREATE PROCEDURE archive_last_days_dynamic(
|
||||
IN p_source_table VARCHAR(64),
|
||||
IN p_archive_table VARCHAR(64),
|
||||
IN p_date_column VARCHAR(64),
|
||||
IN p_days_back INT,
|
||||
IN p_batch_size INT
|
||||
)
|
||||
BEGIN
|
||||
DECLARE done INT DEFAULT FALSE;
|
||||
DECLARE rows_affected INT DEFAULT 0;
|
||||
DECLARE total_rows INT DEFAULT 0;
|
||||
|
||||
DECLARE insert_sql VARCHAR(4000);
|
||||
DECLARE delete_sql VARCHAR(4000);
|
||||
|
||||
-- Validation
|
||||
IF p_source_table IS NULL OR p_source_table = '' THEN
|
||||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Source table name is required';
|
||||
END IF;
|
||||
|
||||
IF p_archive_table IS NULL OR p_archive_table = '' THEN
|
||||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Archive table name is required';
|
||||
END IF;
|
||||
|
||||
IF p_date_column IS NULL OR p_date_column = '' THEN
|
||||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Date column name is required';
|
||||
END IF;
|
||||
|
||||
IF p_days_back IS NULL OR p_days_back <= 0 THEN
|
||||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Days back must be greater than 0';
|
||||
END IF;
|
||||
|
||||
-- If batch size is NULL or <= 0, run once without LIMIT
|
||||
IF p_batch_size IS NULL OR p_batch_size <= 0 THEN
|
||||
|
||||
SET insert_sql = CONCAT(
|
||||
'INSERT INTO `', p_archive_table, '` ',
|
||||
'SELECT * FROM `', p_source_table, '` ',
|
||||
'WHERE `', p_date_column, '` <= NOW() - INTERVAL ', p_days_back, ' DAY'
|
||||
);
|
||||
|
||||
SET delete_sql = CONCAT(
|
||||
'DELETE FROM `', p_source_table, '` ',
|
||||
'WHERE `', p_date_column, '` <= NOW() - INTERVAL ', p_days_back, ' DAY'
|
||||
);
|
||||
|
||||
START TRANSACTION;
|
||||
|
||||
SET @insert_sql = insert_sql;
|
||||
PREPARE stmt_insert FROM @insert_sql;
|
||||
EXECUTE stmt_insert;
|
||||
SET rows_affected = ROW_COUNT();
|
||||
SET total_rows = rows_affected;
|
||||
DEALLOCATE PREPARE stmt_insert;
|
||||
|
||||
SET @delete_sql = delete_sql;
|
||||
PREPARE stmt_delete FROM @delete_sql;
|
||||
EXECUTE stmt_delete;
|
||||
DEALLOCATE PREPARE stmt_delete;
|
||||
|
||||
COMMIT;
|
||||
|
||||
ELSE
|
||||
REPEAT
|
||||
SET insert_sql = CONCAT(
|
||||
'INSERT INTO `', p_archive_table, '` ',
|
||||
'SELECT * FROM `', p_source_table, '` ',
|
||||
'WHERE `', p_date_column, '` <= NOW() - INTERVAL ', p_days_back, ' DAY ',
|
||||
'LIMIT ', p_batch_size
|
||||
);
|
||||
|
||||
SET delete_sql = CONCAT(
|
||||
'DELETE FROM `', p_source_table, '` ',
|
||||
'WHERE `', p_date_column, '` <= NOW() - INTERVAL ', p_days_back, ' DAY ',
|
||||
'LIMIT ', p_batch_size
|
||||
);
|
||||
|
||||
START TRANSACTION;
|
||||
|
||||
SET @insert_sql = insert_sql;
|
||||
PREPARE stmt_insert FROM @insert_sql;
|
||||
EXECUTE stmt_insert;
|
||||
SET rows_affected = ROW_COUNT();
|
||||
SET total_rows = total_rows + rows_affected;
|
||||
DEALLOCATE PREPARE stmt_insert;
|
||||
|
||||
IF rows_affected > 0 THEN
|
||||
SET @delete_sql = delete_sql;
|
||||
PREPARE stmt_delete FROM @delete_sql;
|
||||
EXECUTE stmt_delete;
|
||||
DEALLOCATE PREPARE stmt_delete;
|
||||
END IF;
|
||||
|
||||
COMMIT;
|
||||
|
||||
IF rows_affected < p_batch_size THEN
|
||||
SET done = TRUE;
|
||||
END IF;
|
||||
|
||||
UNTIL done END REPEAT;
|
||||
|
||||
END IF;
|
||||
|
||||
SELECT 'ok' AS status, total_rows AS total_rows_moved;
|
||||
|
||||
END $$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
|
||||
//USE
|
||||
CALL archive_last_days_dynamic('source_table', 'archive_table', 'created_at', 30, 500);
|
||||
Reference in New Issue
Block a user