62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Net;
public partial class BackupDB_Default : System.Web.UI.Page
{
#region Define Variables
bool isSuccess = false;
string ftpServerIP = "101.101.10.104";//Remote Server
int ftpPort = 21;
string ftpUserID = "T";
string ftpPassword = "abcdef11";
string pBackupDBAndCompressFilesPath = @"D:\DataCenter";
string pBackupDBFileName = "Iridian2#168EW@370$.bak";
string pCompressFileName = "Iridian2#168EW@370$.rar";
#endregion
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Directory.Exists(pBackupDBAndCompressFilesPath))
BackupDatabase();
}
}
#endregion
#region BackupDatabase
private void BackupDatabase()
{
//SQLDMO.dll is a COM Object ,Microsoft SQl Server provider
SQLDMO.Backup pBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer pSqlServer = new SQLDMO.SQLServerClass();
try
{
pSqlServer.LoginSecure = false;
pSqlServer.Connect("110.110.10.5", "sa", "1111");//Server IP,Database Login UserName and Password
pBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
pBackup.Database = "Iridian2";
pBackup.Files = pBackupDBAndCompressFilesPath + "\\" + pBackupDBFileName;
pBackup.BackupSetName = "Iridian2";
pBackup.BackupSetDescription = "Database Backup";
pBackup.Initialize = true;
pBackup.SQLBackup(pSqlServer);
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
Response.Write(ex.Message);
}
finally
{
pSqlServer.DisConnect();
if (isSuccess)
{
bool isCompressSuccess = false;
if (File.Exists(pBackupDBAndCompressFilesPath + "\\" + pBackupDBFileName))
isCompressSuccess = CompressDBBackupFile(pBackupDBAndCompressFilesPath + "\\" + pBackupDBFileName, pBackupDBAndCompressFilesPath, pCompressFileName);
if (isCompressSuccess)
SendZipFileToAnotherComputer(pCompressFileName);
}
}
}
#endregion
#region CompressDBBackupFile
private bool CompressDBBackupFile(string patch, string rarPatch, string rarName)
{
bool excuteSuccess = false;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process = new Process();
try
{
the_Info = " a " + rarName + " " + patch + " -r -o+ -m5 -pEW#123@";//Set Password:EW#123@
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";//Winrar install path
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = rarPatch;
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
excuteSuccess = true;
}
catch (Exception ex)
{
excuteSuccess = false;
Response.Write(ex.Message);
}
finally
{
the_Process.WaitForExit();
if (the_Process != null)
{
the_Process.Close();
the_Process = null;
}
}
return excuteSuccess;
}
#endregion
#region SendZipFileToAnotherComputer
private void SendZipFileToAnotherComputer(string filename)
{
FileInfo fileInf = new FileInfo(pBackupDBAndCompressFilesPath + "\\" + filename);
string uri = "ftp://" + ftpServerIP + ":" + ftpPort.ToString() + "/" + fileInf.Name;//FTP Format:ftp://user:password@ftpserver:port/url-path
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int contentLen;
int buffLength = 2048;//The buffer size is set to 2kb
byte[] buff = new byte[buffLength];
FileStream fs = null;
Stream strm = null;
try
{
fs = fileInf.OpenRead();
strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
}
catch (Exception ex)
{
Response.Write("Upload Error:" + ex.Message);
}
finally
{
if (strm != null)
{
strm.Close();
strm = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
reqFTP.Abort();
}
}
#endregion
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="BackupDB_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Backup Database</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>