SQL Server 資料備份預存程序[原創]

來源:互聯網
上載者:User

今天開園,分享一下我一直在使用的資料備份預存程序,歡迎轉載!!!

USE [master]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER OFFGOCREATE Proc [dbo].[SQL_Backup]@DataBase Nvarchar(50)ASDUMP TRANSACTION @DataBase WITH  NO_LOGBACKUP LOG @DataBase WITH NO_LOGDBCC SHRINKDATABASE (@DataBase)---------------------------- 原創SQL備份資料庫 ------------------------------DECLARE @DataBase Nvarchar(100)DECLARE @DataPath Nvarchar(100)DECLARE @FileName Nvarchar(100)   DECLARE @BackupFileName Nvarchar(100)DECLARE @DataDescription Nvarchar(200)DECLARE @DataMediaName Nvarchar(50)DECLARE @DataMediaDescription Nvarchar(200)DECLARE @OkInfo Nvarchar(300)DECLARE @str Nvarchar(300)DECLARE @Rnd Nvarchar(11)DECLARE @dir Nvarchar(15)DECLARE @dirin bitDECLARE @Temp Nvarchar(500)-- Author: im531...-- Description: SQL_Backup-- http://im531.cnblogs.com/-- Date: 2005-02-06--SET @DataBase = 'test'SET @Temp = ''SET @DataPath = 'C:\www\SQL_Backup\'SET @Rnd = (SELECT Left(Newid(),4) + Right(Newid(),4)) + (SELECT Right(Datepart(ms,Getdate()),3))SET @str = RTRIM(CONVERT(CHAR(10),GETDATE(),112)) + '_' + RTRIM(DATEPART(HOUR,GETDATE())) + '_' + LTRIM(DATEPART(MINUTE,GETDATE()))SET @dir = RTRIM(CONVERT(CHAR(10),GETDATE(),112)) + '_' + RTRIM(DATEPART(HOUR,GETDATE()))SET @FileName = @DataPath + @dir + '\'+ @DataBase + '_Backup_'SET @BackupFileName = @FileName + @str  + '_' + @RndCREATE TABLE [#tb](a bit,b bit,c bit)SET @Temp = @DataPath + @dirINSERT INTO [#tb] EXECUTE master..xp_FileExist @TempIF Exists(SELECT 1 FROM [#tb] WHERE b = 1)SET @dirin = 1ELSESET @dirin = 0DROP TABLE [#tb]SET @Temp = 'md ' + @DataPath + @dirIF @dirin = 0BEGINEXEC master..xp_cmdshell @TempENDSET @DataDescription = 'SQL語句產生的備份,備份時間:' + CONVERT(CHAR(19),GETDATE(),121)SET @DataMediaName = 'im531 Backup ...'SET @DataMediaDescription = 'Author im531 ... 'SET @OkInfo = '資料庫 ' + @DataBase + ' 成功備份至 ' + @BackupFileNameBACKUP DATABASE @DataBase TO DISK = @BackupFileNameWITH NOINIT , NOUNLOAD , NOSKIP , STATS = 10 , NOFORMAT ,NAME = @DataBase , DESCRIPTION = @DataDescription , MEDIANAME = @DataMediaName , MEDIADESCRIPTION = @DataMediaDescription-- 7z 壓縮IF @DataBase = 'DataName'BEGINSET @Temp = 'C:\7-zip\7z.exe a -t7z ' + @DataPath + @dir + '.7z ' + @DataPath + @dir + '\* -mx9 -r'EXEC master..xp_cmdshell @TempSET @Temp = 'rd ' + @DataPath + @dir + ' /s/q'EXEC master..xp_cmdshell @TempENDSELECT @OkInfo AS BackupInfo------------------- End ---------------------GOEXEC SQL_Backup@DataBase = N'DataName'
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.