Esempio n. 1
0
        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;
            }
        }
Esempio n. 2
0
        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // STOP
                    foreach (FileSystemWatcher fsw in _fileSystemWatcherMap.Values)
                    {
                        //fsw.EnableRaisingEvents = false;
                        fsw.Dispose();
                    }
                    _fileSystemWatcherMap.Clear();

                    // wait on pending jobs... for a maximum of 10 seconds
                    DateTime startTime = DateTime.Now;
                    TimeSpan timeout   = new TimeSpan(0, 0, 10);
                    while (FileBackupJob.PendingJobs && (DateTime.Now - startTime) < timeout)
                    {
                        try
                        {
                            System.Threading.Thread.Sleep(1000);
                        }
                        catch { }
                    }

                    //if (backupIdentity != null)
                    //{
                    //    backupIdentity.Dispose();
                    //    backupIdentity = null;
                    //}

                    if (_windowsImpersonationContext != null)
                    {
                        Impersonater.LogOff(_windowsImpersonationContext);
                        _windowsImpersonationContext.Dispose();
                        _windowsImpersonationContext = null;
                    }
                }

                FileBackupManager._singleton = null;
                _disposed = true;
            }
        }