码迷,mamicode.com
首页 > 数据库 > 详细

看代码网备份|利用WebClient|eKing.CmdDownLoadDbBakOper|实现定时拷贝数据库备份文件到文件服务器

时间:2018-08-06 00:32:08      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:虚拟目录   name   地址   rms   recv   抛出异常   targe   err   enable   

摘要:

1、有两台服务器
(1)看代码网(记为A):内网IP:10.186.73.30
(2)文件服务器(记为B):内网IP:10.135.87.157
2、在A架设一个网站,端口8088(防火强设置B才能访问)
3、在B运行计划任务,每天定时从A的8088端口网站下载数据库备份文件到B
 
优点:
1、利用iis,无需架设ftp
2、利用防火墙,确保只能指定IP和端口可以访问
3、利用windows计划任务,每天定时备份
4、备份成功邮件定时提醒
 
原文链接:
备份策略:
1、在A服务器每天定时备份数据文件到D:\DataBase\AutoBackAll目录的LookDaiMaDB下
 
2、在A服务器的D:\DataBase\AutoBackAll目录下架设一个IIS网站,端口8088
 
3、在腾讯云服务器上设置安全策略,只能B服务器才能访问8088端口
 
4、(重复安全设置)在A服务器防火墙设置只有B服务器才能访问
 
5、在IIS设置trn文件能下载,见文章:
 
6、在IIS网站下面添加DbLookDaiMaBak.aspx文件
实现文件的读取和下载

DbLookDaiMaBak.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.IO;

public partial class DbLookDaiMaBak 

System.Web.UI.Page
{

#region DateTimeGetByStrYyyyMMddHHmmss|yyyyMMddHHmmss转成时间

/// <summary>
/// yyyyMMddHHmmss转成时间
/// </summary>
/// <param name="str">时间格式字符串(要求14位长)|如:20180621020355
/// <returns>如果格式不正确,抛出异常|否则返回时间值|如:2018-06-21 02:03:55</returns>
public DateTime DateTimeGetByStrYyyyMMddHHmmss(string str)
{
if (str == null || str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "str == null || str.Length == 0"
);
}

int iLen = str.Length;

if (iLen != 14)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "str.Length != 8"
);
}

string dateStr
=
str.Substring(0, 4)
+ "-" + str.Substring(4, 2)
+ "-" + str.Substring(6, 2)
+ " " + str.Substring(8, 2)
+ ":" + str.Substring(10, 2)
+ ":" + str.Substring(12, 2);

return DateTime.Parse(dateStr);
}



/// <summary>
/// yyyyMMddHHmmss转成时间
/// </summary>
/// <param name="str">时间格式字符串(要求14位长)|如:20180621020355
/// <param name="defaultValue">如果时间格式不正确,返回的时间默认值
/// <returns>如果格式不正确,返回默认值defaultValue|否则返回时间值|如:2018-06-21 02:03:55</returns>
public DateTime DateTimeGetByStrYyyyMMddHHmmss(string str, DateTime defaultValue)
{
if (str == null || str.Length == 0)
{
return defaultValue;
}

int iLen = str.Length;

if (iLen != 14)
{
return defaultValue;
}

string dateStr
=
str.Substring(0, 4)
+ "-" + str.Substring(4, 2)
+ "-" + str.Substring(6, 2)
+ " " + str.Substring(8, 2)
+ ":" + str.Substring(10, 2)
+ ":" + str.Substring(12, 2);

DateTime theResult = defaultValue;

if (DateTime.TryParse(dateStr, out theResult))
{
return theResult;
}

return defaultValue;
}

#endregion DateTimeGetByStrYyyyMMddHHmmss|yyyyMMddHHmmss转成时间

#region GetGB2312MD5|获得GB2312(中文编码)格式的MD5值

/// <summary>
/// 获得GB2312编码的MD5值
/// </summary>
/// <param name="s">需要MD5的字符串
/// <returns></returns>
public string GetGB2312MD5(string s)
{
if (s == null)
s = "";

MD5 md5 = new MD5CryptoServiceProvider();

System.Text.Encoding en = System.Text.Encoding.GetEncoding("GB2312");

byte[] t = md5.ComputeHash(en.GetBytes(s));

StringBuilder sb = new StringBuilder(32);

for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, ‘0‘));
}

return sb.ToString();
}

#endregion GetGB2312MD5|获得GB2312编码的MD5值


/// <summary>
/// 
/// </summary>
/// <param name="callName">
/// <param name="dt">
/// <returns></returns>
protected string ToSign(string callName, string dt)
{
string str = callName + dt + "ekinglbs.lookdaima";

return GetGB2312MD5(str);
}

/// <summary>
/// 获得网站根目录(比如网站虚拟目录tools) - http://www.slowx.net/tools/
/// </summary>
/// <returns></returns>
public string GetWebRootUrl()
{
HttpContext hc = HttpContext.Current;

if (hc == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:HttpContext hc = HttpContext.Current值为null。"
);
}

// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;

// 完整URL地址 //
string strAbsoluteUri = hc.Request.Url.AbsoluteUri;

