SQL Server backup database SQL script (LAN)

Source: Internet
Author: User

-- ===================================================== ======
-- Author: Shu mu
-- Create Date: 2011-02-15
-- Description: SQL script used by SQL Server to back up a database (LAN)
-- ===================================================== ======
Create proc [DBO]. [usp_sys_backupdb]
@ Dbname varchar (50) = NULL, -- database to be backed up
@ Backuptype varchar (50) = 'full' -- Backup Type Full full backup diff differential backup
As
Begin
Exec sp_configure 'show advanced options', 1
Reconfigure

Declare @ dbname varchar (50)
Declare @ backuptype varchar (50)

Set @ dbname = db_name ()
Set @ backuptype = 'full'

declare @ filepath varchar (100) -- backup storage path
declare @ filename varchar (100) -- backup file full name
declare @ PWD varchar (50) -- username
declare @ uid varchar (50) -- password

Set @ dbname = isnull (@ dbname, db_name ())
Set @ filepath = '\ 192.168.1.125 \ F $ \ dbbak \ full'
If @ backuptype = 'diff'
Begin
Set @ filepath = Replace (@ filepath, 'full', 'diff ')
End

Set @ filename = @ filepath + '\'
+ @ Dbname
+ Convert (varchar (100), getdate (), 112)
+ '_'
+ @ Backuptype
Set @ Pwd = '02'
Set @ uid = 'wl-02 \ Administrator'

Exec master .. sp_configure 'xp _ Your shell', 1
Reconfigure

Declare @ authority varchar (100)
Set @ authority = 'net use' + @ filepath + '"' + @ PWD + '"/User: "' + @ uid + '"'
Exec master .. xp_mongoshell @ authority
-- Full backup
If @ backuptype = 'full'
Begin
Set @ filename = @ filename
+ '_'
+ Ltrim (STR (datepart (wk, getdate () % 2 ))
+ '. Bak'
Backup Database @ dbname to disk = @ filename with init, retaindays = 6
End

-- Differential backup
If @ backuptype = 'diff'
Begin
Set @ filename = @ filename
+ '_'
+ Ltrim (STR (datepart (DW, getdate ()-1 ))
+ '. Bak'
Backup Database @ dbname to disk = @ filename with init, differential, retaindays = 6
End

Declare @ del varchar (100)
Set @ del = 'net use' + @ filepath + '/delete'
Exec master .. xp_mongoshell @ del
Exec master .. sp_configure 'xp _ Your shell', 0
Reconfigure

Exec sp_configure 'show advanced options', 0
Reconfigure
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.