private static void RunInitialBackup() { var message = "Initial backup started"; _log.Info(message); message.WriteEventLog(); try { var job = new FileBackupJob(); foreach (Configuration.Folder f in Config.Instance.FolderList) { if (!Directory.Exists(f.Path)) { continue; } BackupDirectory(job, f.Path, f.Recursive); } message = "Initial backup finished"; _log.Info(message); message.WriteEventLog(); } catch (Exception ex) { message = "Initial backup failed"; _log.Error(message, ex); message.WriteEventLog(ex); } }
// EVENT HANDLER private void OnChanged(object source, FileSystemEventArgs e) { lock (_eventHandlerSyncLock) { _log.Trace(); try { // do not handle changes for type dirtectory if (Directory.Exists(e.FullPath)) { return; } // block ms office temp files if (Path.GetFileName(e.FullPath).StartsWith("~$")) { return; } if (IsIgnored(e.FullPath)) { return; } _log.DebugFormat("File event {0}: '{1}'", e.ChangeType, e.FullPath); FileBackupJob.Backup(e.FullPath); } catch (Exception ex) { _log.Error(string.Format("File event {0}: '{1}'", e.ChangeType, e.FullPath), ex); ex.WriteEventLog(); } } }
private static void BackupDirectory(FileBackupJob job, string directory, bool recursive) { if (IsIgnored(directory)) { return; } string[] files; try { files = Directory.GetFiles(directory); } catch (UnauthorizedAccessException ex) { var message = string.Format("Initial backup failed for folder {0}", directory); _log.Error(message, ex); message.WriteEventLog(ex); return; } // backup files foreach (var file in files) { var dest = Path.Combine(Instance._backupDir, job.GetFileName(file)); try { job.BackupFile(file, dest); } catch (Exception ex) { _log.Error(string.Format("Failure: <initial backup> from '{0}' to '{1}'", file, dest), ex); ex.WriteEventLog(); } } // backup recursively if (recursive) { string[] directories; try { directories = Directory.GetDirectories(directory); } catch (UnauthorizedAccessException ex) { var message = string.Format("Initial backup failed for subfolders of {0}", directory); _log.Error(message, ex); message.WriteEventLog(ex); return; } foreach (var dir in directories) { BackupDirectory(job, dir, recursive); } } }
private FileBackupManager() { _log.Trace(); try { var config = Config.Instance; var backupUri = new Uri(config.Backup.Path); if (backupUri.IsUnc && !string.IsNullOrEmpty(config.Backup.Username)) { _log.InfoFormat("impersonate as user '{0}'", config.Backup.Username); _windowsImpersonationContext = Impersonater.LogOn(config.Backup.Username, config.Backup.Password); } //create resources // file system watcher map _fileSystemWatcherMap = new Dictionary <string, FileSystemWatcher>(); _invalidCharsRegex = new Regex(@"[\W-[\.]]", RegexOptions.Compiled); _fileExtension = new string[config.NumberOfBackupCopies.Value]; for (int i = 0; i < _fileExtension.Length; i++) { _fileExtension[i] = string.Format(".{0}.bak", (i > 9 ? i.ToString() : "0" + i.ToString())); } _backupDir = Path.Combine(config.Backup.Path, System.Net.Dns.GetHostName()); //var writePermission = new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read | FileIOPermissionAccess.Append, _backupDir); //writePermission.Demand(); Directory.CreateDirectory(_backupDir); //start jobs var activeWatcher = false; foreach (Configuration.Folder f in config.FolderList) { activeWatcher |= CreateFileSystemWatcher(f.Path, f.Recursive); } if (activeWatcher) { FileBackupJob.StartInitialBackup(); } } catch (Exception ex) { _log.Fatal(string.Format("Failed to create file backup manager - {0}: {1}", ex.GetType().FullName, ex.Message), ex); ex.WriteEventLog(); throw; } }
public static void Backup(string file) { _log.Trace(); lock (_queueLock) { FileBackupJob job; if (!_jobQueue.TryGetValue(file, out job)) { job = new FileBackupJob(); _jobQueue.Add(file, job); } job.StartBackup(file); } }
public static void Rename(string oldFile, string newFile) { _log.Trace(); lock (_queueLock) { FileBackupJob job; if (_jobQueue.ContainsKey(oldFile)) { job = _jobQueue[oldFile]; _jobQueue.Remove(oldFile); _jobQueue.Add(newFile, job); } else { job = new FileBackupJob(); } job.StartRename(oldFile, newFile); } }
// EVENT HANDLER private void OnRenamed(object source, RenamedEventArgs e) { lock (_eventHandlerSyncLock) { _log.Trace(); try { // block ms office temp files if (!Path.GetFileName(e.OldFullPath).StartsWith("~") && Path.GetFileName(e.FullPath).StartsWith("~")) { return; } _log.DebugFormat("File event {0}: '{1}' renamed to '{2}'", e.ChangeType, e.OldFullPath, e.FullPath); FileBackupJob.Rename(e.OldFullPath, e.FullPath); } catch (Exception ex) { _log.Error(string.Format("File event {0}: '{1}' renamed to '{2}'", e.ChangeType, e.OldFullPath, e.FullPath), ex); ex.WriteEventLog(); } } }