1. 程式人生 > >看代碼網備份|利用WebClient|eKing.CmdDownLoadDbBakOper|實現定時拷貝數據庫備份文件到文件服務器

看代碼網備份|利用WebClient|eKing.CmdDownLoadDbBakOper|實現定時拷貝數據庫備份文件到文件服務器

emd 返回 隱藏 res -i ogr try 計劃任務 -a

摘要:

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、備份成功郵件定時提醒 原文鏈接: http://www.lookdaima.com/WebForms/WebPages/Blanks/Pm/Docs/DocItemDetail.aspx?id=f51e119e-68d4-49f6-8480-0c967d84902e&EmDocDetailShowTypeV=2 備份策略:
1、在A服務器每天定時備份數據文件到D:\DataBase\AutoBackAll目錄的LookDaiMaDB下 2、在A服務器的D:\DataBase\AutoBackAll目錄下架設一個IIS網站,端口8088

技術分享圖片

架設網站

3、在騰訊雲服務器上設置安全策略,只能B服務器才能訪問8088端口

技術分享圖片

騰訊雲設置安全策略

騰訊雲設置安全防火墻規則的方法指引 騰訊雲怎麽設置防火墻安全 4、(重復安全設置)在A服務器防火墻設置只有B服務器才能訪問

技術分享圖片

防火墻設置

SqlServer2008數據庫被人掃sa密碼攻擊的發現和通過windows防火墻處理知識分享 5、在IIS設置trn文件能下載,見文章: 讓IIS7.0.0.0支持 .iso .7z .torrent .apk等文件下載的設置方法 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 DownLoadDbBak.zip bat腳本:

downdb.bat

C:\Cmds\CS\common\DownLoadDbBak\eKing.CmdDownLoadDbBakOper.exe EmailName=qq89616537 EmailSmtpServer=smtp.163.com [email protected] EmailPwd=[****密碼隱藏***] EnableSsl=false [email protected];[email protected]; 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 利用WebClient|eKing.CmdDownLoadDbBakOper|實現定時拷貝數據庫備份文件到文件服務器 8、雙擊bat測試執行 9、利用windows計劃任務實現每天定時執行bat 通過windows計劃任務和bat腳本實現定時獲取表空間大小的操作指引攻略

看代碼網備份|利用WebClient|eKing.CmdDownLoadDbBakOper|實現定時拷貝數據庫備份文件到文件服務器