Automatic database Restoration

Source: Internet
Author: User

In the previous section, we introduced the automatic database backup and upload operations, and then completed the automatic remote database function.

First, close all user connections. The Code is as follows;

Create proc killDb
@ Dbname varchar (1000)
As
Begin
Declare @ s varchar (40)
Declare tb cursor
Select N 'Kill '+ cast (spid as varchar) from master... sysprocesses
Where dbid = db_id (@ dbname)
Open tb
Fetch next from tb into @ s
While @ fetch_status = 0
Exec (@ s)
Fetch next from tb into @ s
Close tb
Deallocate tb
End

 

Next, execute Restoration:

Exec killDb 'northwind '-- close all user connections
Exec xp_cmdshell '"C: \ Program Files \ WinRAR \ UNRAR.exe" e-df-O + D :\~ Temp \ Northwind.rar D :\~ Temp \'
If day (getdate () = 8
Begin
Restore database [Northwind] from disk = 'd :\~ Temp \ Northwind. bak 'with recovery, replace, move n 'mxcement _ data' TO n' e: \ Northwind. mdf ',
Move n 'mxcement _ log' TO n' e: \ Northwind. ldf' -- recovery cannot restore other Log files
End
Else
Begin
Restore database [Northwind] from disk = 'd :\~ Temp \ Northwind. bak 'with norecovery, replace move n 'mxcement _ data' TO n' e: \ Northwind. mdf ',
Move n 'mxcement _ log' TO n' e: \ Northwind. ldf' -- norecovery can restore other Log files
Restore database [Northwind] from disk = 'd :\~ Temp \ Northwind. cha 'with recovery
End

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.