FileBackupService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using Oreo.FileMan.DatabaseEngine;
  2. using Oreo.FileMan.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Y.Utils.DataUtils.Collections;
  10. using Y.Utils.DataUtils.DateTimeUtils;
  11. using Y.Utils.IOUtils.FileManUtils;
  12. using Y.Utils.IOUtils.FileUtils;
  13. using Y.Utils.IOUtils.PathUtils;
  14. namespace Oreo.FileMan.Services
  15. {
  16. public class FileBackupService
  17. {
  18. public FileWatcher Watcher = new FileWatcher(null);
  19. public string FileManBackup = @"G:\FileManBackup\";
  20. public List<BackupPaths> Paths = new List<BackupPaths>();
  21. List<string> BackupFiles = new List<string>();
  22. int BACK_UP_INTERVAL = 5 * 1000;
  23. int BACK_UP_COUNT = 5;
  24. bool IsStart = false;
  25. public void Start()
  26. {
  27. if (!IsStart)
  28. {
  29. IsStart = true;
  30. Watcher.eventHandler += WatcherChangedEvent;
  31. Watcher.Start();//启动文件变动监听
  32. Task.Factory.StartNew(() => { ReadBackupPaths(); });//读取备份文件夹列表
  33. Task.Factory.StartNew(() => { BackupFileTask(); });//开始定时备份文件任务
  34. }
  35. }
  36. public void Stop()
  37. {
  38. if (IsStart)
  39. {
  40. IsStart = false;
  41. }
  42. }
  43. private void WatcherChangedEvent(object sender, FileWatcherEventArgs e)
  44. {
  45. if (Paths.Any(x => e.FullPath.Contains(x.Path)))
  46. {
  47. //变动的是文件且文件存在
  48. if (FileTool.IsFile(e.FullPath))
  49. {
  50. //添加到备份列表
  51. if (!BackupFiles.Contains(e.FullPath)) BackupFiles.Add(e.FullPath);
  52. //UIDgvFileAdd(e.Name, e.FullPath, e.ChangeType.ToString());
  53. }
  54. }
  55. }
  56. private void BackupFileTask()
  57. {
  58. while (IsStart)
  59. {
  60. if (ListTool.HasElements(BackupFiles))
  61. {
  62. //获取要备份的文件列表并复制样本
  63. List<string> temp;
  64. lock (BackupFiles)
  65. {
  66. temp = BackupFiles;
  67. BackupFiles = new List<string>();
  68. }
  69. foreach (var t in temp)
  70. {
  71. if (File.Exists(t))
  72. {
  73. string filepath = DirTool.GetFilePath(t);
  74. BackupPaths path = Paths.FirstOrDefault(x => filepath.Contains(x.Path));
  75. if (path != null)
  76. {
  77. string pathname = path.Path;
  78. string pathalias = path.Alias;
  79. string pathfile = t.Substring(pathname.Length, t.Length - pathname.Length);
  80. string fileext = DateTimeConvert.CompactString(DateTime.Now);
  81. string fullpath = DirTool.Combine(FileManBackup, pathalias, pathfile + "." + fileext);
  82. //删除冗余
  83. DeleteExcess(t);
  84. //备份文件
  85. BackupFile(t, fullpath);
  86. }
  87. }
  88. }
  89. }
  90. Thread.Sleep(BACK_UP_INTERVAL);
  91. }
  92. }
  93. /// <summary>
  94. /// 读取备份文件夹列表
  95. /// </summary>
  96. private void ReadBackupPaths()
  97. {
  98. //读取要备份的文件路径列表
  99. using (var db = new Muse())
  100. {
  101. Paths = db.GetAll<BackupPaths>(null, false).ToList();
  102. if (ListTool.HasElements(Paths))
  103. {
  104. foreach (var p in Paths)
  105. {
  106. Watcher.AddPath(p.Path);
  107. }
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// 删除超过备份最大次数的项
  113. /// </summary>
  114. private void DeleteExcess(string path)
  115. {
  116. using (var db = new Muse())
  117. {
  118. int count = db.Do<BackupFiles>().Count(x => x.FullPath == path);
  119. if (count >= BACK_UP_COUNT)
  120. {
  121. var fs = db.Gets<BackupFiles>(x => x.FullPath == path, null).OrderBy(x => x.Id).ToList();
  122. if (ListTool.HasElements(fs))
  123. {
  124. for (int i = 0; i <= count - BACK_UP_COUNT; i++)
  125. {
  126. try
  127. {
  128. File.Delete(fs[i].BackupFullPath);
  129. db.Del(fs[i], true);
  130. }
  131. catch (Exception e) { }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 备份文件
  139. /// </summary>
  140. /// <param name="path"></param>
  141. /// <param name="newpath"></param>
  142. private void BackupFile(string path, string newpath)
  143. {
  144. using (var db = new Muse())
  145. {
  146. try
  147. {
  148. if (DirTool.Create(DirTool.GetFilePath(newpath)))
  149. {
  150. File.Copy(path, newpath, true);
  151. db.Add(new BackupFiles()
  152. {
  153. FullPath = path,
  154. BackupFullPath = newpath,
  155. Size = FileTool.Size(path),
  156. BackupTime = DateTimeConvert.StandardString(DateTime.Now),
  157. });
  158. }
  159. }
  160. catch (Exception e) { }
  161. }
  162. }
  163. }
  164. }