简单备份程序

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


class Program
{
static void Main(string[] args)
{
Console.WriteLine("----------------------------------------------------");
Console.WriteLine("删除过期备份") ;
FindAllFileAndDel(Action.Text.gettextbyline2(1));
Console.WriteLine("----------------------------------------------------");
Console.WriteLine("连接数据库实例" + Action.Text.gettextbyline(1));
string strLine = "";
int i = 0;
try
{
StreamReader sr = new StreamReader("DB.do", Encoding.GetEncoding("GB2312"));
strLine = sr.ReadLine();
while (strLine != null)
{

strLine = sr.ReadLine();
i++;

DbBackup(i);
}

sr.Dispose();
sr.Close();
}
catch
{
}
Console.Read();
}

public static System.Diagnostics.Process p = new System.Diagnostics.Process();
public static void z7(string cmdstr)
{
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "Action.exe"; //需要启动的程序名
//p.StartInfo.Arguments = @"a-t7zd:\SMMM_2011103111620.7zd:\SMMM_2011103111620.bak-mx=9"; //启动参数
p.StartInfo.Arguments = @"a-t7z" + cmdstr + " -mx=9"; //启动参数
p.Start(); //启动
p.WaitForExit();
}
///
/// 数据库备份
///
public static void DbBackup(int i)
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(Action.Text.gettextbyline(1) ,Action.Text.gettextbyline(2), Action.Text.gettextbyline(3));
// Console.WriteLine("连接数据库实例" + Action.Text.gettextbyline(1));
Console.WriteLine("----------------------------------------------------");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = Action.Text.gettextbyline1(i);

oBackup.Files = Action.Text.gettextbyline2(1) + Action.Text.gettextbyline1(i) +"_"+ DateTime.Now.ToString().Replace(":","").Replace(" ","").Replace("-","") + ".bak";
//oBackup.BackupSetName = "SMMM";
//oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
Console.WriteLine("备份数据库" + Action.Text.gettextbyline1(i) );
DateTime dt1 = DateTime.Now;
Console.WriteLine("开始时间" + dt1);
Console.WriteLine("备份路径" + oBackup.Files);
【简单备份程序】Console.WriteLine("----------------------------------------------------");
Console.WriteLine("正在执行备份,请稍后......" );
oBackup.SQLBackup(oSQLServer);
Console.WriteLine("数据库" + Action.Text.gettextbyline1(i) + "备份完成");
DateTime dt2 = DateTime.Now;
Console.WriteLine("结束时间" + dt2);
TimeSpan ts = dt2 - dt1;
//Console.WriteLine("执行时间:" + ts.Minutes + "分钟");
Console.WriteLine("执行时间:" + ts.Hours + "小时" + ts.Minutes + "分钟" + ts.Seconds + "秒");

FileInfo fileInfo = new FileInfo(oBackup.Files);
Console.WriteLine("备份大小" + fileInfo.Length / (1024.00 * 1024.00) + "MB");
Console.WriteLine("----------------------------------------------------");
Console.WriteLine("准备文件压缩");
DateTime dt3 = DateTime.Now;
Console.WriteLine("开始时间" + dt3);
Console.WriteLine("压缩文件" + oBackup.Files);

Console.WriteLine("正在执行压缩,请稍后......");
z7(oBackup.Files.Replace("bak", "7z") + " " + oBackup.Files);
DateTime dt4 = new DateTime();
if (p.ExitCode == 0)
{
Console.WriteLine("压缩完成" + oBackup.Files.Replace("bak","7z"));
dt4= DateTime.Now;
Console.WriteLine("结束时间" + dt4);
TimeSpan ts1 = dt4 - dt3;
//Console.WriteLine("执行时间:" + ts.Minutes + "分钟");
Console.WriteLine("执行时间:" +ts1.Hours+"小时"+ts1.Minutes+"分钟"+ ts1.Seconds + "秒");
Console.WriteLine("原文件大小" + fileInfo.Length / (1024.00 * 1024.00) + "MB");
FileInfo fileInfo1 = new FileInfo(oBackup.Files.Replace("bak", "7z"));
Console.WriteLine("压缩后大小" + fileInfo1.Length / (1024.00 * 1024.00) + "MB");
Console.WriteLine("压缩率" + fileInfo1.Length * 1.00 / (fileInfo.Length * 1.00));
Console.WriteLine("----------------------------------------------------");
Console.WriteLine("删除文件" + oBackup.Files);
File.Delete(oBackup.Files);
Console.WriteLine("删除完成");
}



//Console.Read();
}
//catch
// {
//throw;
// }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
oSQLServer.DisConnect();
}
}
public static void FindAllFileAndDel(string path)//统计(删除)过期文件
{

DirectoryInfo[] ChildDirectory; //子目录集
FileInfo[] NewFileInfo; //当前所有文件
DirectoryInfo FatherDirectory = new DirectoryInfo(path); //当前目录
ChildDirectory = FatherDirectory.GetDirectories("*.*"); //得到子目录集
NewFileInfo = FatherDirectory.GetFiles(); //得到文件集,可以进行操作
foreach (FileInfo fileinfo in FatherDirectory.GetFiles("*.7z") )
{
DateTime dt = DateTime.Now.Date;
if (fileinfo.LastWriteTime.AddDays(Convert.ToInt32( Action.Text.gettextbyline3(1))) < dt)//假如最后修改时间是90天之前时
{
fileinfo.Delete();
Console.WriteLine(fileinfo.Name +"已删除");

}
}

foreach (FileInfo fileinfo in FatherDirectory.GetFiles("*.bak"))
{
DateTime dt = DateTime.Now.Date;
if (fileinfo.LastWriteTime.AddDays(Convert.ToInt32(Action.Text.gettextbyline3(1))) < dt)//假如最后修改时间是90天之前时
{
fileinfo.Delete();
Console.WriteLine(fileinfo.Name + "已删除");
}
}
//foreach (DirectoryInfo dirInfo in ChildDirectory)
//{
//FindAllFileAndDel(dirInfo.FullName);
//}
}

}
简单备份程序
文章图片




