Remote database backup to local (no local database)

Source: Internet
Author: User

Recently I checked the method of remote database backup on the Internet, which seems quite troublesome. However, this module must be added to the project, so only one module can be created. Finally, two solutions are summarized:

The first solution is my favorite one:

First, use sqldataadapter to load all the tables into dataset, and write all the information into XML using the dataset class.

When restoring the database, use the sqldataadapter to query the content of the loaded database, read the dataset, and then use the update () of the sqldataadapter.

The specific code will not be written. This method is very simple and easy to use.

 

 

The second solution is to back up database files.

We all know that direct remote database backup is not a problem, but backup files can only be stored on the server. It is difficult to transmit the backup files to the client. However, the database has a function called mongoshell, which can be used to send DOS commands to the server where the database is located to directly operate the server. Oh, is it really a hacker? However, this approach is not difficult. It can be completed through SQL statements.

 

1. First, you must enable the mongoshell function of the database:

Exec sp_configure 'show advanced options', 1; reconfigure; Exec sp_configure 'xp _ Your shell', 1; reconfigure;

Now we have full control over the server. If the Login User Name of the server does not allow operations on the server, you can create a new user to improve the permissions.

Exec xp_cmdshell 'net user mm 123/add' -- add a user
Exec xp_cmdshell 'net localgroup administrators mm/add' -- add administrator permissions to users.

 

2. Create a shared folder on the client.

3. The server creates a ing to the shared folder.

Exec master.. xp_shareshell 'net use Z: // Client IP/Shared Folder Share Name "client's Windows login password"/User: Server IP/client's windows Username'

Z is the ing name. If you do not know what ing is, you can try to create one by yourself. Right-click my computer and map the network drive. Map to a shared folder. In this way, a "z disk" will be added to my computer, and the shared folder will be directly entered. Note that some network knowledge is involved here. For example, if you are not in the same domain as the server, the ing may cause problems. The prompt from the database is always strange, such as the wrong user name.

 

4. The database with init on the backup server overwrites ¦ noinit to add

Backup database name to disk = 'C:/backup file name. Bak' with init

 

5. Copy the backup file to the ing Disk

Exec master .. xp_mongoshell 'Copy C:/backup file name. Bak Z :'

 

6. Clear traces (the hacker is here, of course, to clear the traces)

-- Exec master .. xp_mongoshell 'del C:/backup file name. Bak' Delete backup file

-- Exec master.. xp_mongoshell 'net use Z:/delete' Delete ing

 

Note:
Note that if the ing fails to be created or the ing fails to be deleted, the database returns only one cause of failure, and the program still calculates "Get query result" instead of returning an exception. Therefore, when using C # To write code, do not use catch to capture failed operations. It is determined based on the returned result value.

 

 

Add the code to set the local folder to share:

/// <Summary>
/// Set folder sharing
/// </Summary>
/// <Param name = "folderpath"> folder path </param>
/// <Param name = "sharename"> share name </param>
/// <Param name = "Description"> Share Comments </param>
/// <Returns> </returns>
Public int sharenetfolder (string folderpath, string sharename, string description)
{

Try
{
Managementclass = new managementclass ("win32_share ");
// Create managementbaseobjects for In and out parameters
Managementbaseobject inparams = managementclass. getmethodparameters ("CREATE ");
Managementbaseobject outparams;
// Set the input parameters
Inparams ["Description"] = description;
Inparams ["name"] = sharename;
Inparams ["path"] = folderpath;
Inparams ["type"] = 0x0; // disk drive
// Another type:
// Disk_drive = 0x0
// Print_queue = 0x1
// Device = 0x2
// IPC = 0x3
// Disk_drive_admin = 0x80000000
// Print_queue_admin = 0x80000001
// Device_admin = 0x80000002
// Ipc_admin = 0x8000003
// Inparams ["maximumallowed"] = int maxconnectionsnum;
// Invoke the method on the managementclass object
Outparams = managementclass. invokemethod ("CREATE", inparams, null );
// Check to see if the method invocation was successful
If (uint) (outparams. properties ["returnvalue"]. Value )! = 0)
{
Throw new exception ("unable to share directory .");
}
}
Catch
{
Return-1;
}
Return 0;
}

 

 

/// <Summary>
/// Cancel folder sharing
/// </Summary>
/// <Param name = "sharename"> shared name of the folder </param>
/// <Returns> </returns>
Public int cancelsharenetfolder (string sharename)
{
Try
{
Selectquery = new selectquery ("select * From win32_share where name = '" + sharename + "'");
Managementobjectsearcher searcher = new managementobjectsearcher (selectquery );
Foreach (managementobject Mo in searcher. Get ())
{
Mo. invokemethod ("delete", null, null );
}
}
Catch
{
Return-1;
}
Return 0;
}

 

 

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.