FileBackupService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(() =>
  33. {
  34. ReadBackupPaths();//读取备份文件夹列表
  35. //常规检查备份
  36. BackupFileTask();//开始定时备份任务
  37. });
  38. }
  39. }
  40. public void Stop()
  41. {
  42. if (IsStart)
  43. {
  44. IsStart = false;
  45. }
  46. }
  47. private void WatcherChangedEvent(object sender, FileWatcherEventArgs e)
  48. {
  49. if (Paths.Any(x => e.FullPath.Contains(x.Path)))
  50. {
  51. //变动的是文件且文件存在
  52. if (FileTool.IsFile(e.FullPath))
  53. {
  54. //添加到备份列表
  55. if (!BackupFiles.Contains(e.FullPath)) BackupFiles.Add(e.FullPath);
  56. //UIDgvFileAdd(e.Name, e.FullPath, e.ChangeType.ToString());
  57. }
  58. }
  59. }
  60. private void BackupFileTask()
  61. {
  62. while (IsStart)
  63. {
  64. if (ListTool.HasElements(BackupFiles))
  65. {
  66. //获取要备份的文件列表并复制样本
  67. List<string> temp;
  68. lock (BackupFiles)
  69. {
  70. temp = BackupFiles;
  71. BackupFiles = new List<string>();
  72. }
  73. foreach (var t in temp)
  74. {
  75. if (File.Exists(t))
  76. {
  77. string filepath = DirTool.GetFilePath(t);
  78. BackupPaths path = Paths.FirstOrDefault(x => filepath.Contains(x.Path));
  79. if (path != null)
  80. {
  81. string pathname = path.Path;
  82. string pathalias = path.Alias;
  83. string pathfile = t.Substring(pathname.Length, t.Length - pathname.Length);
  84. string fileext = DateTimeConvert.CompactString(DateTime.Now);
  85. string fullpath = DirTool.Combine(FileManBackup, pathalias, pathfile + "." + fileext);
  86. //删除冗余
  87. DeleteExcess(t);
  88. //备份文件
  89. BackupFile(t, fullpath);
  90. }
  91. }
  92. }
  93. }
  94. Thread.Sleep(BACK_UP_INTERVAL);
  95. }
  96. }
  97. /// <summary>
  98. /// 读取备份文件夹列表
  99. /// </summary>
  100. private void ReadBackupPaths()
  101. {
  102. //读取要备份的文件路径列表
  103. using (var db = new Muse())
  104. {
  105. Paths = db.GetAll<BackupPaths>(null, false).ToList();
  106. if (ListTool.HasElements(Paths))
  107. {
  108. foreach (var p in Paths)
  109. {
  110. Watcher.AddPath(p.Path);
  111. }
  112. }
  113. }
  114. }
  115. public void DefaultBackupFileTask()
  116. {
  117. if (ListTool.HasElements(Paths))
  118. {
  119. foreach (var p in Paths)
  120. {
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// 删除超过备份最大次数的项
  126. /// </summary>
  127. private void DeleteExcess(string path)
  128. {
  129. using (var db = new Muse())
  130. {
  131. int count = db.Do<BackupFiles>().Count(x => x.FullPath == path);
  132. if (count >= BACK_UP_COUNT)
  133. {
  134. var fs = db.Gets<BackupFiles>(x => x.FullPath == path, null).OrderBy(x => x.Id).ToList();
  135. if (ListTool.HasElements(fs))
  136. {
  137. for (int i = 0; i <= count - BACK_UP_COUNT; i++)
  138. {
  139. try
  140. {
  141. File.Delete(fs[i].BackupFullPath);
  142. db.Del(fs[i], true);
  143. }
  144. catch (Exception e) { }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 备份文件
  152. /// </summary>
  153. /// <param name="path"></param>
  154. /// <param name="newpath"></param>
  155. private void BackupFile(string path, string newpath)
  156. {
  157. using (var db = new Muse())
  158. {
  159. try
  160. {
  161. if (DirTool.Create(DirTool.GetFilePath(newpath)))
  162. {
  163. File.Copy(path, newpath, true);
  164. db.Add(new BackupFiles()
  165. {
  166. FullPath = path,
  167. BackupFullPath = newpath,
  168. Size = FileTool.Size(path),
  169. BackupTime = DateTimeConvert.StandardString(DateTime.Now),
  170. });
  171. }
  172. }
  173. catch (Exception e) { }
  174. }
  175. }
  176. }
  177. }