您的位置:首页 > 数据库

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

2012-05-22 09:42 141 查看
最近要求做一个自动备份压缩数据库的需求,很遗憾用的还是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 Master
GO

IF OBJECT_ID('SP_DB_RAR') IS NOT NULL
DROP PROC SP_DB_RAR;
GO
create proc SP_DB_RAR
(
@FilePath_RAR varchar(100),
@DB_NAME varchar(50)
)
as
begin
/** 开启xp_cmdshell支持 **/
Exec sp_configure 'show advanced options', 1
Reconfigure with override
Exec sp_configure 'xp_cmdshell', 1
Reconfigure with override
Exec sp_configure 'show advanced options', 0
Reconfigure 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_RAR
Set @DbName = @DB_NAME
Set @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=@str
WITH RETAINDAYS=15,NOFORMAT,NOINIT,
NAME=N'完整备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
/** 备份完成 **/

/** 开始压缩新备份文件 **/
Set @RarCmd =@RarFilePath+' a -df -ep1 -m5 '+@RarFileName+' '+@BakFile
Exec 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', 1
Reconfigure with override
Exec sp_configure 'xp_cmdshell', 1
Reconfigure with override
Exec sp_configure 'show advanced options', 0
Reconfigure with override
/** 过程完成 **/

end

GO

--exec SP_DB_RAR 'H:\OCT_DB\','OCT_PM_v3'


这样就OK了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: