自动备份压缩数据库--Global.asax

来源:互联网 发布:金山测速软件 编辑:程序博客网 时间:2024/05/16 11:52

       最近要求做一个自动备份压缩数据库的需求,很遗憾用的还是vs2003(以前最喜欢的版本,为啥?因为基本都要自己去敲,提示没2008,2010那么牛,部署还要手动,俺退化了......)

        言归正传——————

        这就要调用到Timers了,直接上代码:

   

/// <summary>/// Global 的摘要说明。/// </summary>public class Global : System.Web.HttpApplication{public static int CheckUpDateLock = 0;public static object LockObject = new Object();
         protected void Application_Start(Object sender, EventArgs e){
                           System.Timers.Timer t = new System.Timers.Timer(20000);//实例化Timer类,设置间隔时间为10000毫秒; t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; }
public void theout(object source, System.Timers.ElapsedEventArgs e) { SqlConnection conn=DAOCommon.GetConnection();//数据库后台连接方法 ,这个地方可以修改为 new SqlConnection("XX")string dbname=conn.Database;string DB_FilePath=System.Configuration.ConfigurationSettings.AppSettings["DB_FilePath"];string DB_FileTime=System.Configuration.ConfigurationSettings.AppSettings["DB_FileTime"];if(DB_FilePath!=null && DB_FilePath!="" &&DB_FileTime!=null && DB_FileTime!=""){if (DateTime.Now.ToShortTimeString() == DB_FileTime){lock (LockObject)//锁定{                if (CheckUpDateLock == 0) CheckUpDateLock = 1;else return;}  string strConnectionString=conn.ConnectionString;string strConn=strConnectionString.Replace(conn.Database,"master");SqlConnection sqlcon =new SqlConnection(strConn);try{if(sqlcon.State== ConnectionState.Closed){sqlcon.Open();}SqlCommand  sqlComm = new SqlCommand("SP_DB_RAR", sqlcon);sqlComm.CommandTimeout=200000;//设置命令的类型为存储过程sqlComm.CommandType = CommandType.StoredProcedure;//设置参数sqlComm.Parameters.Add("@FilePath_RAR", SqlDbType.VarChar);sqlComm.Parameters.Add("@DB_NAME", SqlDbType.VarChar);//为参数赋值sqlComm.Parameters["@FilePath_RAR"].Value =DB_FilePath;sqlComm.Parameters["@DB_NAME"].Value =conn.Database;SqlDataAdapter myAdapter = new SqlDataAdapter(sqlComm);//执行sqlComm.ExecuteNonQuery();// 解锁更新检查锁lock (LockObject){CheckUpDateLock = 0;}     }catch(Exception ex){throw ex;}finally{sqlcon.Close();}}}}   

其中调用了存储过程SP_DB_RAR,有两个参数FilePath_RAR(存储压缩数据库的路径),DB_NAME(要进行压缩的数据库名称)

脚本:

     

USE MasterGOIF OBJECT_ID('SP_DB_RAR') IS NOT NULL  DROP PROC SP_DB_RAR;GOcreate proc SP_DB_RAR(@FilePath_RAR varchar(100),@DB_NAME varchar(50))asbegin/** 开启xp_cmdshell支持 **/Exec sp_configure 'show advanced options', 1Reconfigure with overrideExec sp_configure 'xp_cmdshell', 1 Reconfigure with overrideExec sp_configure 'show advanced options', 0Reconfigure with override/** 定义参数 **/Declare--备份文件存储路径@FilePath Nvarchar(100),--数据库备份格式 为 数据库名称_日期(日期格式:20110326).bak --数据库名称@DbName Nvarchar(100),--数据库备份后缀@FileName Nvarchar(100),--被压缩文件名称@BakFile Nvarchar(100),--压缩文件名称@RarFileName Nvarchar(100),--rar文件存放路劲@RarFilePath varchar(100),@RarCmd Nvarchar(150),@Str varchar(100),@Dir varchar(100)Set @FilePath = @FilePath_RARSet @DbName = @DB_NAMESet @FileName = convert(varchar(10),getdate(),112)Set @RarFilePath = 'C:\Progra~1\WinRAR\RAR.exe'set @BakFile=@FilePath+@DbName+'_'+@FileName+'.bak'set @RarFileName=@FilePath+''+@DbName+'_'+@FileName+'.rar'/** 定义完成 **//** 开始完整备份数据库 **/Set @Str=@FilePath+@DbName+'_'+@FileName+'.bak'BACKUP DATABASE @DbName TO DISK=@strWITH RETAINDAYS=15,NOFORMAT,NOINIT,NAME=N'完整备份',SKIP,NOREWIND,NOUNLOAD,STATS=10/** 备份完成 **//** 开始压缩新备份文件 **/Set @RarCmd =@RarFilePath+' a -df -ep1 -m5 '+@RarFileName+' '+@BakFileExec master..xp_cmdshell @RarCmd/** 参数说明 **//** a:添加文件到压缩文件 -df:压缩后删除文件 -ep1:从名称中排除基本目录 -m5:压缩级别为最大 **//** 压缩完成 **//** 删除过期的压缩文件 **/Set @Dir='Del '+@FilePath+@DbName+'_'+convert(varchar(10),getdate()-4,112)+'.rar'Exec master..xp_cmdshell @Dir/** 删除结束 **//** 关闭xp_cmdshell支持 **/Exec sp_configure 'show advanced options', 1Reconfigure with overrideExec sp_configure 'xp_cmdshell', 1 Reconfigure with overrideExec sp_configure 'show advanced options', 0Reconfigure with override/** 过程完成 **/endGO--exec SP_DB_RAR 'H:\OCT_DB\','OCT_PM_v3'




这样就OK了!