Beispiel #1
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //If we are on Windows, append the bundled "win-tools" programs to the search path
            //We add it last, to allow the user to override with other versions
            if (!Library.Utility.Utility.IsClientLinux)
            {
                string wintools = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "win-tools");
                Environment.SetEnvironmentVariable("PATH",
                    Environment.GetEnvironmentVariable("PATH") +
                    System.IO.Path.PathSeparator.ToString() +
                    wintools +
                    System.IO.Path.PathSeparator.ToString() +
                    System.IO.Path.Combine(wintools, "gpg") //GPG needs to be in a subfolder for wrapping reasons
                );
            }

            Library.Utility.UrlUtillity.ErrorHandler = new XervBackup.Library.Utility.UrlUtillity.ErrorHandlerDelegate(DisplayURLOpenError);

            //If we are on windows we encrypt the database by default
            //We do not encrypt on Linux as most distros use a SQLite library without encryption support,
            //Linux users can use an encrypted home folder, or install a SQLite library with encryption support
            if (!Library.Utility.Utility.IsClientLinux && string.IsNullOrEmpty(Environment.GetEnvironmentVariable(DB_KEY_ENV_NAME)))
            {
                //Note that the password here is a default password and public knowledge
                //
                //The purpose of this is to prevent casual read of the database, as well
                // as protect from harddisk string scans, not to protect from determined
                // attacks.
                //
                //If you desire better security, start XervBackup once with the commandline option
                // --unencrypted-database to decrypt the database.
                //Then set the environment variable XERVBACKUP_DB_KEY to the desired key,
                // and run XervBackup again without the --unencrypted-database option
                // to re-encrypt it with the new key
                //
                //If you change the key, please note that you need to supply the same
                // key when restoring the setup, as the setup being backed up will
                // be encrypted as well.
                Environment.SetEnvironmentVariable(DB_KEY_ENV_NAME, "XervBackup_Key_42");
            }

            //Find commandline options here for handling special startup cases
            Dictionary<string, string> commandlineOptions = CommandLine.CommandLineParser.ExtractOptions(new List<string>(args));

            foreach (string s in args)
                if (
                    s.Equals("help", StringComparison.InvariantCultureIgnoreCase) ||
                    s.Equals("/help", StringComparison.InvariantCultureIgnoreCase) ||
                    s.Equals("usage", StringComparison.InvariantCultureIgnoreCase) ||
                    s.Equals("/usage", StringComparison.InvariantCultureIgnoreCase))
                    commandlineOptions["help"] = "";

            //If the commandline issues --help, just stop here
            if (commandlineOptions.ContainsKey("help"))
            {
                List<string> lines = new List<string>();
                foreach (Library.Interface.ICommandLineArgument arg in SupportedCommands)
                    lines.Add(string.Format(Strings.Program.HelpDisplayFormat, arg.Name, arg.LongDescription));
                MessageBox.Show(string.Format(Strings.Program.HelpDisplayDialog, string.Join(Environment.NewLine, lines.ToArray())), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (commandlineOptions.ContainsKey("trayless"))
                Program.TraylessMode = Library.Utility.Utility.ParseBoolOption(commandlineOptions, "trayless");

            #if DEBUG
            //Log various information in the logfile
            if (!commandlineOptions.ContainsKey("log-file"))
            {
                commandlineOptions["log-file"] = System.IO.Path.Combine(Application.StartupPath, "XervBackup.debug.log");
                commandlineOptions["log-level"] = XervBackup.Library.Logging.LogMessageType.Profiling.ToString();
            }
            #endif

            if (commandlineOptions.ContainsKey("log-level"))
                foreach (string s in Enum.GetNames(typeof(XervBackup.Library.Logging.LogMessageType)))
                    if (s.Equals(commandlineOptions["log-level"].Trim(), StringComparison.InvariantCultureIgnoreCase))
                        XervBackup.Library.Logging.Log.LogLevel = (XervBackup.Library.Logging.LogMessageType)Enum.Parse(typeof(XervBackup.Library.Logging.LogMessageType), s);

            if (commandlineOptions.ContainsKey("log-file"))
            {
                if (System.IO.File.Exists(commandlineOptions["log-file"]))
                    System.IO.File.Delete(commandlineOptions["log-file"]);
                XervBackup.Library.Logging.Log.CurrentLog = new XervBackup.Library.Logging.StreamLog(commandlineOptions["log-file"]);
            }

            //Set the %XERVBACKUP_HOME% env variable, if it is not already set
            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(DATAFOLDER_ENV_NAME)))
            {
            #if DEBUG
                //debug mode uses a lock file located in the app folder
                Environment.SetEnvironmentVariable(DATAFOLDER_ENV_NAME, System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
            #else
                bool portableMode = Library.Utility.Utility.ParseBoolOption(commandlineOptions, "portable-mode");

                if (portableMode)
                {
                    //Portable mode uses a data folder in the application home dir
                    Environment.SetEnvironmentVariable(DATAFOLDER_ENV_NAME, System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "data"));
                }
                else
                {
                    //Normal release mode uses the systems "Application Data" folder
                    Environment.SetEnvironmentVariable(DATAFOLDER_ENV_NAME, System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.ProductName));
                }
            #endif
            }

            try
            {
                try
                {
                    //This will also create Program.DATAFOLDER if it does not exist
                    SingleInstance = new SingleInstance(Application.ProductName, Program.DATAFOLDER);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format(Strings.Program.StartupFailure, ex.ToString()), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (!SingleInstance.IsFirstInstance)
                {
                    //Linux shows this output
                    Console.WriteLine(Strings.Program.AnotherInstanceDetected);
                    return;
                }

                Version sqliteVersion = new Version((string)SQLiteLoader.SQLiteConnectionType.GetProperty("SQLiteVersion").GetValue(null, null));
                if (sqliteVersion < new Version(3, 6, 3))
                {
                    //The official Mono SQLite provider is also broken with less than 3.6.3
                    MessageBox.Show(string.Format(Strings.Program.WrongSQLiteVersion, sqliteVersion, "3.6.3"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //Create the connection instance
                System.Data.IDbConnection con = (System.Data.IDbConnection)Activator.CreateInstance(SQLiteLoader.SQLiteConnectionType);

                try
                {
            #if DEBUG
                    //Default is to not use encryption for debugging
                    Program.UseDatabaseEncryption = commandlineOptions.ContainsKey("unencrypted-database") ? !Library.Utility.Utility.ParseBoolOption(commandlineOptions, "unencrypted-database") : false;
            #else
                    //Default is to use encryption for release
                    Program.UseDatabaseEncryption = !Library.Utility.Utility.ParseBoolOption(commandlineOptions, "unencrypted-database");
            #endif

                    OpenSettingsDatabase(con, Program.DATAFOLDER);
                }
                catch (Exception ex)
                {
                    //Unwrap the reflection exceptions
                    if (ex is System.Reflection.TargetInvocationException && ex.InnerException != null)
                        ex = ex.InnerException;

                    MessageBox.Show(string.Format(Strings.Program.DatabaseOpenError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                DataConnection = new DataFetcherWithRelations(new SQLiteDataProvider(con));

                string displayLanguage = new Datamodel.ApplicationSettings(DataConnection).DisplayLanguage;
                if (!string.IsNullOrEmpty(displayLanguage) && displayLanguage != Library.Utility.Utility.DefaultCulture.Name)
                {
                    try
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(displayLanguage);
                        System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(displayLanguage);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(string.Format(Strings.Program.LanguageSelectionError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //This is non-fatal, just keep running with system default language
                    }
                }

                try
                {
                    DataFetcherNested logcon = new DataFetcherNested(DataConnection);
                    Log[] items = logcon.GetObjects<Log>("Subaction LIKE ?", "InProgress");
                    if (items != null && items.Length > 0)
                    {
                        foreach (Log l in items)
                            l.SubAction = "Primary";

                        logcon.CommitAllRecursive();
                    }
                }
                catch
                {
                    //Non-fatal but any interrupted backup will not show
                }

                LiveControl = new LiveControls(new ApplicationSettings(DataConnection));
                LiveControl.StateChanged += new EventHandler(LiveControl_StateChanged);
                LiveControl.ThreadPriorityChanged += new EventHandler(LiveControl_ThreadPriorityChanged);
                LiveControl.ThrottleSpeedChanged += new EventHandler(LiveControl_ThrottleSpeedChanged);

                Runner = new XervBackupRunner();
                WorkThread = new WorkerThread<IDuplicityTask>(new WorkerThread<IDuplicityTask>.ProcessItemDelegate(Runner.ExecuteTask), LiveControl.State == LiveControls.LiveControlState.Paused);
                Scheduler = new Scheduler(DataConnection, WorkThread, MainLock);

                DataConnection.AfterDataConnection += new DataConnectionEventHandler(DataConnection_AfterDataConnection);

                DisplayHelper = new MainForm();
                DisplayHelper.InitialArguments = args;

                Program.IsRunningMainLoop = true;
                Application.Run(DisplayHelper);
                Program.IsRunningMainLoop = false;
            }
            catch (Exception ex)
            {
                //If the helper thread aborts the main thread, it also sets IsRunningMainLoop to false
                // and in that case we accept the abort call
                if (ex is System.Threading.ThreadAbortException && !Program.IsRunningMainLoop)
                    System.Threading.Thread.ResetAbort();
                else
                    MessageBox.Show(string.Format(Strings.Program.SeriousError, ex.ToString()), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                //Find logs that are no longer displayed, and delete them
                DataFetcherNested con = new DataFetcherNested(DataConnection);
                foreach (Log x in con.GetObjects<Log>("EndTime < ?", Library.Utility.Timeparser.ParseTimeInterval(new ApplicationSettings(con).RecentBackupDuration, DateTime.Now, true)))
                {
                    if (x.Blob != null) //Load the blob part if required
                        con.DeleteObject(x.Blob);
                    con.DeleteObject(x);
                }

                con.CommitAllRecursive();
            }
            catch
            { }

            try
            {
                //Compact the database
                using (System.Data.IDbCommand vaccum_cmd = DataConnection.Provider.Connection.CreateCommand())
                {
                    vaccum_cmd.CommandText = "VACUUM;";
                    vaccum_cmd.ExecuteNonQuery();
                }
            }
            catch
            { }

            if (Scheduler != null)
                Scheduler.Terminate(true);
            if (WorkThread != null)
                WorkThread.Terminate(true);
            if (SingleInstance != null)
                SingleInstance.Dispose();

            #if DEBUG
            using(XervBackup.Library.Logging.Log.CurrentLog as XervBackup.Library.Logging.StreamLog)
                XervBackup.Library.Logging.Log.CurrentLog = null;
            #endif
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //If we are on Windows, append the bundled "win-tools" programs to the search path
            //We add it last, to allow the user to override with other versions
            if (!Library.Utility.Utility.IsClientLinux)
            {
                string wintools = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "win-tools");
                Environment.SetEnvironmentVariable("PATH",
                                                   Environment.GetEnvironmentVariable("PATH") +
                                                   System.IO.Path.PathSeparator.ToString() +
                                                   wintools +
                                                   System.IO.Path.PathSeparator.ToString() +
                                                   System.IO.Path.Combine(wintools, "gpg") //GPG needs to be in a subfolder for wrapping reasons
                                                   );
            }

            Library.Utility.UrlUtillity.ErrorHandler = new Duplicati.Library.Utility.UrlUtillity.ErrorHandlerDelegate(DisplayURLOpenError);

            //If we are on windows we encrypt the database by default
            //We do not encrypt on Linux as most distros use a SQLite library without encryption support,
            //Linux users can use an encrypted home folder, or install a SQLite library with encryption support
            if (!Library.Utility.Utility.IsClientLinux && string.IsNullOrEmpty(Environment.GetEnvironmentVariable(DB_KEY_ENV_NAME)))
            {
                //Note that the password here is a default password and public knowledge
                //
                //The purpose of this is to prevent casual read of the database, as well
                // as protect from harddisk string scans, not to protect from determined
                // attacks.
                //
                //If you desire better security, start Duplicati once with the commandline option
                // --unencrypted-database to decrypt the database.
                //Then set the environment variable DUPLICATI_DB_KEY to the desired key,
                // and run Duplicati again without the --unencrypted-database option
                // to re-encrypt it with the new key
                //
                //If you change the key, please note that you need to supply the same
                // key when restoring the setup, as the setup being backed up will
                // be encrypted as well.
                Environment.SetEnvironmentVariable(DB_KEY_ENV_NAME, "Duplicati_Key_42");
            }


            //Find commandline options here for handling special startup cases
            Dictionary <string, string> commandlineOptions = CommandLine.CommandLineParser.ExtractOptions(new List <string>(args));

            foreach (string s in args)
            {
                if (
                    s.Equals("help", StringComparison.InvariantCultureIgnoreCase) ||
                    s.Equals("/help", StringComparison.InvariantCultureIgnoreCase) ||
                    s.Equals("usage", StringComparison.InvariantCultureIgnoreCase) ||
                    s.Equals("/usage", StringComparison.InvariantCultureIgnoreCase))
                {
                    commandlineOptions["help"] = "";
                }
            }

            //If the commandline issues --help, just stop here
            if (commandlineOptions.ContainsKey("help"))
            {
                List <string> lines = new List <string>();
                foreach (Library.Interface.ICommandLineArgument arg in SupportedCommands)
                {
                    lines.Add(string.Format(Strings.Program.HelpDisplayFormat, arg.Name, arg.LongDescription));
                }
                MessageBox.Show(string.Format(Strings.Program.HelpDisplayDialog, string.Join(Environment.NewLine, lines.ToArray())), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (commandlineOptions.ContainsKey("trayless"))
            {
                Program.TraylessMode = Library.Utility.Utility.ParseBoolOption(commandlineOptions, "trayless");
            }

#if DEBUG
            //Log various information in the logfile
            if (!commandlineOptions.ContainsKey("log-file"))
            {
                commandlineOptions["log-file"]  = System.IO.Path.Combine(Application.StartupPath, "Duplicati.debug.log");
                commandlineOptions["log-level"] = Duplicati.Library.Logging.LogMessageType.Profiling.ToString();
            }
#endif

            if (commandlineOptions.ContainsKey("log-level"))
            {
                foreach (string s in Enum.GetNames(typeof(Duplicati.Library.Logging.LogMessageType)))
                {
                    if (s.Equals(commandlineOptions["log-level"].Trim(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        Duplicati.Library.Logging.Log.LogLevel = (Duplicati.Library.Logging.LogMessageType)Enum.Parse(typeof(Duplicati.Library.Logging.LogMessageType), s);
                    }
                }
            }

            if (commandlineOptions.ContainsKey("log-file"))
            {
                if (System.IO.File.Exists(commandlineOptions["log-file"]))
                {
                    System.IO.File.Delete(commandlineOptions["log-file"]);
                }
                Duplicati.Library.Logging.Log.CurrentLog = new Duplicati.Library.Logging.StreamLog(commandlineOptions["log-file"]);
            }

            //Set the %DUPLICATI_HOME% env variable, if it is not already set
            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(DATAFOLDER_ENV_NAME)))
            {
#if DEBUG
                //debug mode uses a lock file located in the app folder
                Environment.SetEnvironmentVariable(DATAFOLDER_ENV_NAME, System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
#else
                bool portableMode = Library.Utility.Utility.ParseBoolOption(commandlineOptions, "portable-mode");

                if (portableMode)
                {
                    //Portable mode uses a data folder in the application home dir
                    Environment.SetEnvironmentVariable(DATAFOLDER_ENV_NAME, System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "data"));
                }
                else
                {
                    //Normal release mode uses the systems "Application Data" folder
                    Environment.SetEnvironmentVariable(DATAFOLDER_ENV_NAME, System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.ProductName));
                }
#endif
            }

            try
            {
                try
                {
                    //This will also create Program.DATAFOLDER if it does not exist
                    SingleInstance = new SingleInstance(Application.ProductName, Program.DATAFOLDER);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format(Strings.Program.StartupFailure, ex.ToString()), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (!SingleInstance.IsFirstInstance)
                {
                    //Linux shows this output
                    Console.WriteLine(Strings.Program.AnotherInstanceDetected);
                    return;
                }

                Version sqliteVersion = new Version((string)SQLiteLoader.SQLiteConnectionType.GetProperty("SQLiteVersion").GetValue(null, null));
                if (sqliteVersion < new Version(3, 6, 3))
                {
                    //The official Mono SQLite provider is also broken with less than 3.6.3
                    MessageBox.Show(string.Format(Strings.Program.WrongSQLiteVersion, sqliteVersion, "3.6.3"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //Create the connection instance
                System.Data.IDbConnection con = (System.Data.IDbConnection)Activator.CreateInstance(SQLiteLoader.SQLiteConnectionType);

                try
                {
#if DEBUG
                    //Default is to not use encryption for debugging
                    Program.UseDatabaseEncryption = commandlineOptions.ContainsKey("unencrypted-database") ? !Library.Utility.Utility.ParseBoolOption(commandlineOptions, "unencrypted-database") : false;
#else
                    //Default is to use encryption for release
                    Program.UseDatabaseEncryption = !Library.Utility.Utility.ParseBoolOption(commandlineOptions, "unencrypted-database");
#endif

                    OpenSettingsDatabase(con, Program.DATAFOLDER);
                }
                catch (Exception ex)
                {
                    //Unwrap the reflection exceptions
                    if (ex is System.Reflection.TargetInvocationException && ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }

                    MessageBox.Show(string.Format(Strings.Program.DatabaseOpenError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                DataConnection = new DataFetcherWithRelations(new SQLiteDataProvider(con));

                string displayLanguage = new Datamodel.ApplicationSettings(DataConnection).DisplayLanguage;
                if (!string.IsNullOrEmpty(displayLanguage) && displayLanguage != Library.Utility.Utility.DefaultCulture.Name)
                {
                    try
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture   = System.Globalization.CultureInfo.GetCultureInfo(displayLanguage);
                        System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(displayLanguage);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(string.Format(Strings.Program.LanguageSelectionError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //This is non-fatal, just keep running with system default language
                    }
                }

                try
                {
                    DataFetcherNested logcon = new DataFetcherNested(DataConnection);
                    Log[]             items  = logcon.GetObjects <Log>("Subaction LIKE ?", "InProgress");
                    if (items != null && items.Length > 0)
                    {
                        foreach (Log l in items)
                        {
                            l.SubAction = "Primary";
                        }

                        logcon.CommitAllRecursive();
                    }
                }
                catch
                {
                    //Non-fatal but any interrupted backup will not show
                }

                LiveControl = new LiveControls(new ApplicationSettings(DataConnection));
                LiveControl.StateChanged          += new EventHandler(LiveControl_StateChanged);
                LiveControl.ThreadPriorityChanged += new EventHandler(LiveControl_ThreadPriorityChanged);
                LiveControl.ThrottleSpeedChanged  += new EventHandler(LiveControl_ThrottleSpeedChanged);

                Runner     = new DuplicatiRunner();
                WorkThread = new WorkerThread <IDuplicityTask>(new WorkerThread <IDuplicityTask> .ProcessItemDelegate(Runner.ExecuteTask), LiveControl.State == LiveControls.LiveControlState.Paused);
                Scheduler  = new Scheduler(DataConnection, WorkThread, MainLock);

                DataConnection.AfterDataConnection += new DataConnectionEventHandler(DataConnection_AfterDataConnection);

                DisplayHelper = new MainForm();
                DisplayHelper.InitialArguments = args;

                Program.IsRunningMainLoop = true;
                Application.Run(DisplayHelper);
                Program.IsRunningMainLoop = false;
            }
            catch (Exception ex)
            {
                //If the helper thread aborts the main thread, it also sets IsRunningMainLoop to false
                // and in that case we accept the abort call
                if (ex is System.Threading.ThreadAbortException && !Program.IsRunningMainLoop)
                {
                    System.Threading.Thread.ResetAbort();
                }
                else
                {
                    MessageBox.Show(string.Format(Strings.Program.SeriousError, ex.ToString()), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            try
            {
                //Find logs that are no longer displayed, and delete them
                DataFetcherNested con = new DataFetcherNested(DataConnection);
                foreach (Log x in con.GetObjects <Log>("EndTime < ?", Library.Utility.Timeparser.ParseTimeInterval(new ApplicationSettings(con).RecentBackupDuration, DateTime.Now, true)))
                {
                    if (x.Blob != null) //Load the blob part if required
                    {
                        con.DeleteObject(x.Blob);
                    }
                    con.DeleteObject(x);
                }

                con.CommitAllRecursive();
            }
            catch
            { }

            try
            {
                //Compact the database
                using (System.Data.IDbCommand vaccum_cmd = DataConnection.Provider.Connection.CreateCommand())
                {
                    vaccum_cmd.CommandText = "VACUUM;";
                    vaccum_cmd.ExecuteNonQuery();
                }
            }
            catch
            { }


            if (Scheduler != null)
            {
                Scheduler.Terminate(true);
            }
            if (WorkThread != null)
            {
                WorkThread.Terminate(true);
            }
            if (SingleInstance != null)
            {
                SingleInstance.Dispose();
            }

#if DEBUG
            using (Duplicati.Library.Logging.Log.CurrentLog as Duplicati.Library.Logging.StreamLog)
                Duplicati.Library.Logging.Log.CurrentLog = null;
#endif
        }
Beispiel #3
0
        void m_form_Finished(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Wizard_pages.WizardSettingsWrapper wrapper = new Duplicati.GUI.Wizard_pages.WizardSettingsWrapper(m_form.Settings);

            if (wrapper.PrimayAction == Duplicati.GUI.Wizard_pages.WizardSettingsWrapper.MainAction.Add || wrapper.PrimayAction == Duplicati.GUI.Wizard_pages.WizardSettingsWrapper.MainAction.Edit)
            {
                bool scheduleRun = wrapper.RunImmediately;
                bool autoScheduled = (wrapper.BackupTimeOffset < DateTime.Now) && !string.IsNullOrEmpty(wrapper.RepeatInterval);

                //Resume Duplicati if the backup is supposed to run
                if (scheduleRun || autoScheduled)
                {
                    if (!AskToResumeIfPaused())
                    {
                        e.Cancel = true;
                        return;
                    }
                }

                Schedule schedule;
                IDataFetcherWithRelations con = new DataFetcherNested(Program.DataConnection);

                if (wrapper.PrimayAction == Duplicati.GUI.Wizard_pages.WizardSettingsWrapper.MainAction.Add)
                    schedule = con.Add<Schedule>();
                else
                    schedule = con.GetObjectById<Schedule>(wrapper.ScheduleID);

                wrapper.UpdateSchedule(schedule);

                lock (Program.MainLock)
                    con.CommitAllRecursive();
                schedule = Program.DataConnection.GetObjectById<Schedule>(schedule.ID);

                if (wrapper.UseEncryptionAsDefault)
                {
                    ApplicationSettings appset = new ApplicationSettings(Program.DataConnection);
                    appset.UseCommonPassword = true;
                    appset.CommonPassword = wrapper.BackupPassword;
                    appset.CommonPasswordEncryptionModule = wrapper.EncryptionModule;
                    Program.DataConnection.Commit(Program.DataConnection.GetObjects<ApplicationSetting>());
                }

                //If the user has selected that the backup should run now
                // and it is not run automatically, start it now
                if (scheduleRun && !autoScheduled)
                    Program.WorkThread.AddTask(new IncrementalBackupTask(schedule));

            }
            else if (m_form.CurrentPage is Wizard_pages.Restore.FinishedRestore)
            {
                if (!AskToResumeIfPaused())
                {
                    e.Cancel = true;
                    return;
                }

                Schedule schedule = wrapper.DataConnection.GetObjectById<Schedule>(wrapper.ScheduleID);

                DateTime when = wrapper.RestoreTime;
                string target = wrapper.FullRestorePath;
                string restoreFilter = wrapper.RestoreFilter;

                if (when.Ticks == 0)
                    Program.WorkThread.AddTask(new RestoreTask(schedule, target, restoreFilter));
                else
                    Program.WorkThread.AddTask(new RestoreTask(schedule, target, restoreFilter, when));

                Program.DisplayHelper.ShowStatus();
            }
            else if (m_form.CurrentPage is Wizard_pages.RunNow.RunNowFinished)
            {
                if (!AskToResumeIfPaused())
                {
                    e.Cancel = true;
                    return;
                }

                Schedule schedule = wrapper.DataConnection.GetObjectById<Schedule>(wrapper.ScheduleID);
                if (wrapper.ForceFull)
                    Program.WorkThread.AddTask(new FullBackupTask(schedule));
                else
                    Program.WorkThread.AddTask(new IncrementalBackupTask(schedule));

            }
            else if (m_form.CurrentPage is Wizard_pages.Delete_backup.DeleteFinished)
            {
                Schedule schedule = wrapper.DataConnection.GetObjectById<Schedule>(wrapper.ScheduleID);

                if (Program.WorkThread.Active)
                {
                    try
                    {
                        //TODO: It's not safe to access the values like this,
                        //because the runner thread might interfere
                        if (Program.WorkThread.CurrentTask.Schedule.ID == schedule.ID)
                        {
                            bool paused = Program.LiveControl.State == LiveControls.LiveControlState.Paused;
                            Program.LiveControl.Pause();
                            if (MessageBox.Show(m_form.Dialog, Strings.WizardHandler.StopRunningBackupQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes)
                            {
                                e.Cancel = true;
                                if (!paused)
                                    Program.LiveControl.Resume();
                                return;
                            }

                            bool repaused = Program.LiveControl.State == LiveControls.LiveControlState.Paused;
                            Program.LiveControl.Pause();
                            if (Program.WorkThread.CurrentTask.Schedule.ID == schedule.ID)
                                Program.Runner.Terminate();

                            Cursor prevCursor = m_form.Dialog.Cursor;

                            try
                            {
                                m_form.Dialog.Cursor = Cursors.WaitCursor;
                                for (int i = 0; i < 10; i++)
                                    if (Program.WorkThread.Active)
                                    {
                                        IDuplicityTask t = Program.WorkThread.CurrentTask;
                                        if (t != null && t.Schedule.ID == schedule.ID)
                                            System.Threading.Thread.Sleep(1000);
                                        else
                                            break;
                                    }
                                    else
                                        break;
                            }
                            finally
                            {
                                try { m_form.Dialog.Cursor = prevCursor; }
                                catch { }
                            }

                            if (Program.WorkThread.Active)
                            {
                                IDuplicityTask t = Program.WorkThread.CurrentTask;
                                if (t == null && t.Schedule.ID == schedule.ID)
                                {
                                    MessageBox.Show(m_form.Dialog, Strings.WizardHandler.UnableToStopBackupError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    e.Cancel = true;
                                    return;
                                }
                            }

                            if (!paused || !repaused)
                                Program.LiveControl.Resume();
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(m_form.Dialog, string.Format(Strings.WizardHandler.StopBackupError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        e.Cancel = true;
                        return;
                    }
                }

                //Prior to deleting we need to make sure all relations are loaded
                Queue<object> unvisited = new Queue<object>();
                Dictionary<object, object> visited = new Dictionary<object, object>();

                //Set up a transaction we can safely work on
                System.Data.LightDatamodel.DataFetcherNested con = new DataFetcherNested(Program.DataConnection);

                //Load the schedule in the transaction context
                object entryItem = con.GetObjectById<Schedule>(schedule.ID);
                unvisited.Enqueue(entryItem);
                visited[entryItem] = null;

                //Traverse the object relations
                while (unvisited.Count > 0)
                {
                    object x = unvisited.Dequeue();
                    foreach (System.Reflection.PropertyInfo pi in x.GetType().GetProperties())
                    {
                        if (pi.PropertyType != typeof(string) && (typeof(System.Data.LightDatamodel.IDataClass).IsAssignableFrom(pi.PropertyType) || typeof(System.Collections.IEnumerable).IsAssignableFrom(pi.PropertyType)))
                            try
                            {
                                object tmp = pi.GetValue(x, null);
                                foreach(object i in tmp as System.Collections.IEnumerable ?? new object[] { tmp })
                                if (i as IDataClass != null && !visited.ContainsKey(i))
                                {
                                    visited[i] = null;
                                    unvisited.Enqueue(i);
                                }
                            }
                            catch
                            {
                                //TODO: Perhaps log this?
                            }
                    }
                }

                //Remove all entries visited
                foreach(IDataClass o in visited.Keys)
                    con.DeleteObject(o);

                //TODO: The worker may schedule the task while we attempt to de-schedule it
                foreach (IDuplicityTask t in Program.WorkThread.CurrentTasks)
                    if (t != null && t is IncrementalBackupTask && ((IncrementalBackupTask)t).Schedule.ID == schedule.ID)
                        Program.WorkThread.RemoveTask(t);

                //Persist to database
                con.CommitAllRecursive();

                //We have fiddled with the schedules
                Program.Scheduler.Reschedule();
            }
            else if (m_form.CurrentPage is Wizard_pages.RestoreSetup.FinishedRestoreSetup)
            {
                MessageBox.Show(m_form as Form, Strings.WizardHandler.SetupRestoreSuccess, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Beispiel #4
0
        void m_form_Finished(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Wizard_pages.WizardSettingsWrapper wrapper = new Duplicati.GUI.Wizard_pages.WizardSettingsWrapper(m_form.Settings);

            if (wrapper.PrimayAction == Duplicati.GUI.Wizard_pages.WizardSettingsWrapper.MainAction.Add || wrapper.PrimayAction == Duplicati.GUI.Wizard_pages.WizardSettingsWrapper.MainAction.Edit)
            {
                bool scheduleRun   = wrapper.RunImmediately;
                bool autoScheduled = (wrapper.BackupTimeOffset < DateTime.Now) && !string.IsNullOrEmpty(wrapper.RepeatInterval);

                //Resume Duplicati if the backup is supposed to run
                if (scheduleRun || autoScheduled)
                {
                    if (!AskToResumeIfPaused())
                    {
                        e.Cancel = true;
                        return;
                    }
                }

                Schedule schedule;
                IDataFetcherWithRelations con = new DataFetcherNested(Program.DataConnection);

                if (wrapper.PrimayAction == Duplicati.GUI.Wizard_pages.WizardSettingsWrapper.MainAction.Add)
                {
                    schedule = con.Add <Schedule>();
                }
                else
                {
                    schedule = con.GetObjectById <Schedule>(wrapper.ScheduleID);
                }

                wrapper.UpdateSchedule(schedule);

                lock (Program.MainLock)
                    con.CommitAllRecursive();
                schedule = Program.DataConnection.GetObjectById <Schedule>(schedule.ID);

                if (wrapper.UseEncryptionAsDefault)
                {
                    ApplicationSettings appset = new ApplicationSettings(Program.DataConnection);
                    appset.UseCommonPassword = true;
                    appset.CommonPassword    = wrapper.BackupPassword;
                    appset.CommonPasswordEncryptionModule = wrapper.EncryptionModule;
                    Program.DataConnection.Commit(Program.DataConnection.GetObjects <ApplicationSetting>());
                }

                //If the user has selected that the backup should run now
                // and it is not run automatically, start it now
                if (scheduleRun && !autoScheduled)
                {
                    Program.WorkThread.AddTask(new IncrementalBackupTask(schedule));
                }
            }
            else if (m_form.CurrentPage is Wizard_pages.Restore.FinishedRestore)
            {
                if (!AskToResumeIfPaused())
                {
                    e.Cancel = true;
                    return;
                }

                Schedule schedule = wrapper.DataConnection.GetObjectById <Schedule>(wrapper.ScheduleID);

                DateTime when          = wrapper.RestoreTime;
                string   target        = wrapper.FullRestorePath;
                string   restoreFilter = wrapper.RestoreFilter;

                if (when.Ticks == 0)
                {
                    Program.WorkThread.AddTask(new RestoreTask(schedule, target, restoreFilter));
                }
                else
                {
                    Program.WorkThread.AddTask(new RestoreTask(schedule, target, restoreFilter, when));
                }

                Program.DisplayHelper.ShowStatus();
            }
            else if (m_form.CurrentPage is Wizard_pages.RunNow.RunNowFinished)
            {
                if (!AskToResumeIfPaused())
                {
                    e.Cancel = true;
                    return;
                }

                Schedule schedule = wrapper.DataConnection.GetObjectById <Schedule>(wrapper.ScheduleID);
                if (wrapper.ForceFull)
                {
                    Program.WorkThread.AddTask(new FullBackupTask(schedule));
                }
                else
                {
                    Program.WorkThread.AddTask(new IncrementalBackupTask(schedule));
                }
            }
            else if (m_form.CurrentPage is Wizard_pages.Delete_backup.DeleteFinished)
            {
                Schedule schedule = wrapper.DataConnection.GetObjectById <Schedule>(wrapper.ScheduleID);

                if (Program.WorkThread.Active)
                {
                    try
                    {
                        //TODO: It's not safe to access the values like this,
                        //because the runner thread might interfere
                        if (Program.WorkThread.CurrentTask.Schedule.ID == schedule.ID)
                        {
                            bool paused = Program.LiveControl.State == LiveControls.LiveControlState.Paused;
                            Program.LiveControl.Pause();
                            if (MessageBox.Show(m_form.Dialog, Strings.WizardHandler.StopRunningBackupQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes)
                            {
                                e.Cancel = true;
                                if (!paused)
                                {
                                    Program.LiveControl.Resume();
                                }
                                return;
                            }

                            bool repaused = Program.LiveControl.State == LiveControls.LiveControlState.Paused;
                            Program.LiveControl.Pause();
                            if (Program.WorkThread.CurrentTask.Schedule.ID == schedule.ID)
                            {
                                Program.Runner.Terminate();
                            }

                            Cursor prevCursor = m_form.Dialog.Cursor;

                            try
                            {
                                m_form.Dialog.Cursor = Cursors.WaitCursor;
                                for (int i = 0; i < 10; i++)
                                {
                                    if (Program.WorkThread.Active)
                                    {
                                        IDuplicityTask t = Program.WorkThread.CurrentTask;
                                        if (t != null && t.Schedule.ID == schedule.ID)
                                        {
                                            System.Threading.Thread.Sleep(1000);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            finally
                            {
                                try { m_form.Dialog.Cursor = prevCursor; }
                                catch { }
                            }

                            if (Program.WorkThread.Active)
                            {
                                IDuplicityTask t = Program.WorkThread.CurrentTask;
                                if (t == null && t.Schedule.ID == schedule.ID)
                                {
                                    MessageBox.Show(m_form.Dialog, Strings.WizardHandler.UnableToStopBackupError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    e.Cancel = true;
                                    return;
                                }
                            }

                            if (!paused || !repaused)
                            {
                                Program.LiveControl.Resume();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(m_form.Dialog, string.Format(Strings.WizardHandler.StopBackupError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        e.Cancel = true;
                        return;
                    }
                }

                //Prior to deleting we need to make sure all relations are loaded
                Queue <object> unvisited            = new Queue <object>();
                Dictionary <object, object> visited = new Dictionary <object, object>();

                //Set up a transaction we can safely work on
                System.Data.LightDatamodel.DataFetcherNested con = new DataFetcherNested(Program.DataConnection);

                //Load the schedule in the transaction context
                object entryItem = con.GetObjectById <Schedule>(schedule.ID);
                unvisited.Enqueue(entryItem);
                visited[entryItem] = null;

                //Traverse the object relations
                while (unvisited.Count > 0)
                {
                    object x = unvisited.Dequeue();
                    foreach (System.Reflection.PropertyInfo pi in x.GetType().GetProperties())
                    {
                        if (pi.PropertyType != typeof(string) && (typeof(System.Data.LightDatamodel.IDataClass).IsAssignableFrom(pi.PropertyType) || typeof(System.Collections.IEnumerable).IsAssignableFrom(pi.PropertyType)))
                        {
                            try
                            {
                                object tmp = pi.GetValue(x, null);
                                foreach (object i in tmp as System.Collections.IEnumerable ?? new object[] { tmp })
                                {
                                    if (i as IDataClass != null && !visited.ContainsKey(i))
                                    {
                                        visited[i] = null;
                                        unvisited.Enqueue(i);
                                    }
                                }
                            }
                            catch
                            {
                                //TODO: Perhaps log this?
                            }
                        }
                    }
                }

                //Remove all entries visited
                foreach (IDataClass o in visited.Keys)
                {
                    con.DeleteObject(o);
                }

                //TODO: The worker may schedule the task while we attempt to de-schedule it
                foreach (IDuplicityTask t in Program.WorkThread.CurrentTasks)
                {
                    if (t != null && t is IncrementalBackupTask && ((IncrementalBackupTask)t).Schedule.ID == schedule.ID)
                    {
                        Program.WorkThread.RemoveTask(t);
                    }
                }

                //Persist to database
                con.CommitAllRecursive();

                //We have fiddled with the schedules
                Program.Scheduler.Reschedule();
            }
            else if (m_form.CurrentPage is Wizard_pages.RestoreSetup.FinishedRestoreSetup)
            {
                MessageBox.Show(m_form as Form, Strings.WizardHandler.SetupRestoreSuccess, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }