One of our TFS assignments needed to extend the current build and deployment process which includes Creating and Deploying Web package, Deploying Database, Versioning Assembly, running post-integration tests after deployment, etc., to include rollback which will perform backup and restore. We agreed on performing the backup during the deployment and the restore will be a separate build definition. For the backup during the deployment, I added the following section that backs up the DB and the Web App.
You can see this video if you would like to find more information about how to restore the old database to the new SQL server by running the command line and TFS Restore tool.
For the rollback and restore, I added the following section that restores the backup for both DB and Web. For the DB, I had to get exclusive access so I can restore the DB even if there are active connections from other clients.
1
"cmd.exe /k Sqlcmd -Q \"\"use master alter database "+DBNameForBackup+" set single_user with rollback immediate RESTORE DATABASE "+DBNameForBackup+" FROM DISK='"+DBNameForBackup+"_Bakup.bak' WITH REPLACE alter database "+DBNameForBackup+" set multi_user\"\" -S "+DBServerOrIP
You can see **this video if you would like to find more information about how to open the SQL Server Reporting Services Configuration Manager on the new Data Base tier (which is the application tier) and change the Database to the restored Data Base, how to restore the SSRS Encryption Key, how to solve the issue with the Deployment pointing on two servers using the RSKeyMngmt and RSKeyMngmt - r commands in the Command Prompt.
The build definition for that part will be as the following:
Invoke-Sqlcmd-Query"BACKUP DATABASE DATABASE1 TO DISK='Backups\\MyDB.bak'"-ServerInstance10.43.94.222-UsernameYourUserName-PasswordYourPassword
Restore a SQL Database using Invoke-Sqlcmd
1
Invoke-Sqlcmd-Query"RESTORE DATABASE DATABASE1 FROM DISK='Backups\\MyDB.bak' WITH REPLACE"-ServerInstance10.43.94.222-UsernameYourUserName-PasswordYourPassword
Set Database to Single User Mode and back to Multi User Mode
1
Invoke-Sqlcmd-Query"use master; alter database database1 set single_user with rollback immediate; alter database database1 set multi_user"-ServerInstance10.43.94.222-UsernameYourUserName-PasswordYourPassword
Restore a SQL Database and set to Multi User Mode in a single command
1
2
Invoke-Sqlcmd-Query"use master; alter database database1 set single_user with rollback immediate; RESTORE DATABASE DATABASE1 FROM DISK='Backups\\MyDB.bak' WITH REPLACE; alter database database1 set multi_user"-ServerInstance10.43.94.222-UsernameYourUserName-PasswordYourPassword
If you would like to learn more about using the Build Variables in VSTS and Release Management, - have a look at the following post: VSTS Build variables and Echo. The post describes how to see the output at any point of time, while automating a process, through setting variables and displaying them during the build.
sqlcmd
1
2
# Backup a SQL Database
sqlcmd -U YourUserName -P YourPassword -S 10.43.94.222 -Q"BACKUP DATABASE DATABASE1 TO DISK='Backups\\MyDB.bak'"
1
2
3
# Set Database to Single User Mode, Restore, and Set to Multi User Mode
sqlcmd -U YourUserName -P YourPassword -S 10.43.94.222 -Q"use master; alter database database1 set single_user with rollback immediate; RESTORE DATABASE DATABASE1 FROM DISK='Backups\\MyDB.bak' WITH REPLACE; alter database database1 set multi_user"
You can see this video If you would like to find more information about how to use the cliconfig command in the Command Prompt in the elevated mode to enable the TCP/IP and create a new alias in the SQL Server Client Network Utility, how to use the get-spdatabase in the SharePoint Management Shell and how to map each database with the correct ID using the db-ChangeDatabaseInstance method.
Web
MSDeploy Commands
Sync IIS Application to a Local Package
1
msdeploy -verb:sync -source:iisapp='Default web site/Lara'-dest:package='defaultWebsiteBackup.zip',encryptPassword=password123
Sync IIS Application to a Remote Package
1
msdeploy -verb:sync -source:iisapp='Default web site/Lara'-dest:package='defaultWebsiteBackup.zip',encryptPassword=password123 -computerName=172.18.0.333 -userName=YourUserName -password=YourPassword
Sync Local Package to IIS Application
1
msdeploy -verb:sync -source:package="C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\defaultWebsiteBackup.zip"-dest:auto -setParam:kind=ProviderPath,scope=iisApp,value='Default web site/Lara'
Sync Local Package to Remote IIS Application
1
msdeploy -verb:sync -source:package="C:\Windows\SysWOW64\defaultWebsiteBackup.zip"-dest:iisapp='Default web site/Lara'-computerName=172.18.0.333 -userName=YourUserName -password=YourPassword -setParam:kind=ProviderPath,scope=iisApp,value='Default web site/Lara'