Sql-server – SQL Server 2008 backup script

backupsharepointsql serversql-server-2008

I have a lot of SharePoint 2013 databases to backup and if update fail to restore, so I want to automate this operations.

ALTER DATABASE [TEST_1] SET OFFLINE WITH ROLLBACK IMMEDIATE
GO

BACKUP DATABASE [TEST_1] 
TO DISK = 'C:\path\test_1.bak' WITH NOINIT, STATS = 10
GO

ALTER DATABASE [TEST_1] SET ONLINE WITH ROLLBACK IMMEDIATE

But there is and error msg:

Msg 942, Level 14, State 4, Line 1
Database 'TEST_1' cannot be opened because it is offline.

Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.

Is it possbile to switch database offline, make backup and turn it in online mode after this?

Best Answer

Two things. First if you are going to do that then make your script this:

 ALTER DATABASE [TEST_1] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
 GO
 USE [TEST_1]
 GO
 BACKUP DATABASE [TEST_1] TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\test\test_1.bak' WITH NOINIT, STATS = 10
 GO
 ALTER DATABASE [TEST_1] SET MULTI_USER WITH ROLLBACK IMMEDIATE

Single user will kick everyone off but then you will be the first one back on with the backup. Then when you are done set it back to multi_user.

Second you might consider using Minion Backup. It's a free automated backup script that is supposed to be both very powerful and easy to use. The writers may very well have a config specifically for SharePoint or at the very least you can ask them. http://minionware.net/#miniontabs|2