• SQL Server 2008 备份改进版


    1.Add compressing function with 7-Zip

    2.With tool win.rar code so you can change it if you want

     1 USE [master]
     2 GO
     3 /****** Object:  StoredProcedure [dbo].[sp_BackupDBBatch]    Script Date: 09/27/2013 22:25:40 ******/
     4 SET ANSI_NULLS ON
     5 GO
     6 SET QUOTED_IDENTIFIER ON
     7 GO
     8 /*-----------------------------
     9     Description:
    10         1.Backup multiple Database
    11         2.If you want to backup database except 'mastermodelmsdb	empdb',you can input '%' of the second parameter
    12         3.parameter 1 is the path where you want to back,parameter 2 is the database name you want to backup
    13         3.This procedure backup database with fuzzy database name
    14         
    15         
    16     Author:jiangxiaoqiang
    17     Date:2013-09-27
    18     Modify history:
    19     2013-09-27    jiangxiaoqiang    add compressing function,the compressing tool 7-Zip.
    20 -----------------------------*/
    21 
    22 
    23 
    24 
    25 ALTER procedure [dbo].[sp_BackupDBBatch]
    26     @backup_path varchar(200),
    27     @backup_name varchar(200)
    28 as
    29 begin
    30     declare @DynamicSQL varchar(256)
    31     set @DynamicSQL='if not exist '+@backup_path+'\%Date:~0,4%%Date:~5,2%%Date:~8,2% mkdir '+@backup_path+'\%Date:~0,4%%Date:~5,2%%Date:~8,2%'
    32     exec xp_cmdshell @DynamicSQL
    33     
    34     declare @backup_path_detail varchar(200)
    35     set @backup_path_detail=@backup_path+''+CONVERT(char(8),GETDATE(),112)
    36     --declare variable
    37     DECLARE @dbname nvarchar(200)
    38     --SET @backup_path='D:DatabaseBackup'--you can define by yourself
    39     /*
    40      declare cursor syntax
    41      DECLARE cursor_name CURSOR 
    42      [LOCAL | GLOBAL] 
    43      [FORWARD_ONLY | SCROLL] 
    44      [STATIC | KEYSET | DYNAMIC | FAST_FORWARD] 
    45      [READ_ONLY | SCROLL_LOCKS | OPTIMISTIC] 
    46      [TYPE_WARNING] 
    47      FOR select_statement 
    48      [FOR UPDATE [OF column_name [,...n]]] 
    49     */
    50     DECLARE db_info CURSOR --declare cursor
    51     LOCAL
    52     STATIC
    53     READ_ONLY
    54     FORWARD_ONLY
    55     FOR --add other condition by select
    56     SELECT name
    57     FROM master.sys.databases with(NOLOCK)
    58     WHERE database_id>4 and name like '%'+@backup_name+'%'--maser,model,msdb,tempdb
    59      --here you can add the condition like:name like'NJPEC%'
    60     OPEN db_info --open cursor
    61     FETCH NEXT FROM db_info INTO @dbname
    62    
    63     WHILE @@FETCH_STATUS=0
    64     BEGIN
    65      EXEC master.dbo.backupDB @dbname,@backup_path_detail
    66      FETCH NEXT FROM db_info INTO @dbname
    67     END
    68     CLOSE db_info
    69     DEALLOCATE db_info
    70 ---------------------------END of backup database--------------------------------
    71    --Find&list all database
    72    /*
    73     select * 
    74     from master.sys.databases
    75     order by database_id;
    76    */
    77    
    78    /*
    79         This code to compress backup file to specify dictionary.
    80         My tool is 7zip,and you can change the code with your
    81         specifical compress tool.Read the help manual.    
    82         
    83         There is another tool code,you can reference:
    84         select @sql='"C:Program Files (x86)WinRAR
    ar.exe" a -ep '+@RarFullFileName+' '+@BakFullFileName
    85         exec xp_cmdshell @sql        
    86    */
    87    
    88    set @DynamicSQL='cd /d D:Program Files7-Zip&&7z a '+@backup_path_detail+''+CONVERT(char(8),GETDATE(),112)+'.zip '+@backup_path_detail+'*.bak -mx9'
    89    exec xp_cmdshell @DynamicSQL   
    90    -------------------------------end of compressing file----------------------------------------
    91 end
    92 
    93 
    94 --exec sp_BackupDBBatch 'F:akDB','Arc'
     1 USE [master]
     2 GO
     3 /****** Object:  StoredProcedure [dbo].[backupDB]    Script Date: 09/27/2013 23:33:02 ******/
     4 SET ANSI_NULLS ON
     5 GO
     6 SET QUOTED_IDENTIFIER ON
     7 GO
     8 ALTER PROC [dbo].[backupDB] 
     9 (
    10   --We'd better add bracket even if no parameters,it is up to you.
    11   @DatabaseName nvarchar(200), 
    12   @Path nvarchar(200)
    13 )
    14 AS
    15 BEGIN
    16  DECLARE @FileName varchar(200),@sql varchar(1000)
    17  --RIGHT function:return the @Path last X word
    18  --CONVERT function:CONVERT(data_type(length),data_to_be_converted,style)
    19  --112 representative yymmdd format
    20  --108 representative hh:mm:ss format
    21   SET @FileName=@Path+(CASE WHEN RIGHT(@Path,1)<>'' THEN '' ELSE '' END)
    22            +@DatabaseName+'_'+CONVERT(char(8),GETDATE(),112)+'_' 
    23            +replace(convert(char(8),getdate(),108),':','')+'.bak'
    24   --to explain the ''':the ' before to and the last ' was a couple.
    25   --the backup folder must be exists,and N must be uppercase.
    26   --what's the 'N' meaning?It means the varible using code with unicode.
    27   SET @sql='backup database '+@DatabaseName+' to disk =N'''+@FileName+''''
    28   EXEC(@sql)
    29  END

    This code have many comment,if someone make it progress,thank you!

  • 相关阅读:
    Eclipse插件安装方法大全
    Weblogic web应用中获取文件的绝对路径
    Weblogic 设置优先引用web项目的jar包
    Oracle 数据库创建(图形界面操作)
    Java 输入输出流总结
    如何获取项目中文件的路径
    EclEmma安装与使用
    单向链表

    散列表
  • 原文地址:https://www.cnblogs.com/jiangxiaoqiang/p/3343779.html
Copyright © 2020-2023  润新知