Use SQL Server's extended stored procedure for remote backup and recovery

Source: Internet
Author: User
Recently I am working on a framework for my company Program (Data application-oriented application system) as a data management module, the requirements of this module are relatively simple: backup, recovery and log clearing. My company's software basically uses C/S as the basic architecture, so the two main features of the data management module 'backup and restore' may be operated on the client side, backup and recovery files may also be stored on the client. Therefore, this data management module must be able to remotely back up and restore the database.
Article After explaining the premise, let's talk about how to implement it. In fact, they are all very simple. I want to write a remote backup test instance.
Let's take a look at it, so we can clearly describe it!
Instance description:
Environment: Win2k + sqlserver 2 K + query Analyzer
Sqlserver service instance name: mainserver
Name of the database to be backed up: MSDB
Local Machine name (client): David
Local User: ZF password 123
Local Domain: domain
Folder for local backup: e: est

Step 1: create a shared folder
In the programCodeNET Share test = E: est
Or use netmask to add this API.
Brief description:
NET Share: A Windows internal network command.
Purpose: create a local shared resource to display the shared resource information of the current computer.
Syntax: see net share /?

Step 2: Establish a shared credit relationship

Master.. xp_mongoshell 'net use \ David est 123/User: domainzf'
Brief description:
1: xp_mongoshell: an extended stored procedure of sqlserver.
To execute the specified command string in the command line interpreter of the operating system,
And return any output in text lines.
Syntax: see sqlserver online help
2: net use: A Windows internal network command.
Function: connects or disconnects a computer from a shared resource, or displays information about the computer.
Connection information. This command also controls persistent network connections.
Syntax: see net use /?

Step 3: Back up the database

Backup database MSDB to disk = '\ David estmsdb. Bak'
This does not need to be explained. For more information about the syntax, see SQL Server online help.

Step 4: delete Shared Folders
Call net share test/delete in the program code (or in the CMD window ).
Or use the netdomaindel API.
Result:
1376 pages have been processed. These pages belong to the database's 'msdbdata' file 'msdbdata' (located on file 1 ).
One page has been processed, which belongs to the database's 'msdblog' file (located on file 1 ).
The backup database operation successfully processed 1377 pages and took 3.653 seconds (3.086 MB/second ).

In this way, msdb on the mainserver is backed up to the E: estmsdb. Bak file of the David machine. Is it easy to use? The same is true for database restoration. You only need to change the statement in step 3 to 'Restore database MSDB from disk = '\ David estmsdb. Bak .. Can you try it after reading it ?! (The simplest test tool query analyzer + cmd window)

Note:The extended stored procedure xp_mongoshell can only be called by SA-level users, and it is one of the security risks of sqlserver. Many DBAs like to delete or disable it, so developers should be careful when using it.
The example in this article only briefly illustrates how to use the extended stored procedure to achieve remote backup and recovery, without any security or other considerations. We hope that the reader can improve the code.

Related Article

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.