Exemplo n.º 1
0
        private void Run(object sender, DoWorkEventArgs ev)
        {
            BOM = BackupOperationManager.Instance;
            BOM.Initialize();
            PipeSecurity ps = new PipeSecurity();

            ps.AddAccessRule(new PipeAccessRule("CREATOR OWNER", PipeAccessRights.FullControl, AccessControlType.Allow));
            ps.AddAccessRule(new PipeAccessRule("USERS", PipeAccessRights.FullControl, AccessControlType.Allow));
            ps.AddAccessRule(new PipeAccessRule("SYSTEM", PipeAccessRights.FullControl, AccessControlType.Allow));
            while (true)
            {
                BOM.RunEventHandlers();

                IPCServer = new NamedPipeServerStream("SilentBackupIPCServer", PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 64, 0, ps);
                do
                {
                    try
                    {
                        var ar = IPCServer.BeginWaitForConnection(null, null);
                        if (ar.AsyncWaitHandle.WaitOne(5000))
                        {
                            IPCServer.EndWaitForConnection(ar);
                        }

                        if ((sender as BackgroundWorker).CancellationPending == true)
                        {
                            ev.Cancel = true;
                            IPCServer.Dispose();
                            BOM.Dispose();
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                    }
                } while (IPCServer.IsConnected == false);

                StreamReader reader = new StreamReader(IPCServer);
                try
                {
                    var line = reader.ReadLine();
                    if (line == "Reload")
                    {
                        DebugIO.WriteStatement("IPCServer thread", "Reloading config");
                        BOM.Reload();
                    }
                }
                catch (Exception e)
                {
                    ReportIO.WriteStatement(e.Message);
                }
                reader.Close();

                IPCServer.Dispose();
                IPCServer = null;
            }
        }
Exemplo n.º 2
0
 private void RunDateTimeHandler(object sender, DoWorkEventArgs ev)
 {
     try
     {
         DTHandler.Run(sender, ev);
     }
     catch (Exception ex)
     {
         ReportIO.WriteStatement(ex.Message);
     }
 }
Exemplo n.º 3
0
 public virtual void AddEvent(Event e, List <Callback> callBacks)
 {
     try
     {
         EventToCallbackMapping.Add(e, callBacks);
     }
     catch (ArgumentException ex)
     {
         EventToCallbackMapping.SingleOrDefault(x => x.Key == e).Value.AddRange(callBacks);
     }
     catch (Exception ex)
     {
         ReportIO.WriteStatement("An issue occurred adding events to the event handler: " + ex.Message);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            //Directory.SetCurrentDirectory(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%") + "\\SilentBackup\\SilentBackup"); // USE THIS FOR RELEASE

            try
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new BackupService()
                };
                RunInteractiveServices(ServicesToRun);                 // USE THIS FOR TESTING
                //ServiceBase.Run(ServicesToRun);  // USE THIS FOR RELEASE
            }
            catch (Exception ex)
            {
                ReportIO.WriteStatement("Fatal service exception: " + ex.Message);
                return;
            }
        }
Exemplo n.º 5
0
        public void Initialize()
        {
            if (Config != null)
            {
                throw new Exception("BackupOperationManager is already initialized.");
            }

            Config       = new Configuration();
            USBHandler   = new USBInsertionEventHandler();
            DTHandler    = new DateTimeEventHandler();
            LogonHandler = new LogonEventHandler();
            USBWorker    = new BackgroundWorker();
            USBWorker.WorkerSupportsCancellation = true;
            USBWorker.DoWork += RunUSBHandler;
            DTWorker          = new BackgroundWorker();
            DTWorker.WorkerSupportsCancellation = true;
            DTWorker.DoWork += RunDateTimeHandler;
            try
            {
                LoadConfig();
                foreach (var trigger in Config.Triggers.Where(ev => ev.Enabled))
                {
                    List <Callback> callBacks = new List <Callback>();
                    foreach (var backop in Config.BackupOperations.Where(x => x.Triggers.Contains(trigger.Id)).Where(x => x.Enabled).ToList())
                    {
                        foreach (var dest in backop.Destinations)
                        {
                            callBacks.Add(new Callback(() =>
                            {
                                // Save original path
                                string ogname = dest.Path.AbsolutePath;
                                // Label the destination.
                                // It is important that the original be labeled and not the copy
                                // because we modify the destination information and save the configuration.
                                // If we modified a copy, we would lose those modifications and end up with
                                // adverse effects.
                                Labeler.Label(dest.Path, dest.Label);
                                // Make a copy of the destination
                                var labeledCopy = new Path(dest.Path);
                                // Reset the original path because we only need the label for the copy
                                dest.Path.AbsolutePath = ogname;

                                FileTransferManager.Copy(new Path(backop.Source), labeledCopy, backop.CopySubDirs, true);
                            }));
                        }
                    }

                    var triggerType = trigger.GetType();

                    // Send event to appropriate handler based on the event type
                    if (triggerType == typeof(USBInsertionEvent))
                    {
                        USBHandler.AddEvent(trigger, callBacks.ToList());
                    }
                    else if (triggerType == typeof(DateTimeEvent))
                    {
                        DTHandler.AddEvent(trigger, callBacks.ToList());
                    }
                    else if (triggerType == typeof(LogonEvent))
                    {
                        LogonHandler.AddEvent(trigger, callBacks.ToList());
                    }
                    else
                    {
                        ReportIO.WriteStatement("Invalid trigger type defined in configuration.");
                    }
                }
            }
            catch (Exception e)
            {
                ReportIO.WriteStatement(e.Message);
                Reload();
            }
        }