Back up the local database to the server (Stored Procedure)

Source: Internet
Author: User
Code
Create procedure backupproc
@ Dbname varchar ( 50 ), -- Database to be backed up
@ Filepath varchar ( 50 ), -- File storage server. If it is not a local backup, it stores the server address and its shared folder name, as shown in figure ' \ 192.168.0.39 \ file name '
@ Logouser varchar ( 50 ), -- The user name of the file server. The user name created by the server with all permissions on the shared folder
@ Logepwd varchar ( 50 ) -- File Server Password
As
-- Step 1 Backup
Declare @ dbpath varchar ( 100 )
Declare @ copypath varchar ( 200 )

set @ dbpath = ' C: \ ' + @ dbname + convert (varchar ( 10 ), getdate (), 120 ) + ' . bak '
Backup Database @ dbname to disk = @ dbpath

--Step2Copyfile
Declare @ brief copy varchar (500)--Copy statement
Declare @ Alibaba logo varchar (500)--Create a session
Declare @ logging logoout varchar (500)

Set @ Brief copy = ' Copy c :\ ' + @ Dbname + Convert (varchar ( 10 ), Getdate (), 120 ) +   ' . Bak '
+ @ Filepath + ' \ ' + @ Dbname

Set @ Brief logo = ' Net use ' + @ Filepath + '   ' + @ Logepwd + ' /User: '   +   ' Administrator \ ' +  

@ Logouser
Set@ Brief logoout='Net use'+@ Filepath+'/Delete'
Exec master .. xp_mongoshell @ mongologo
Exec master .. xp_mongoshell @ mongocopy
Exec master .. xp_mongoshell @ mongologoout

-- Step 3 Delete old file
Declare @ delfile varchar ( 100 )
Declare @ mongodel varchar ( 200 )
Set @ Delfile = @ Dbname + Convert (varchar ( 10 ), Getdate (), 120 ) + ' . Bak '  

Set@ Mongodel='Del c :\'+@ Delfile--Delete a local backup database
Exec master .. xp_mongoshell @ mongodel

Follow these steps to create a server shared folder:

1. Machine A and machine B create a Windows user with the same name. Set the user group to "Administrators" and set the same password as the valid user accessing the backup folder. The operation is as follows:
My computer
-- Control Panel
-- Management Tools
-- Computer Management
-- Users and groups
-- Right-click the user
-- Create a user
-- Create a Windows login user affiliated to the Administrator Group

2. Create a new shared directory on the B server as the directory for storing backup files:
My computer -- D: \ creates a directory named Bak
-- Right-click the newly created directory
-- Property -- share
-- Select "share this folder"
-- Use the "permission" button to set specific user permissions to ensure that the user created in step 1 has all permissions on the folder
-- OK


3. Set the start user of the MSSQLServer and SQLServerAgent services
Start -- Program -- Management Tools -- services
-- Right-click MSSQLServer
-- Property -- login -- select "this account"
-- Enter or select the Windows logon user name created in step 1.
-- Enter the user's password in "password"
-- OK
-- Set SQLServerAgent in the same way

 

The above texts are all from other posts. I have only made slight modifications and integration, and hope it will be useful to you!

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.