int idx = strAbsoluteUri.LastIndexOf(strPathAndQuery);

if (idx == -1)
{

throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "int idx = strAbsoluteUri[" + strAbsoluteUri + "].LastIndexOf(strPathAndQuery[" + strPathAndQuery + "]) == -1;"
);
}

string theResult = strAbsoluteUri.Substring(0, idx);

if (hc.Request.ApplicationPath == "/")
return theResult;
else
return theResult + hc.Request.ApplicationPath;
}

/// <summary>
/// 
/// </summary>
/// <returns></returns>
protected string DataBindTheControls(bool isDebug)
{
if (isDebug)
{
string debugWebRootUrl = GetWebRootUrl();


if (debugWebRootUrl.EndsWith("/") || debugWebRootUrl.EndsWith("\\"))
{
debugWebRootUrl = debugWebRootUrl.Substring(0, debugWebRootUrl.Length - 1);
}

return "+" + debugWebRootUrl + "/debugdata.zip";

}

string noPower = "-鉴权失败:" + Request.Url.AbsoluteUri;

string callName = Request.QueryString["CallUserName"];
string dt = Request.QueryString["dt"];
string sign = Request.QueryString["sign"];

if (callName == null || callName.Length == 0)
return noPower + "[callName]";

if (dt == null || dt.Length == 0)
return noPower +"[dt]";

if (sign == null || sign.Length == 0)
{
return noPower + "[sign]";
}

DateTime dtValue = DateTimeGetByStrYyyyMMddHHmmss(dt, DateTime.MinValue);

if (dtValue == DateTime.MinValue)
{
return noPower + "[dt.MinValue]";
}

if (callName != "ekinglbs")
{
return noPower + "[callName=" + callName + "]";
}

string newSign = ToSign(callName, dt);

if (newSign != sign)
{
return noPower + "[callName=" + callName + "]" + "[dt=" + dt + "]" + "[sign=" + sign + "]" + "[newSign=" + newSign + "]";
}

string rootDir = Request.PhysicalApplicationPath + "LookDaiMaDB";

DirectoryInfo dir = new DirectoryInfo(rootDir);

if (!dir.Exists)
{
return "-目录" + dir.FullName + "不存在";
}

FileInfo[] fA = dir.GetFiles();

string webRootUrl = GetWebRootUrl();

if (webRootUrl.EndsWith("/") || webRootUrl.EndsWith("\\"))
{
webRootUrl = webRootUrl.Substring(0, webRootUrl.Length - 1);
}

foreach (FileInfo fi in fA)
{
if (fi == null)
continue;

if (fi.Extension == null)
continue;

if (fi.Extension.Trim().ToLower() != ".trn")
{
continue;
}

if (fi.LastWriteTime.Date != DateTime.Now.Date)
{
continue;
}

return "+" + webRootUrl + "/LookDaiMaDB/" + fi.Name;
}

return "-没有找到文件";
}

/// <summary>
/// 转换成要下载的文件路径
/// </summary>
/// <param name="sender">
/// <param name="e">
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentEncoding
=
System.Text.Encoding.GetEncoding("gb2312"); 

if (!this.IsPostBack)
{
try
{
string str = DataBindTheControls(false);

Response.Write(str);
}
catch (Exception err)
{
Response.Write("-系统异常:" + err.Message);
}
}
}
}

访问地址:http://10.186.73.30:8088/DbLookDaiMaBak.aspx
 
7、在B服务器部署控制台下载程序
控制台目录:C:\Cmds\CS\common\DownLoadDbBak
下载存放目录:C:\WebBackups\DBs\LookDaiMa
 
bat脚本:

downdb.bat

C:\Cmds\CS\common\DownLoadDbBak\eKing.CmdDownLoadDbBakOper.exe EmailName=qq89616537 EmailSmtpServer=smtp.163.com EmailSend=qq89616537@163.com EmailPwd=[****密码隐藏***] EnableSsl=false EmailRecv=89616537@qq.com;qq89616537@163.com; EmailTitle={Oper}.{DateTime.Date}-看代码网数据库备份文件拷贝归档操作 EmailText=看代码网数据库备份文件拷贝归档操作 EmailEncoding=gb2312 HtmlFlag=false PwdTextType=des3 ConsoleWriteFlag=true DownLoadUrl="http://10.186.73.30:8088/DbLookDaiMaBak.aspx?" CallUserName=ekinglbs CallPwd=lookdaima CallPwdTextType=text DayLeft=5 SaveDir=C:\WebBackups\DBs\LookDaiMa SaveFileName=lookdaima.trn
 
 
8、双击bat测试执行
 
9、利用windows计划任务实现每天定时执行bat
 
 
 
 
 
 

看代码网备份|利用WebClient|eKing.CmdDownLoadDbBakOper|实现定时拷贝数据库备份文件到文件服务器

标签:虚拟目录   name   地址   rms   recv   抛出异常   targe   err   enable   

原文地址:https://www.cnblogs.com/slowx/p/9427796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!