Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.IO.Packaging;
  6. using System.Linq;
  7. using System.Net.Mime;
  8. using System.Windows.Forms;
  9. using Backupr.Helpers;
  10. using Backupr.Models;
  11.  
  12. namespace Backupr.Services
  13. {
  14. public interface IFileActionService
  15. {
  16. void ArchiveRule(BackupRule rule);
  17. void EndOfDay();
  18. }
  19.  
  20. public class FileActionService : IFileActionService
  21. {
  22. public void ArchiveRule(BackupRule rule)
  23. {
  24. ArchiveAction archiver = new ArchiveAction(rule);
  25. archiver.MoveFilesToTemp();
  26. if (rule.Type == RuleType.Archive)
  27. {
  28. archiver.ZipTempFile();
  29. archiver.MoveArchiveToDestinations();
  30. }
  31. else if (rule.Type == RuleType.Mirror)
  32. {
  33. archiver.MirrorRule();
  34. }
  35. archiver.CleanTempDirectory();
  36. archiver.CleanOldBackups();
  37. }
  38.  
  39. public void EndOfDay()
  40. {
  41. IRuleService ruleService = new RuleService();
  42. MessageBox.Show("Warning this action will reboot your PC!");
  43. foreach (BackupRule rule in ruleService.GetAll().Where(r => r.EndOfDay == true))
  44. {
  45. ArchiveRule(rule);
  46. }
  47. Process.Start("shutdown.exe", "/r /f /t 1");
  48. }
  49. }
  50.  
  51. // HACK: need to find a better way to handle deleted files and directories.
  52. // perhaps a logging system that lets the user know the file is missing
  53. // or just removing the deleted file from the rule.
  54. // refer to MoveFilesToTemp(), MirrorRule(), DirectoryCopy()
  55. internal class ArchiveAction
  56. {
  57. // TODO: allow users to define archiveName via built in tokens.
  58. private string archiveName = System.Environment.MachineName + '_' + DateTime.Now.ToString("yyyyMMddhhmmss");
  59. private BackupRule rule;
  60. private List<string> excludes = new List<string>();
  61.  
  62. public ArchiveAction(BackupRule _rule)
  63. {
  64. rule = _rule;
  65. foreach (BackupPath excludePath in rule.Paths.Where(p => p.Type == PathType.Exclude))
  66. {
  67. excludes.Add(excludePath.Value);
  68. }
  69. }
  70.  
  71. public void MoveFilesToTemp()
  72. {
  73. foreach (BackupPath sourcePath in rule.Paths.Where(p => p.Type == PathType.Source))
  74. {
  75. // See comment at top of ArchiveAction Class
  76. if (PathIsDirectory(sourcePath.Value))
  77. {
  78. DirectoryCopy(sourcePath.Value, Path.Combine(tempDirectory, Path.GetFileName(sourcePath.Value)), true);
  79. }
  80. else if (File.Exists(sourcePath.Value) && !excludes.Any(p => p == sourcePath.Value))
  81. {
  82. File.Copy(sourcePath.Value, Path.Combine(tempDirectory, Path.GetFileName(sourcePath.Value)));
  83. }
  84. }
  85. }
  86.  
  87. public void ZipTempFile()
  88. {
  89. using (Package zip = Package.Open(Path.Combine(Asset.BackuprTempDirectory, archiveName + ".zip"), FileMode.Create))
  90. {
  91. foreach (string file in Directory.GetFiles(tempDirectory, "*.*", SearchOption.AllDirectories))
  92. {
  93. string packageFile = file.Replace(' ', '_');
  94. Uri uri = new Uri(packageFile.Substring(tempDirectory.Length), UriKind.Relative);
  95. PackagePart ZipPart = zip.CreatePart(PackUriHelper.CreatePartUri(uri), MediaTypeNames.Application.Zip, CompressionOption.Maximum);
  96. byte[] B = File.ReadAllBytes(file);
  97. ZipPart.GetStream().Write(B, 0, B.Length);
  98. }
  99. }
  100. }
  101.  
  102. public void MoveArchiveToDestinations()
  103. {
  104. foreach (BackupPath destinationPath in rule.Paths.Where(p => p.Type == PathType.Destination))
  105. {
  106. if (!Directory.Exists(destinationPath.Value))
  107. {
  108. Directory.CreateDirectory(destinationPath.Value);
  109. }
  110. File.Copy(Path.Combine(Asset.BackuprTempDirectory, archiveName + ".zip"), Path.Combine(destinationPath.Value, archiveName + ".zip"));
  111. }
  112. }
  113.  
  114. public void MirrorRule()
  115. {
  116. foreach (BackupPath destinationPath in rule.Paths.Where(p => p.Type == PathType.Destination))
  117. {
  118. DirectoryCopy(tempDirectory, destinationPath.Value, true);
  119. }
  120. }
  121.  
  122. public void CleanTempDirectory()
  123. {
  124. Directory.Delete(Asset.BackuprTempDirectory, true);
  125. }
  126.  
  127. public void CleanOldBackups()
  128. {
  129. if (rule.Type == RuleType.Archive)
  130. {
  131. foreach (BackupPath destinationPath in rule.Paths.Where(p => p.Type == PathType.Destination))
  132. {
  133. DirectoryInfo directory = new DirectoryInfo(destinationPath.Value);
  134. if (!directory.Exists)
  135. {
  136. // See comment at top of ArchiveAction Class
  137. break;
  138. }
  139. FileInfo[] files = directory.GetFiles();
  140. foreach (FileInfo file in files.OrderByDescending(p => p.LastWriteTime).Skip(rule.BackupsToKeep))
  141. {
  142. string filePath = Path.Combine(destinationPath.Value, file.Name);
  143. File.Delete(filePath);
  144. }
  145. }
  146. }
  147. }
  148.  
  149. private bool PathIsDirectory(string path)
  150. {
  151. FileAttributes fileAttribute = File.GetAttributes(@path);
  152. if ((fileAttribute & FileAttributes.Directory) == FileAttributes.Directory)
  153. {
  154. return true;
  155. }
  156. return false;
  157. }
  158.  
  159. private string tempDirectory
  160. {
  161. get
  162. {
  163. string directory = Path.Combine(Asset.BackuprTempDirectory, archiveName);
  164. if (!Directory.Exists(directory))
  165. {
  166. Directory.CreateDirectory(directory);
  167. }
  168. return directory;
  169. }
  170. }
  171.  
  172. private void DirectoryCopy(string source, string destination, bool copySubDirectories)
  173. {
  174. if (excludes.Any(p => p == source))
  175. {
  176. return;
  177. }
  178.  
  179. DirectoryInfo directory = new DirectoryInfo(source);
  180. if (!directory.Exists)
  181. {
  182. // See comment at top of ArchiveAction Class
  183. return;
  184. }
  185. if (!Directory.Exists(destination))
  186. {
  187. Directory.CreateDirectory(destination);
  188. }
  189. FileInfo[] files = directory.GetFiles();
  190. foreach (FileInfo file in files)
  191. {
  192. if (!excludes.Any(p => p == file.FullName))
  193. {
  194. string temppath = Path.Combine(destination, file.Name);
  195. file.CopyTo(temppath, true);
  196. }
  197. }
  198. if (copySubDirectories)
  199. {
  200. DirectoryInfo[] subDirectories = directory.GetDirectories();
  201. foreach (DirectoryInfo subDirectory in subDirectories)
  202. {
  203. if (!excludes.Any(p => p == subDirectory.FullName))
  204. {
  205. string temppath = Path.Combine(destination, subDirectory.Name);
  206. DirectoryCopy(subDirectory.FullName, temppath, copySubDirectories);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement