SQL Server Scheduled backup database

Source: Internet
Author: User
Tags filetime getdate

CREATE PROCEDURE [dbo]. [sp_dbbackup_everynight_local] @cycle int,---save period @islocal int,---is local 0 means 1 means no @savepath NVARCHAR,---file save path \ \ 192.168.19.102\databaseback@dbprefix NVARCHAR (,---) generates a file prefix @server VARCHAR (,---server IP 192.168.19.102@username varchar,---Login Server user name Administrator@password VARCHAR---login server password soft_2016as BEGIN DECLARE @backpath2 V              Archar (+) DECLARE @command VARCHAR (200)--Creating a temporary table create table #Filetabel (  FILEPATH VARCHAR (+) NULL)--Call the DOS command to log on to the server IF @IsLocal = 1 Beginset @command = ' net use ' + @SavePath + ' + @PassWord + '/user: ' + @Server + ' \ ' + @UserName EXEC master. xp_cmdshell @command END-Adds all the file names of the shared directory to the temporary table SET @command = ' dir/b ' + @SavePath INSERT into # Filetabel EXEC Master. xp_cmdshell @command DELETE from #Filetabel WHERE FILEPATH are NULL while E Xists (SELECT * from #Filetabel)                                            BEGIN DECLARE @fileName VARCHAR SET @fileName = (SELECT TOP 1 * FROM #Filetabel ORDER by FILE PATH)--Define variable get file time DECLARE @fileTime VARCHAR DECLARE @fDa Tetime DATETIME IF @fileName is not NULL BEGIN SET @fileTime = SU Bstring (@fileName, 8) SET @fDateTime = CONVERT (DATETIME, @fileTime) DECL                                                is @lastTime DATETIME SET @lastTime = DATEADD (day, [email protected],                            CONVERT (VARCHAR), GETDATE (), 111)) IF @fDateTime <= @lastTime BEGIN-delete backup DECLARE @filePath VARCHAR SET @filePath = ' del ' + @SavePath + ' \ ' + @fileName EXEC Master.                                xp_cmdshell @filePath END--delete this piece of data                    DELETE from #Filetabel WHERE FILEPATH = @fileName                                    End end-Delete temporary table drop table #Filetabel --Back up the database to the server SET @backpath2 = @SavePath + ' \ ' + @DBPrefix + ' _ ' + RE                                  Place (CONVERT (VARCHAR), GETDATE (), +), '-', ') + ' _back.bak ' BACKUP DATABASE [LGS] to [email protected] with INIT, FORMAT END GO

  

Executes exec [dbo]. [Sp_dbbackup_everynight_local] 5,0, ' F:\LGSCMS_20141117_Back ', ' LGS ', ', ', '

SQL Server Scheduled backup database

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.