C#实现文件增量备份

最近将客户的一个ASP网站部署到了公司的机房云服务器上,该ASP网站的文件总容量已有将近4GB。

虽然现在硬盘容量很大,但每天一次完整备份的话,那占用的硬盘空间会急剧上升,考虑一个更优的备份方案就是每天一次增量备份,每月一次完整备份。

于是就有了自己动手写一个增量备份程序的念头,虽然网上可能已经有了很多增量备份的程序,但为了更加灵活和能够随时适应项目的个性化要求,就决定使用winform写一个备份程序。

代码实现思路:

1、首先对需要备份的文件夹下的所有文件信息进行初始化,将所有文件的完整路径及文件最后修改时间存储到数据库里(这里我采用的是SQLite数据库)。

2、备份时遍历文件夹下所有文件,然后通过文件的完整路径对数据库进行查询,如果查询不到数据,说明是新增的文件,这时就将文件复制到指定的备份目录下,并且将文件信息插入数据库;

    如果查询到数据,则对文件最后修改时间进行比较,如果时间不一致,就说明文件有改动,这时就将文件复制到指定的备份目录下,并且将文件信息数据进行更新。

核心代码如下:

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) + "个文件");
            }
        }
    }
原文地址:https://www.cnblogs.com/liuxin-listenx/p/5714133.html