public static void TryRestoreWhenHasSoftDeleteAndBackup() { var dummy = new DummySerializable(1); var file = CreateFile(); var backup = CreateBackupFile(); var softDelete = file.SoftDeleteFile(); backup.Save(dummy); dummy.Value++; softDelete.Save(dummy); AssertFile.Exists(false, file); AssertFile.Exists(true, softDelete); AssertFile.Exists(true, backup); var backuper = Backuper.Create(new BackupSettings(Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 3)); Assert.IsTrue(backuper.CanRestore(file)); Assert.IsTrue(backuper.TryRestore(file)); Assert.AreEqual(dummy, file.Read <DummySerializable>()); AssertFile.Exists(true, file); AssertFile.Exists(false, softDelete); AssertFile.Exists(true, backup); }
public void SetUp() { _tempDir = new DirectoryInfo(@"C:\Temp\Gu.Persist"); _setting = new BackupSettings(true, _tempDir, ".bak", BackupSettings.DefaultTimeStampFormat, false, 1, Int32.MaxValue); _file = _tempDir.CreateFileInfoInDirectory("Meh.cfg"); _backup = _tempDir.CreateFileInfoInDirectory("Meh.bak"); _backup1 = _tempDir.CreateFileInfoInDirectory(string.Format("Meh{0}.bak", new DateTime(2015, 06, 13, 21, 38, 00).ToString(BackupSettings.DefaultTimeStampFormat, CultureInfo.InvariantCulture))); _backup2 = _tempDir.CreateFileInfoInDirectory(string.Format("Meh{0}.bak", new DateTime(2015, 06, 13, 21, 37, 00).ToString(BackupSettings.DefaultTimeStampFormat, CultureInfo.InvariantCulture))); _backup3 = _tempDir.CreateFileInfoInDirectory(string.Format("Meh{0}.bak", new DateTime(2015, 06, 12, 21, 38, 00).ToString(BackupSettings.DefaultTimeStampFormat, CultureInfo.InvariantCulture))); _backup4 = _tempDir.CreateFileInfoInDirectory(string.Format("Meh{0}.bak", new DateTime(2015, 06, 11, 21, 38, 00).ToString(BackupSettings.DefaultTimeStampFormat, CultureInfo.InvariantCulture))); _backup5 = _tempDir.CreateFileInfoInDirectory(string.Format("Meh{0}.bak", new DateTime(2015, 06, 10, 21, 38, 00).ToString(BackupSettings.DefaultTimeStampFormat, CultureInfo.InvariantCulture))); _otherBackup = _tempDir.CreateFileInfoInDirectory(string.Format("Other{0}.bak", new DateTime(2015, 06, 13, 21, 38, 00).ToString(BackupSettings.DefaultTimeStampFormat, CultureInfo.InvariantCulture))); _softDelete = _tempDir.CreateFileInfoInDirectory(string.Format("Meh{0}.bak.delete", new DateTime(2015, 06, 10, 21, 38, 00).ToString(BackupSettings.DefaultTimeStampFormat, CultureInfo.InvariantCulture))); _timestampedBackups = new FileInfo[] { _backup1, _backup2, _backup3, _backup4, _backup5 }; _file.Delete(); _backup.Delete(); foreach (var backup in _timestampedBackups) { backup.Delete(); } _otherBackup.Delete(); _otherBackup.VoidCreate(); _softDelete.VoidCreate(); _backuper = new Backuper(_setting); }
public static void AfterSaveWhenNoFiles() { var file = CreateFile(); var backuper = Backuper.Create(new BackupSettings(Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 2)); using var lockedFile = LockedFile.CreateIfExists(file, x => x.OpenRead()); backuper.AfterSave(lockedFile !); }
public void Can_PerformTheBackup() { string backupPath = Backuper.Backup(@"TestFiles\text_file.txt"); string read = File.ReadAllText(backupPath); File.Delete(backupPath); Assert.AreEqual("hello", read); }
public void BackupWhenNotExtsis() { var backuper = Backuper.Create(new BackupSettings(this.Directory.FullName, BackupSettings.DefaultExtension, null, 1, 3)); AssertFile.Exists(false, this.File); AssertFile.Exists(false, this.Backup); Assert.IsFalse(backuper.BeforeSave(this.File)); AssertFile.Exists(false, this.File); AssertFile.Exists(false, this.Backup); }
private void Button_Click(object sender, RoutedEventArgs e) { try { Backuper.doBackup(); MessageBox.Show("Complete :)", "Hey!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (IOException) { MessageBox.Show("Probably, Firefox is already ran.\n Please close FF and try again...", "Hey!", MessageBoxButton.OK, MessageBoxImage.Error); } }
public static void BeforeSaveWhenFileDoesNotNotExists() { var file = CreateFile(); var backup = CreateBackupFile(); var backuper = Backuper.Create(new BackupSettings(Directory.FullName, BackupSettings.DefaultExtension, null, 1, 3)); AssertFile.Exists(false, file); AssertFile.Exists(false, backup); Assert.AreEqual(false, backuper.BeforeSave(file)); AssertFile.Exists(false, file); AssertFile.Exists(false, backup); }
public void TryRestoreWhenNoFiles() { AssertFile.Exists(false, this.File); AssertFile.Exists(false, this.SoftDelete); AssertFile.Exists(false, this.Backup); var backuper = Backuper.Create(new BackupSettings(this.Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 3)); Assert.IsFalse(backuper.TryRestore(this.File)); AssertFile.Exists(false, this.File); AssertFile.Exists(false, this.SoftDelete); AssertFile.Exists(false, this.Backup); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); while (true) { Console.WriteLine("Select an operating mode:\n" + "1) Observer Mode\n" + "2) Rollback mode"); Console.Write("Input: "); if (int.TryParse(Console.ReadLine(), out int input) && input > 0 && input < 3) { switch (input) { case 1: var watcher = new FileWatcher(); watcher.FileAction += OnFileChanged; Console.WriteLine("To exit observer mode, click Q"); while (true) { ConsoleKeyInfo key = Console.ReadKey(true); if (key.Key == ConsoleKey.Q) { break; } } break; case 2: var backuper = new Backuper(); string dateFormat = "dd.MM.yyyy HH:mm:ss"; Console.Write($"Enter date in format {dateFormat}: "); if (DateTime.TryParseExact(Console.ReadLine(), dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result)) { backuper.BackupAllFiles(result); } else { Console.WriteLine("Date entered incorrectly."); } break; } } else { Console.WriteLine("Data entered incorrectly"); } } }
public void TestRules() { var backupRule = new BackupRule(_dataPlace, _config); var actionMock = new Mock <IBackupAction>(); var backuper = new Backuper(actionMock.Object, backupRule); _testedData.ChangeFile(); Thread.Sleep(MoreTime); backuper.Update(); Thread.Sleep(MoreTime); backuper.Update(); actionMock.Verify(act => act.Backup(), Times.Once); }
public void PurgeWhenNoFiles() { var backuper = Backuper.Create(new BackupSettings(this.Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 2)); using (var lockedFile = this.LockedFile()) { backuper.AfterSave(lockedFile); } AssertFile.Exists(false, this.BackupOneMinuteOld); AssertFile.Exists(false, this.BackupOneHourOld); AssertFile.Exists(false, this.BackupOneDayOld); AssertFile.Exists(false, this.BackupOneMonthOld); AssertFile.Exists(false, this.BackupOneYearOld); ////AssertFile.Exists(false, _softDelete); }
public void TryRestoreWhenHasSoftDelete() { var backuper = Backuper.Create(new BackupSettings(this.Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 3)); this.SoftDelete.Save(this.dummy); AssertFile.Exists(false, this.File); AssertFile.Exists(true, this.SoftDelete); Assert.IsTrue(backuper.CanRestore(this.File)); Assert.IsTrue(backuper.TryRestore(this.File)); Assert.AreEqual(this.dummy, this.File.Read <DummySerializable>()); AssertFile.Exists(true, this.File); AssertFile.Exists(false, this.SoftDelete); AssertFile.Exists(false, this.Backup); }
public void PurgeDeletesSoftDeletesNoTimestamp() { this.File.CreatePlaceHolder(); this.SoftDelete.CreatePlaceHolder(); this.Backup.CreatePlaceHolder(); var backuper = Backuper.Create(new BackupSettings(this.Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, int.MaxValue, int.MaxValue)); using (var lockedFile = this.LockedFile()) { backuper.AfterSave(lockedFile); } AssertFile.Exists(true, this.File); AssertFile.Exists(true, this.Backup); AssertFile.Exists(false, this.SoftDelete); }
public void TryRestoreWhenHasBackupAndOriginal() { this.File.WriteAllText("File"); this.Backup.Save(this.dummy); AssertFile.Exists(true, this.File); AssertFile.Exists(false, this.SoftDelete); AssertFile.Exists(true, this.Backup); var backuper = Backuper.Create(new BackupSettings(this.Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 3)); Assert.Throws <InvalidOperationException>(() => backuper.TryRestore(this.File)); AssertFile.Exists(true, this.File); AssertFile.Exists(false, this.SoftDelete); AssertFile.Exists(true, this.Backup); }
public static void Restore(string destinationFolderPath, Cancelable cancelable) { if (destinationFolderPath.StartsWith(Backuper.BackupStorageBaseFolderPath, StringComparison.InvariantCultureIgnoreCase)) { throw new Exception("Path must be outside of Simple Backup folder."); } var dst = new DirectoryInfo(destinationFolderPath); var src = new DirectoryInfo(Path.Combine(Backuper.BackupStorageBaseFolderPath, Backuper.MakeUnique(dst))); if (!src.Exists) { throw new MessageBoxException("No backup available for this folder."); } deleteDirectoryContents(dst, cancelable); Backuper.CopyFilesRecursively(src, dst, cancelable); }
public static void TryRestoreWhenNoFiles() { var file = CreateFile(); var backup = CreateBackupFile(); var softDelete = file.SoftDeleteFile(); AssertFile.Exists(false, file); AssertFile.Exists(false, softDelete); AssertFile.Exists(false, backup); var backuper = Backuper.Create(new BackupSettings(Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 3)); Assert.IsFalse(backuper.TryRestore(file)); AssertFile.Exists(false, file); AssertFile.Exists(false, softDelete); AssertFile.Exists(false, backup); }
public static void Restore() { var file = Directory.CreateFileInfoInDirectory(nameof(DummySerializable) + ".cfg"); Assert.AreEqual(false, repository !.Backuper.CanRestore(file)); repository !.Save(file, dummy); var json = File.ReadAllText(file.FullName); Assert.AreEqual("{\r\n \"Value\": 1\r\n}", json); Assert.AreEqual(false, repository !.Backuper.CanRestore(file)); dummy !.Value++; JsonFile.Save(file, dummy); json = File.ReadAllText(file.FullName); Assert.AreEqual("{\"Value\":2}", json); Assert.AreEqual(true, repository.Backuper.CanRestore(file), "CanRestore after save"); Assert.AreEqual(true, repository.Backuper.TryRestore(file), "TryRestore"); Assert.AreEqual(false, repository.Backuper.CanRestore(file), "CanRestore after restore"); var restored = JsonFile.Read <DummySerializable>(file); Assert.AreEqual(dummy.Value - 1, restored.Value); }
public void TestEncryptedArchives() { var newConfig = _config; newConfig.usePassword = true; newConfig.password = "******"; newConfig.backupToPath += "\\test.zip"; var backupRule = new BackupRule(_dataPlace, newConfig); var backupAction = new BackupAction(_dataPlace, newConfig); var backuper = new Backuper(backupAction, backupRule); backupAction.Backup(); var verificator = new DataVerificator(_testedData); Assert.Catch <InvalidDataException>(() => verificator.testArchive(newConfig.backupToPath)); Assert.IsTrue(verificator.testEncryptedArchive(newConfig.backupToPath, newConfig.password)); }
public void PurgeOld() { this.SoftDelete.CreatePlaceHolder(); foreach (var backup in this.TimestampedBackups) { backup.CreatePlaceHolder(); } var backuper = Backuper.Create(new BackupSettings(this.Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, int.MaxValue, 2)); using (var lockedFile = this.LockedFile()) { backuper.AfterSave(lockedFile); } AssertFile.Exists(true, this.BackupOneMinuteOld); AssertFile.Exists(true, this.BackupOneHourOld); AssertFile.Exists(true, this.BackupOneDayOld); AssertFile.Exists(false, this.BackupOneMonthOld); AssertFile.Exists(false, this.BackupOneYearOld); AssertFile.Exists(false, this.SoftDelete); }
public static void AfterSaveDeletesSoftDeletesWhenNoPurgeOfBackups() { var settings = new BackupSettings(Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, int.MaxValue, int.MaxValue); var file = CreateFile(); var backup = CreateBackupFile(); var softDelete = file.SoftDeleteFile(); softDelete.CreateFileOnDisk(); var backupOneMinuteOld = backup.WithTimeStamp(DateTime.Now.AddMinutes(-1), settings); backupOneMinuteOld.CreateFileOnDisk(); var backupOneHourOld = backup.WithTimeStamp(DateTime.Now.AddHours(-1), settings); backupOneHourOld.CreateFileOnDisk(); var backupOneDayOld = backup.WithTimeStamp(DateTime.Now.AddDays(-1), settings); backupOneDayOld.CreateFileOnDisk(); var backupOneMonthOld = backup.WithTimeStamp(DateTime.Now.AddMonths(-1), settings); backupOneMonthOld.CreateFileOnDisk(); var backupOneYearOld = backup.WithTimeStamp(DateTime.Now.AddYears(-1), settings); backupOneYearOld.CreateFileOnDisk(); var backuper = Backuper.Create(settings); using (var lockedFile = LockedFile.CreateIfExists(file, x => x.OpenRead())) { backuper.AfterSave(lockedFile !); } AssertFile.Exists(true, backupOneMinuteOld); AssertFile.Exists(true, backupOneHourOld); AssertFile.Exists(true, backupOneDayOld); AssertFile.Exists(true, backupOneMonthOld); AssertFile.Exists(true, backupOneYearOld); AssertFile.Exists(false, softDelete); }
public static void TryRestoreWhenHasBackupAndOriginal() { var dummy = new DummySerializable(-1); var file = CreateFile(); var backup = CreateBackupFile(); var softDelete = file.SoftDeleteFile(); file.WriteAllText("File"); backup.Save(dummy); AssertFile.Exists(true, file); AssertFile.Exists(false, softDelete); AssertFile.Exists(true, backup); var backuper = Backuper.Create(new BackupSettings(Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 3)); var exception = Assert.Throws <InvalidOperationException>(() => backuper.TryRestore(file)); StringAssert.IsMatch(@"Expected file .*BackuperTests\\TryRestoreWhenHasBackupAndOriginal.cfg to not exist.", exception !.Message); AssertFile.Exists(true, file); AssertFile.Exists(false, softDelete); AssertFile.Exists(true, backup); }
public override void SetUp() { _backuper = (Backuper)Backuper.Create(Setting); _dummy = new DummySerializable(1); }