In today’s world we rely more on third party tools to do a Backup and Restore of databases. Most of us still use SQL Native method to do a Database Backup and Restoration. This post illustrates the quickest way of taking backup and restore it on the destination server. I’m using Robocopy utility to perform the data transfer activity. Please refer below one of my previous post on Robocopy.
https://sqlpowershell.wordpress.com/2013/07/05/sqlcmd-quick-copy-of-files-using-robocopy/
By default, SQLCMD mode is not enabled in the Query Editor. You can enable scripting mode by clicking the SQLCMD Mode icon in the toolbar or by selecting SQLCMD Mode from the Query menu. Please refer this link for more information. Go to SSMS Menu -> Query ->Select SQLCMD.
copy and paste the below code SSMS and Modify the below variables as per requirement . I would rather say it to download it from the below link
T-SQL Code is here – Backup and Restore
:setvar DATABASE UAT2010_Search
:setvar SOURCE HQSPDBSU01
:setvar DESTINATION HQSPDBSU02
:setvar BACKUPPATH g:\MSSQL
:setvar RESTOREPATH g:\MSSQL
:setvar LOGICALDATANAME UAT2010_Search
:setvar LOGICALLOGNAME UAT2010_Search_Log
:setvar RESTOREDATAPATH “G:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA”
:setvar RESTORELOGPATH “G:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA”
:setvar COPYPATH g$\MSSQL
:setvar Timeout 10
—Before Execution
:CONNECT $(SOURCE)
select * from sys.databases where name=’$(DATABASE)’
Go
:CONNECT $(DESTINATION)
select * from sys.databases where name=’$(DATABASE)’
SET NOCOUNT ON
GO
:CONNECT $(SOURCE)
Go
print ‘*** Take full backup of Source database $(DATABASE) with copy_only option***’
IF (left(cast(SERVERPROPERTY(‘ProductVersion’)as varchar),5)=’10.50′ and SERVERPROPERTY(‘EngineEdition’)=3) OR (left(cast(SERVERPROPERTY(‘ProductVersion’)as varchar),5)=’10.00′ and SERVERPROPERTY(‘EngineEdition’)in(2,3))
BEGIN
— Compression Option is set
BACKUP DATABASE $(DATABASE) TO DISK = ‘$(BACKUPPATH)\$(DATABASE).bak’
WITH COPY_ONLY, NOFORMAT, INIT, NAME = ‘$(DATABASE) Full Database Backup’, SKIP, NOREWIND, NOUNLOAD, STATS = 10,COMPRESSION
END
ELSE
BEGIN
–Backups are not compressed for older versions
BACKUP DATABASE $(DATABASE) TO DISK = ‘$(BACKUPPATH)\$(DATABASE).bak’
WITH COPY_ONLY, NOFORMAT, INIT, NAME = ‘$(DATABASE) Full Database Backup’, SKIP, NOREWIND, NOUNLOAD, STATS = 10
END
GO
—-2. Copy the files from Source to Destination –
print ‘*** Copy database $(DATABASE) from Source server $(Source) to Destination server $(destination) ***’
!!ROBOCOPY $(BACKUPPATH)\ \\$(Destination)\$(COPYPATH) $(DATABASE).*
GO
—–3. Restore database to Destination
print ‘*** Restore full backup of database $(DATABASE) ***’
:CONNECT $(Destination)
GO
RESTORE DATABASE $(DATABASE)
FROM disk = ‘$(RESTOREPATH)\$(DATABASE).bak’
WITH RECOVERY, NOUNLOAD, STATS = 10,REPLACE,
MOVE ‘$(LogicalDataName)’ TO
‘$(RestoreDataPath)\$(LogicalDataName).mdf’,
MOVE ‘$(LogicalLogName)’
TO ‘$(RestoreDataPath)\$(LogicalLogName).ldf’
GO
— After Execution
:CONNECT $(SOURCE)
select * from sys.databases where name=’$(DATABASE)’
Go
:CONNECT $(DESTINATION)
select * from sys.databases where name=’$(DATABASE)’
Before –
After –
Hello Prashant
It was just phenomenal, Can you provide the same with the powershell to automate the db Refreshes. Here’s the scenario
1)We used to take backups that exist on the same folder of around more than 10 databases
2)script out users,privileges -note-the output of this should be in text format.
3)Restore the multiple databases from that backup folder
4)Execute the scripts of step 2
Any help would be great !
LikeLike
Hi Kyo,
I’m working the restore part. Its done. I will posting it shortly.
Check out of backup part in the below link
https://sqlpowershell.wordpress.com/2016/05/11/powershell-backup-specific-group-of-sql-databases/
Prashanth
LikeLike
Hi Kyo,
I hope my new post would answer your question
Please check
https://sqlpowershell.wordpress.com/2016/05/12/powershell-sql-database-refresh-restore-multiple-databases/
Prashanth
LikeLike
Hello PJ,
Sorry for the delay though, this was the perfect thing that I was looking for but this doesn’t not include the 2)script out users,privileges -note-the output of this should be in text format.
of the step 2 of my requirement and then later we can execute the results of this scripts to map the users again with the appropriated privileges.
Any help would be much appreciable.
LikeLike
Hi Kyo,
There are lot of scripts are readily available. I hope you can try that first.
Prashanth
LikeLike
Try the below link
https://gallery.technet.microsoft.com/scriptcenter/Use-PowerShell-to-Migrate-86c841df
Let me know if you need any assistance.
Prashanth
LikeLike
Kyo,
Try this my new post
https://sqlpowershell.wordpress.com/2016/05/23/powershell-scripting-logins-role-assignments-and-server-permissions-using-powershell/
let me know in case of any issues
Prashanth
LikeLike