Exemplo n.º 1
0
        static private void backupFolder(object obj)//DirectoryInfo folder, string parentFolderId, StreamWriter sw)
        {
            DirectoryInfo folder         = ((BackupFolderParameter)obj).folder;
            String        parentFolderId = ((BackupFolderParameter)obj).parendFolderId;
            StreamWriter  sw             = ((BackupFolderParameter)obj).sw;
            string        histroyId      = ((BackupFolderParameter)obj).histroyId;

            try
            {
                foreach (DirectoryInfo excludeFolder in excludeFolderList)
                {
                    if (excludeFolder.FullName.Equals(folder.FullName))
                    {
                        return;
                    }
                }
                if (isIgnoreFolder(folder))
                {
                    return;
                }

                FileInfo[] files    = folder.GetFiles();
                string     folderId = sql.GetFolderPathId(folder.FullName, parentFolderId);
                foreach (FileInfo fileInfo in files)
                {
                    try
                    {
                        if (fileInfo.Length > 0 && !isIgnore(fileInfo))
                        {
                            string md5 = quick_hash(fileInfo, sw);
                            if (md5.Length > 5)
                            {
                                if (!sql.CheckExist(md5))
                                {
                                    //Console.WriteLine(String.Format("BackupFile:{0} {1}", md5, fileInfo.FullName));
                                    Backup(fileInfo, new DirectoryInfo(backupTargetDir), md5, sw);
                                    sql.AddMd5(md5);
                                }

                                sql.Insert(fileInfo, md5, folderId, histroyId);
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine(err.Message);
                        Console.WriteLine(err.StackTrace);
                        lock (logWriter)
                        {
                            sw.WriteLine(DateTime.Now.ToString());
                            sw.WriteLine("File:" + fileInfo.FullName);
                            sw.WriteLine(err.Message);
                            sw.WriteLine(err.StackTrace);
                            sw.Flush();
                        }
                    }
                }

                DirectoryInfo[] folders = folder.GetDirectories();

                foreach (DirectoryInfo subfolder in folders)
                {
                    //Thread thread = new Thread(new ParameterizedThreadStart(backupFolder));
                    BackupFolderParameter p = new BackupFolderParameter();
                    p.folder         = subfolder;
                    p.parendFolderId = folderId;

                    p.sw        = sw;
                    p.histroyId = histroyId;
                    lock (countObj) mThreadCount++;
                    //thread.Start(p);
                    //Console.WriteLine(string.Format("Thread:{0}", p.folder.FullName));
                    ThreadPool.QueueUserWorkItem(new WaitCallback(backupFolder), p);
                    //backupFolder(p);
                }
            }
            catch (Exception err)
            {
                //Console.WriteLine(err.StackTrace);
                lock (logWriter)
                {
                    sw.WriteLine("Folder: " + folder.FullName);
                    sw.Write(err.Message);
                    sw.WriteLine(err.StackTrace);

                    sw.Flush();
                }
            }

            lock (countObj) mThreadCount--;
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            FileInfo curFile = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
            string   defFile = string.Format("{0}\\backup.def", curFile.DirectoryName);

            readDef(defFile);

            sql = SqlClass.GetInstance();

            ThreadPool.SetMaxThreads(20, 20);
            string log_directory = string.Format("{0}\\log\\{1}", curFile.DirectoryName, DateTime.Now.ToString("yyyy-M-d"));

            if (!Directory.Exists(log_directory))
            {
                Directory.CreateDirectory(log_directory);
            }

            StreamWriter log = new StreamWriter(string.Format("{0}\\{1}.txt", log_directory, DateTime.Now.ToString("yyyy-M-d HH-mm-ss")));

            try
            {
                sql.Init();
                foreach (DirectoryInfo folder in backupFolderList)
                {
                    StreamWriter sw = new StreamWriter(string.Format("{0}\\{1}_{2}.txt", log_directory, folder.Name, DateTime.Now.ToString("yyyy-M-d HH-mm-ss")));
                    DateTime     dt = DateTime.Now;
                    if (folder.Exists)
                    {
                        BackupFolderParameter p = new BackupFolderParameter();
                        p.folder         = folder;
                        p.parendFolderId = "-1";
                        p.sw             = sw;
                        p.histroyId      = sql.GetNewBackupHistroyId(sql.GetFolderPathId(p.folder.FullName, p.parendFolderId));
                        //Thread thread = new Thread(new ParameterizedThreadStart(backupFolder));
                        lock (countObj) mThreadCount++;
                        //thread.Start(p);
                        //Console.WriteLine(string.Format("Thread:{0}",p.folder.FullName));
                        ThreadPool.QueueUserWorkItem(new WaitCallback(backupFolder), p);
                        //backupFolder(folder, "-1", sw);
                    }
                    else
                    {
                        lock (logWriter)
                        {
                            sw.WriteLine(string.Format("{0} 路径错误\n", folder.FullName));
                        }
                    }
                    //sw.WriteLine("Hash 文件夹 {0}", folder.FullName);
                    //DateTime dt1 = DateTime.Now;
                    //TimeSpan ts = dt1 - dt;
                    //sw.WriteLine("总用时:{0} 小时 {1} 分钟 {2} 秒", ts.Hours, ts.Minutes, ts.Seconds);
                    //sw.Close();
                }
                //sql.save_cache();
            }
            catch (Exception e)
            {
                log.WriteLine(e.Message);
                log.WriteLine(e.StackTrace);
            }
            //sql.save_cache();

            while (true)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Thread Count:" + mThreadCount.ToString());
                if (mThreadCount <= 0)
                {
                    sql.Save_cache();
                    break;
                }
            }
            Console.WriteLine("BackupFinished" + mThreadCount.ToString());
            log.Close();
        }