My end goal is to have some code that automatically takes the Content Database backup from Production and automatically nightly restores it on Beta. Can I just do backup and restore sql commands? Or do I have to do some type of detach/attach process instead of restore?

I wrote a console app that does the following:

1. LockDownSharepoint - I turn off the timer service and set all site collections to Writeblocked and Readblocked ( is it neccessary to set ReadBlocked to true? or just writeblock)

2. Restore Content Database - I set the Database to singleuser mode - I run the restore command

string.Format("USE master RESTORE DATABASE [{0}] FROM DISK = N'{1}' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10", BackupDBName, FullFilePath);

  • I set the Database to multiuser mode

3.Unlock SharePoint - Turn on the timer service and set readblocked and writeblocked to false.

The code works in my development environment when I try to restore a database backup that I took from the same content database. restoring **DEV1_ContentDB over DEV1_ContentDB works**!

However, as soon as I run it in a different environment, but try to restore the content database from my dev environment, things go work. restoring DEV1_ContentDB over DEV2_ContentDB does NOT WORK.

It properly connects to the Database and runs the restore sql command with no errors. But when it is done the site collection is shot...and I can't connect to it to turn back read and write access..


So the issue was that I was trying to restore a database backup file to a database of a different type. First I tried renaming the target database to the same name as the source database, but that didn't work.

The Solution:

  1. Lock Down Sharepoint on SOURCE
  2. Physically copy the SOURCE_ContentDB.mdf
  3. Unlock SharePoint on Source
  4. Attach SOURCE_ContentDB.mdf to DESTINATION
  5. Go through Central Admin and remove the previous content database and add SOURCE_ContentDB

Those were the initial steps necessary.

Now I can use my code as desired to automatically take backup files from SOURCE and restore them to DESTINATION.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy