Remote backup and recovery of databases by extended stored procedures in SQL Server _mssql

Source: Internet
Author: User
This paper analyzes the SQL Server database extended stored procedure through an example, and realizes the method and step of remote backup and recovery.
Example Description:

Environment: Win2k+sqlserver 2k+ Query Analyzer
SQL Server Service Instance name: Mainserver
Database name to be backed up: msdb
Local machine name (client side): David
Local User: ZF Password: 123
Local Domain name: domain
A folder for local backup requirements: e:est

   First step: Create a shared folder

Call (or CMD window) net share in program code Test=e:est

Or use the Netshareadd API

Brief description:

NET share: is a network command within Windows.

Role: Create a local shared resource that displays shared resource information for the current computer.

Syntax: see net share/?

  Step Two: Establish a shared credit relationship

Master.. xp_cmdshell ' net use \david est 123/user:domainzf '

Brief description:

1:xp_cmdshell: is an extended stored procedure for SQL Server.

function, executing the given command string in the manner of the operating system command-line interpreter,

and returns any output as a text line.

Syntax: See SQL Server online Help

2:net use: is a network command within Windows.

To connect or disconnect a computer from a shared resource, or to display information about a computer

The information for the connection. This command also controls persistent network connections.

Syntax: see net use/?

   step three: Back up the database

Backup DATABASE msdb to disk= ' \david Estmsdb.bak '

This doesn't need to be explained. syntax See SQL Server online Help

   Fourth Step: Delete the shared folder

Call (or CMD window) net share in program code Test/delete

Or use the Netsharedel API

Results:

Processed 1376 pages that belong to the file ' Msdbdata ' (located on file 1) of the database ' msdb '.

Processed 1 pages that belong to the file ' Msdblog ' (located on file 1) of the database ' msdb '.

The BACKUP DATABASE operation successfully processed 1377 pages and took 3.653 seconds (3.086 mb/sec).

So the mainserver on the server's msdb back to the David Machine's E:estmsdb.bak file, it is easy to use it? The same is true with the restore database operation, as long as the statement of the third step is changed to ' Restore database msdb from disk= ' \david estmsdb.bak ' is OK. Can you try it after you finish?! (Simplest test Tool Query Analyzer +cmd window)

Note: xp_cmdshell this extended stored procedure can only be invoked by the SA-level user and is one of the security vulnerabilities of SQL Server, which many DBAs like to remove or disable, so be careful when using the developer.
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.