服务器文件自动备份工具

网站经常需要定期备份文件,天天折腾累死人 ,索性写了个自动备份 的工具,让它运行在服务器上,每天凌晨自动将需要备份的数据打包成压缩文件并传到另外的服务器。

1、定时执行任务,用到开源框架Quartz.net 使用方法: 引用Quartz.dll

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();scheduler.Start();IJobDetail job = JobBuilder.Create<HelloJob>().WithIdentity(“job1”, “group1”).Build();var tr = TriggerBuilder.Create().WithCronSchedule(runtime).Build();scheduler.ScheduleJob(job, tr);

其中 runtime 变量是 定义执行的时间:

<add key=”runtime” value=”0 3 19 * * ?”/>

说明: 0 3 19 分别为 秒,分,时

其中的HelloJob 是需要实现IJob接口的类,在其中写备份文件的一系列方法。

class HelloJob : IJob{public void Execute(IJobExecutionContext context){

//…这里写实现的方法

}

}

2、实现文件的备份 整个过程包括:文件复制、压缩文件、上传压缩文件到制定服务器、发送通知邮件

直接复制文件夹:

static bool DirectoryCopy(string sourceDir, string targetDir){bool back = false;if (Directory.Exists(sourceDir) && Directory.Exists(targetDir)){string sourceFolderName = sourceDir.Replace(Directory.GetParent(sourceDir).ToString(), “”).Replace(Path.DirectorySeparatorChar.ToString(), “”);if (sourceDir != targetDir + sourceFolderName){//要复制到的路径string tagetPath = targetDir + Path.DirectorySeparatorChar.ToString() + sourceFolderName;if (Directory.Exists(tagetPath)){Directory.Delete(tagetPath, true);}Directory.CreateDirectory(tagetPath);//复制文件string[] files = Directory.GetFiles(sourceDir);for (int i = 0; i < files.Length; i++){File.Copy(files[i], tagetPath + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(files[i]));}//复制目录string[] dires = Directory.GetDirectories(sourceDir);for (int j = 0; j < dires.Length; j++){DirectoryCopy(dires[j], tagetPath);}back = true;}}return back;}

对复制产生的新文件目录进行打包(不能对原使用中的文件进行打包操作,因为被占用的资源在打包时会出错)

打包技术采用 SharpZipLib组件

打包压缩文件方法类: 引用 ICSharpCode.SharpZipLib.dll

ZipFloClass 的摘要说明ZipFloClass{(string strFile, string strZip){if (strFile[strFile.Length – 1] != Path.DirectorySeparatorChar)strFile += Path.DirectorySeparatorChar;ZipOutputStream s = new ZipOutputStream(File.Create(strZip));s.SetLevel(6); // 0 – store only to 9 – means best compressionzip(strFile, s, strFile);s.Finish();s.Close();}(string strFile, ZipOutputStream s, string staticFile){if (strFile[strFile.Length – 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;Crc32 crc = new Crc32();string[] filenames = Directory.GetFileSystemEntries(strFile);foreach (string file in filenames){if (Directory.Exists(file)){zip(file, s, staticFile);}else // 否则直接压缩文件{//打开压缩文件FileStream fs = File.OpenRead(file);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);string tempfile = file.Substring(staticFile.LastIndexOf(“\\”) + 1);ZipEntry entry = new ZipEntry(tempfile);entry.DateTime = DateTime.Now;entry.Size = fs.Length;fs.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;s.PutNextEntry(entry);s.Write(buffer, 0, buffer.Length);}}}}

外层调用上面类里面方法进行压缩文件

static bool yasuoFile(string yasuoPath, string savePath, string name){bool back = false;string[] FileProperties = new string[2];FileProperties[0] = yasuoPath;//待压缩文件目录FileProperties[1] = savePath + name+ “.zip”; //压缩后的目标文件ZipFloClass Zc = new ZipFloClass();Zc.ZipFile(FileProperties[0], FileProperties[1]);back = true;return back;}

上传文件

上传文件客户端:

上传文件 到 WebService接口static string uploadfile(string path){try {webservice.WebServiceSoapClient upload = new webservice.WebServiceSoapClient();byte[] b = GetBytesByPath(path);string name = Path.GetFileName(path);string back = upload.SaveFile(b, name);return back;}catch(Exception ex){return “上传数据失败!”+ex.ToString();}}三亚呀——赴一个蓝天碧海。

服务器文件自动备份工具

相关文章:

你感兴趣的文章:

标签云: