-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/MadeiraData/MadeiraToolbox
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |