private bool FileRequiresBackup(BackupFile backupFile) { if (backupFile.LastModified.CompareTo(LongFile.GetLastWriteTime(Path)) != 0) { return(true); } if (backupFile.Size != LongFile.GetFileSize(Path)) { return(true); } if (!LongFile.Exists(GetBackupLocation())) { return(true); } return(false); }
public void Backup() { bool backupRequired = false; try { DateTime currentFileTimestamp = LongFile.GetLastWriteTime(Path).ToLocalTime(); BackupFile backupFile = BackupDbContext.DB.BackupFiles .Where(f => f.Path == Path) .OrderByDescending(f => f.BackupTimestamp) .FirstOrDefault(f => true); if (backupFile == null) { backupRequired = true; } else if (backupFile.LastModified.CompareTo(currentFileTimestamp) != 0) { backupRequired = true; } else if (backupFile.Size != LongFile.GetFileSize(Path)) { backupRequired = true; } if (backupRequired) { backupFile = new BackupFile { Path = Path, BackupFileType = BackupFileType.File, LastModified = currentFileTimestamp, Size = LongFile.GetFileSize(Path), BackupTimestamp = DateTime.Now, BackupLocation = GetBackupLocation(), Parent = LongFile.GetParent(Path), Name = LongFile.GetName(Path), }; string destPath = LongFile.Combine(BackupDbContext.Destination, LongFile.Combine("Files", backupFile.BackupLocation)); string destFolder = LongFile.GetParent(destPath); if (!LongDirectory.Exists(destFolder)) { LongDirectory.CreateDirectory(destFolder); } LongFile.Copy(Path, destPath, true); LongFile.SetAttributes(destPath, LongFile.GetAttributes(destPath) & ~System.IO.FileAttributes.ReadOnly & ~System.IO.FileAttributes.Hidden); BackupDbContext.DB.BackupFiles.Add(backupFile); BackupDbContext.DB.SaveChanges(); if (FileBackupFinished != null) { FileBackupFinished(this, new FileEventArgs() { Path = Path, Cancelled = false, Success = true }); } } } catch (Exception ex) { } }