From e2aa98fbca889c5e02f17557db6b4e696b0ed5a6 Mon Sep 17 00:00:00 2001 From: Kenneth Hsu Date: Sun, 4 Mar 2018 12:29:56 -0800 Subject: [PATCH] Add option to disable backup when on battery power. This addresses issue #2666. --- Duplicati/Library/Main/Options.cs | 13 ++++++++++++- Duplicati/Library/Main/Strings.cs | 2 ++ Duplicati/Server/Runner.cs | 4 ++-- Duplicati/Server/Scheduler.cs | 15 ++++++++++++++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Duplicati/Library/Main/Options.cs b/Duplicati/Library/Main/Options.cs index f69fdc2b1..f801df4cf 100644 --- a/Duplicati/Library/Main/Options.cs +++ b/Duplicati/Library/Main/Options.cs @@ -257,7 +257,8 @@ namespace Duplicati.Library.Main "compression-extension-file", "full-remote-verification", "disable-synthetic-filelist", - "disable-file-scanner" + "disable-file-scanner", + "disable-on-battery" }; } } @@ -541,6 +542,7 @@ namespace Duplicati.Library.Main new CommandLineArgument("auto-vacuum", CommandLineArgument.ArgumentType.Boolean, Strings.Options.AutoVacuumShort, Strings.Options.AutoVacuumLong, "false"), new CommandLineArgument("disable-file-scanner", CommandLineArgument.ArgumentType.Boolean, Strings.Options.DisablefilescannerShort, Strings.Options.DisablefilescannerLong, "false"), + new CommandLineArgument("disable-on-battery", CommandLineArgument.ArgumentType.Boolean, Strings.Options.DisableOnBatteryShort, Strings.Options.DisableOnBatteryLong, "false"), }); return lst; @@ -1883,6 +1885,15 @@ namespace Duplicati.Library.Main get { return Library.Utility.Utility.ParseBoolOption(m_options, "disable-file-scanner"); } } + /// + /// Gets a flag indicating whether the backup should be disabled when on battery power. + /// + /// true if the backup should be disabled when on battery power; otherwise, false. + public bool DisableOnBattery + { + get { return Library.Utility.Utility.ParseBoolOption(m_options, "disable-on-battery"); } + } + /// /// Gets the threshold for when log data should be cleaned /// diff --git a/Duplicati/Library/Main/Strings.cs b/Duplicati/Library/Main/Strings.cs index 3f8cf00fe..4ebdbb0aa 100644 --- a/Duplicati/Library/Main/Strings.cs +++ b/Duplicati/Library/Main/Strings.cs @@ -242,6 +242,8 @@ namespace Duplicati.Library.Main.Strings public static string AutoVacuumLong { get { return LC.L(@"Some operations that manipulate the local database leave unused entries behind. These entries are not deleted from a hard drive until a VACUUM operation is run. This operation saves disk space in the long run but needs to temporarily create a copy of all valid entries in the database. Setting this to true will allow Duplicati to perform VACUUM operations at its discretion."); } } public static string DisablefilescannerShort { get { return LC.L(@"Disable the read-ahead scanner"); } } public static string DisablefilescannerLong { get { return LC.L(@"When this flag is enabled, the scanner that computes the size of source files is disabled, and instead the reported size is read from the database. Using this flag can speed up the backup by reducing disk access, but will give a less accurate progress indicator."); } } + public static string DisableOnBatteryShort { get { return LC.L("Disable the backup when on battery power"); } } + public static string DisableOnBatteryLong { get { return LC.L("When this flag is enabled, a scheduled backup will not run if the system is detected to be running on battery power (manual or command line backups will still be run). If the detected power source is mains (i.e., AC) or unknown, then scheduled backups will proceed as normal."); } } public static string LogfileloglevelLong { get { return LC.L(@"Specifies the amount of log information to write into the file specified by --log-file"); } } public static string LogfileloglevelShort { get { return LC.L(@"Log file information level"); } } diff --git a/Duplicati/Server/Runner.cs b/Duplicati/Server/Runner.cs index d7359bebf..ec910d2fa 100644 --- a/Duplicati/Server/Runner.cs +++ b/Duplicati/Server/Runner.cs @@ -843,7 +843,7 @@ namespace Duplicati.Server options["disable-module"] = string.Join(",", mods.Union(new string[] { module }).Distinct(StringComparer.OrdinalIgnoreCase)); } - private static Dictionary ApplyOptions(Duplicati.Server.Serialization.Interface.IBackup backup, DuplicatiOperation mode, Dictionary options) + internal static Dictionary ApplyOptions(Duplicati.Server.Serialization.Interface.IBackup backup, DuplicatiOperation mode, Dictionary options) { options["backup-name"] = backup.Name; options["dbpath"] = backup.DBPath; @@ -891,7 +891,7 @@ namespace Duplicati.Server return filter; } - private static Dictionary GetCommonOptions(Duplicati.Server.Serialization.Interface.IBackup backup, DuplicatiOperation mode) + internal static Dictionary GetCommonOptions(Duplicati.Server.Serialization.Interface.IBackup backup, DuplicatiOperation mode) { return (from n in Program.DataConnection.Settings diff --git a/Duplicati/Server/Scheduler.cs b/Duplicati/Server/Scheduler.cs index 41325f70a..44b53ed8d 100644 --- a/Duplicati/Server/Scheduler.cs +++ b/Duplicati/Server/Scheduler.cs @@ -34,6 +34,8 @@ namespace Duplicati.Server /// public class Scheduler { + private static readonly string LOGTAG = Duplicati.Library.Logging.Log.LogTagFromType(); + /// /// The thread that runs the scheduler /// @@ -278,7 +280,18 @@ namespace Duplicati.Server { var entry = Program.DataConnection.GetBackup(id); if (entry != null) - jobsToRun.Add(Server.Runner.CreateTask(Duplicati.Server.Serialization.DuplicatiOperation.Backup, entry)); + { + Dictionary options = Duplicati.Server.Runner.GetCommonOptions(entry, Duplicati.Server.Serialization.DuplicatiOperation.Backup); + Duplicati.Server.Runner.ApplyOptions(entry, Duplicati.Server.Serialization.DuplicatiOperation.Backup, options); + if ((new Duplicati.Library.Main.Options(options)).DisableOnBattery && (Duplicati.Library.Utility.Power.PowerSupply.GetSource() == Duplicati.Library.Utility.Power.PowerSupply.Source.Battery)) + { + Duplicati.Library.Logging.Log.WriteInformationMessage(LOGTAG, "BackupDisabledOnBattery", "Scheduled backup disabled while on battery power."); + } + else + { + jobsToRun.Add(Server.Runner.CreateTask(Duplicati.Server.Serialization.DuplicatiOperation.Backup, entry)); + } + } } }