1 /** 2 * Copyright © DiamondMVC 2019 3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module diamond.security.backup.filebackup; 7 8 import diamond.security.backup.backupservice; 9 import diamond.security.backup.backuppath; 10 11 /// Wrapper around a file backup service. 12 final class FileBackupService : BackupService 13 { 14 public: 15 /** 16 * Creates a new file backup service. 17 * Params: 18 * time = The time to wait between backing up. 19 */ 20 this(size_t time) 21 { 22 super(time); 23 } 24 25 protected: 26 /** 27 * Handler for performing a file backup. 28 * Params: 29 * paths = The paths to backup. 30 */ 31 override void onBackup(const(BackupPath[]) paths) 32 { 33 import std.file : dirEntries, SpanMode, exists, isFile, isDir, rmdirRecurse, copy; 34 import std.path : dirName; 35 import std.array : replace; 36 37 foreach (path; paths) 38 { 39 if (path.source.isDir && path.destination.isDir) 40 { 41 if (!path.destination.exists) 42 { 43 rmdirRecurse(path.destination); 44 } 45 46 foreach (string entryPath; dirEntries(path.source, SpanMode.depth)) 47 { 48 auto subEntryPath = path.destination ~ "/" ~ entryPath.replace(path.source, ""); 49 50 if (subEntryPath.isDir && !subEntryPath.exists) 51 { 52 rmdirRecurse(subEntryPath); 53 } 54 else if (subEntryPath.isFile) 55 { 56 if (!subEntryPath.dirName.exists) 57 { 58 rmdirRecurse(subEntryPath.dirName); 59 } 60 61 entryPath.copy(subEntryPath); 62 } 63 } 64 } 65 else if (path.source.isFile && path.destination.isFile) 66 { 67 if (!path.destination.dirName.exists) 68 { 69 rmdirRecurse(path.destination.dirName); 70 } 71 72 path.source.copy(path.destination); 73 } 74 } 75 } 76 }