C#實現文件增量備份

最近將客戶的一個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) + "個文件");
            }
        }
    }
相關文章
相關標籤/搜索