最近将客户的一个ASP网站部署到了公司的机房云服务器上,该ASP网站的文件总容量已有将近4GB。数据库
虽然如今硬盘容量很大,但天天一次完整备份的话,那占用的硬盘空间会急剧上升,考虑一个更优的备份方案就是天天一次增量备份,每个月一次完整备份。服务器
因而就有了本身动手写一个增量备份程序的念头,虽然网上可能已经有了不少增量备份的程序,但为了更加灵活和可以随时适应项目的个性化要求,就决定使用winform写一个备份程序。网站
代码实现思路:spa
一、首先对须要备份的文件夹下的全部文件信息进行初始化,将全部文件的完整路径及文件最后修改时间存储到数据库里(这里我采用的是SQLite数据库)。code
二、备份时遍历文件夹下全部文件,而后经过文件的完整路径对数据库进行查询,若是查询不到数据,说明是新增的文件,这时就将文件复制到指定的备份目录下,而且将文件信息插入数据库;orm
若是查询到数据,则对文件最后修改时间进行比较,若是时间不一致,就说明文件有改动,这时就将文件复制到指定的备份目录下,而且将文件信息数据进行更新。blog
核心代码以下:部署
class Bakuper { private static string srcPath = ConfigurationManager.AppSettings["srcPath"]; private static string destPath = ConfigurationManager.AppSettings["destPath"]; private static int fileCount; private static int copyCount; private static SQLiteConnection conn; public static int backupFile() { fileCount = 0; copyCount = 0; conn = SQLHelper.getSQLiteConnection(); DirectoryInfo theFolder = new DirectoryInfo(srcPath); readFolderList(theFolder); readFileList(theFolder); Console.WriteLine("共备份了" + copyCount+"个文件"); return copyCount; } static void readFolderList(DirectoryInfo folder) { DirectoryInfo[] dirInfo = folder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { readFolderList(NextFolder); readFileList(NextFolder); } } static void readFileList(DirectoryInfo folder) { FileInfo[] fileInfo = folder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { SQLiteCommand cmd = new SQLiteCommand("select lastWriteTime from " + SQLHelper.TB_NAME + " where fullPath='" + NextFile.FullName + "'", conn); object obj = cmd.ExecuteScalar(); if (obj == null)//若是是新增的文件 { String fullname = folder.FullName; string newpath = fullname.Replace(srcPath, destPath + "\\" + DateTime.Now.ToString("yyyyMMdd")); DirectoryInfo newFolder = new DirectoryInfo(newpath); if (!newFolder.Exists) { newFolder.Create(); } NextFile.CopyTo(newpath + "\\" + NextFile.Name, true); SQLiteCommand cmdInsert = new SQLiteCommand(conn);//实例化SQL命令 cmdInsert.CommandText = "insert into " + SQLHelper.TB_NAME + " values(@fullPath, @lastWriteTime)";//设置带参SQL语句 cmdInsert.Parameters.AddRange(new[] {//添加参数 new SQLiteParameter("@fullPath", NextFile.FullName), new SQLiteParameter("@lastWriteTime", NextFile.LastWriteTime) }); cmdInsert.ExecuteNonQuery(); copyCount++; } else { DateTime lastWriteTime = DateTime.Parse(obj.ToString()); if (!DateTime.Parse(NextFile.LastWriteTime.ToString()).Equals(lastWriteTime)) { String fullname = folder.FullName; string newpath = fullname.Replace(srcPath, destPath + "\\" + DateTime.Now.ToString("yyyyMMdd")); DirectoryInfo newFolder = new DirectoryInfo(newpath); if (!newFolder.Exists) { newFolder.Create(); } NextFile.CopyTo(newpath + "\\" + NextFile.Name, true); SQLiteCommand cmdUpdate = new SQLiteCommand(conn);//实例化SQL命令 cmdUpdate.CommandText = "update " + SQLHelper.TB_NAME + " set lastWriteTime=@lastWriteTime where fullPath=@fullPath"; cmdUpdate.Parameters.AddRange(new[] {//添加参数 new SQLiteParameter("@fullPath", NextFile.FullName), new SQLiteParameter("@lastWriteTime", NextFile.LastWriteTime) }); cmdUpdate.ExecuteNonQuery(); copyCount++; } } Console.WriteLine("已遍历第" + (++fileCount) + "个文件"); } } }