Text.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace Action
{
class Text
{
public static string gettextbyline(int line)
{
StreamReader sr = new StreamReader("Action.do", Encoding.GetEncoding("GB2312"));
String strline;
TextBox textBox1 = new TextBox(); while ((strline = sr.ReadLine()) != null)
{
textBox1.Text += strline.ToString() + "\r\n";
}
return textBox1.Lines[line - 1].ToString();
}
public static string gettextbyline1(int line)
{
StreamReader sr = new StreamReader("DB.do", Encoding.GetEncoding("GB2312"));
String strline;
TextBox textBox1 = new TextBox(); while ((strline = sr.ReadLine()) != null)
{
textBox1.Text += strline.ToString() + "\r\n";
}
return textBox1.Lines[line - 1].ToString();
}
public static string gettextbyline2(int line)
{
StreamReader sr = new StreamReader("BackUp.do", Encoding.GetEncoding("GB2312"));
String strline;
TextBox textBox1 = new TextBox(); while ((strline = sr.ReadLine()) != null)
{
textBox1.Text += strline.ToString() + "\r\n";
}
return textBox1.Lines[line - 1].ToString();
}
public static string gettextbyline3(int line)
{
StreamReader sr = new StreamReader("Delete.do", Encoding.GetEncoding("GB2312"));
String strline;
TextBox textBox1 = new TextBox(); while ((strline = sr.ReadLine()) != null)
{
textBox1.Text += strline.ToString() + "\r\n";
}
return textBox1.Lines[line - 1].ToString();
}
}
}



简单备份程序
文章图片



下载/Files/qanholas/备份程序.7z

    推荐阅读