Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaFMadeira committed Jan 7, 2024
2 parents a20605d + eb37f37 commit 4d3face
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Utility Scripts/Backup1.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$MyServer = "localhost"
$MyFolder = "C:\TEMP\Backup"
$DBs = @("Try001","Try002")
foreach ($DB in $DBs) {
Write-Host $DB
$date = Get-Date -format "yyyyMMdd_hhmmss"
Backup-SqlDatabase -ServerInstance $MyServer -Database $DB -BackupFile $MyFolder\$MyServer-$DB-Full-$date.bak
}
8 changes: 8 additions & 0 deletions Utility Scripts/Backup2.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$MyServer = "localhost"
$DBs = @(Get-SqlDatabase -Server $MyServer -Credential $sqlCredential | Where-Object {$_.Name -in @('Try002','Try001')})
foreach ($DB in $DBs) {
Write-Host $DB.Name
$dbName = $DB.Name
$date = Get-Date -format "yyyyMMdd_hhmmss"
Backup-SqlDatabase -ServerInstance localhost -Database $dbName -BackupFile C:\TEMP\Backup\$MyServer-$dbName-Full-$date.bak
}
9 changes: 9 additions & 0 deletions Utility Scripts/Backup3.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$MyServer = "localhost"
$MyFolder = "C:\TEMP\Backup"
$DBs = Get-SqlDatabase -ServerInstance $MyServer | Out-GridView -OutputMode Multiple
foreach ($DB in $DBs) {
$dbName = $DB.Name
Write-Host $dbName
$date = Get-Date -format "yyyyMMdd_hhmmss"
Backup-SqlDatabase -ServerInstance $MyServer -Database $dbName -BackupFile $MyFolder\$MyServer-$dbName-Full-$date.bak
}
28 changes: 28 additions & 0 deletions Utility Scripts/Restore1.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$BackupFolder="C:\temp\Backup\"
$NewDataPath="c:\Temp\Data\" #The Data files directory (for the new DB)
$NewLogPath="c:\Temp\Log\" #The Log file directory (for the new DB)
Get-ChildItem $BackupFolder -Filter *.bak |
Foreach-Object {
$File=$_.Name
Write-Host $File
$dbname = $File.replace('.bak','') #The new DB name
$relocate = @() #This variable (for the Restore-SqlDatabase) is initialized as a list (and not an empty string), for the "With Move" part of the Restore command
$OutputFile="C:\temp\$dbname.sql" #A script file will be created
$BackupFile=$BackupFolder+$File
$dbfiles = Invoke-Sqlcmd -ServerInstance localhost -Database tempdb -Query "Restore FileListOnly From Disk='$BackupFile';" #The Restore FileListOnly output is inserted into a variable
foreach($dbfile in $dbfiles){
$DbFileName = $dbfile.PhysicalName | Split-Path -Leaf
if($dbfile.Type -eq 'L'){
$newfile = Join-Path -Path $NewLogPath -ChildPath $DbFileName #The Log part of the "With Move"
} else {
$newfile = Join-Path -Path $NewDataPath -ChildPath $DbFileName #The Data parts of the "With Move"
}
$relocate += New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ($dbfile.LogicalName,$newfile)
}
Restore-SqlDatabase -ServerInstance localhost `
-Database $dbname `
-RelocateFile $relocate `
-BackupFile "$BackupFile" `
-RestoreAction Database #`
#-Script | Out-File $OutputFile #An optional parameter: instead of executing the command, it will be exported into a script file.
}
27 changes: 27 additions & 0 deletions Utility Scripts/Restore2.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$BackupFolder="C:\temp\Backup\"
$NewDataPath="c:\Temp\Data\" #The Data files directory (for the new DB)
$NewLogPath="c:\Temp\Log\" #The Log file directory (for the new DB)
$DBs = Get-ChildItem $BackupFolder -Filter *.bak | Out-GridView -OutputMode Multiple
foreach ($File in $DBs) {
Write-Host $File
$dbname = $File.Name.replace('.bak','') #The new DB name
$relocate = @() #This variable (for the Restore-SqlDatabase) is initialized as a list (and not an empty string), for the "With Move" part of the Restore command
$OutputFile="C:\temp\$dbname.sql" #A script file will be created
$BackupFile=$BackupFolder+$File.Name
$dbfiles = Invoke-Sqlcmd -ServerInstance localhost -Database tempdb -Query "Restore FileListOnly From Disk='$BackupFile';" #The Restore FileListOnly output is inserted into a variable
foreach($dbfile in $dbfiles){
$DbFileName = $dbfile.PhysicalName | Split-Path -Leaf
if($dbfile.Type -eq 'L'){
$newfile = Join-Path -Path $NewLogPath -ChildPath $DbFileName #The Log part of the "With Move"
} else {
$newfile = Join-Path -Path $NewDataPath -ChildPath $DbFileName #The Data parts of the "With Move"
}
$relocate += New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ($dbfile.LogicalName,$newfile)
}
Restore-SqlDatabase -ServerInstance localhost `
-Database $dbname `
-RelocateFile $relocate `
-BackupFile "$BackupFile" `
-RestoreAction Database #`
#-Script | Out-File $OutputFile #An optional parameter: instead of executing the command, it will be exported into a script file.
}

0 comments on commit 4d3face

Please sign in to comment.