changes to AutoBackModule. Store folder and number of days expire is now also only defined in OpenSim.ini and so same for all regions.

0.9.0-post-fixes
UbitUmarov 2017-06-21 13:35:36 +01:00
parent 36442c004f
commit 70da902732
2 changed files with 91 additions and 135 deletions

View File

@ -66,7 +66,10 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
/// if false module is disable and all rest is ignored /// if false module is disable and all rest is ignored
/// AutoBackupInterval: Double, non-negative value. Default: 720 (12 hours). /// AutoBackupInterval: Double, non-negative value. Default: 720 (12 hours).
/// The number of minutes between each backup attempt. /// The number of minutes between each backup attempt.
/// /// AutoBackupDir: String. Default: "." (the current directory).
/// A directory (absolute or relative) where backups should be saved.
/// AutoBackupKeepFilesForDays remove files older than this number of days. 0 disables
///
/// Next can be set on OpenSim.ini, as default, and or per region in Regions.ini /// Next can be set on OpenSim.ini, as default, and or per region in Regions.ini
/// Region-specific settings take precedence. /// Region-specific settings take precedence.
/// ///
@ -86,10 +89,8 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
/// AutoBackupNaming: string. Default: Time. /// AutoBackupNaming: string. Default: Time.
/// One of three strings (case insensitive): /// One of three strings (case insensitive):
/// "Time": Current timestamp is appended to file name. An existing file will never be overwritten. /// "Time": Current timestamp is appended to file name. An existing file will never be overwritten.
/// "Sequential": A number is appended to the file name. So if RegionName_x.oar exists, we'll save to RegionName_{x+1}.oar next. An existing file will never be overwritten. /// "Sequential": A number is appended to the file name. So if RegionName_x.oar exists, we'll save to RegionName_{x+1}.oar next. An existing file will never be overwritten.
/// "Overwrite": Always save to file named "${AutoBackupDir}/RegionName.oar", even if we have to overwrite an existing file. /// "Overwrite": Always save to file named "${AutoBackupDir}/RegionName.oar", even if we have to overwrite an existing file.
/// AutoBackupDir: String. Default: "." (the current directory).
/// A directory (absolute or relative) where backups should be saved.
/// </remarks> /// </remarks>
[Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AutoBackupModule")] [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AutoBackupModule")]
public class AutoBackupModule : ISharedRegionModule public class AutoBackupModule : ISharedRegionModule
@ -106,6 +107,10 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
private List<Scene> m_Scenes = new List<Scene> (); private List<Scene> m_Scenes = new List<Scene> ();
private Timer m_masterTimer; private Timer m_masterTimer;
private bool m_busy; private bool m_busy;
private int m_KeepFilesForDays = -1;
private string m_backupDir;
private bool m_doneFirst;
private double m_baseInterval;
private IConfigSource m_configSource; private IConfigSource m_configSource;
@ -154,15 +159,19 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
if(!m_enabled) if(!m_enabled)
return; return;
m_log.Info("[AUTO BACKUP]: AutoBackupModule enabled"); ParseDefaultConfig(moduleConfig);
m_masterTimer = new Timer(43200000); if(!m_enabled)
m_masterTimer.Elapsed += HandleElapsed; return;
m_masterTimer.AutoReset = false;
ParseDefaultConfig();
m_log.Debug("[AUTO BACKUP]: Default config:"); m_log.Debug("[AUTO BACKUP]: Default config:");
m_log.Debug(m_defaultState.ToString()); m_log.Debug(m_defaultState.ToString());
m_log.Info("[AUTO BACKUP]: AutoBackupModule enabled");
m_masterTimer = new Timer();
m_masterTimer.Interval = m_baseInterval;
m_masterTimer.Elapsed += HandleElapsed;
m_masterTimer.AutoReset = false;
m_console = MainConsole.Instance; m_console = MainConsole.Instance;
m_console.Commands.AddCommand ( m_console.Commands.AddCommand (
@ -251,7 +260,6 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
/// </summary> /// </summary>
void ISharedRegionModule.PostInitialise() void ISharedRegionModule.PostInitialise()
{ {
} }
#endregion #endregion
@ -261,18 +269,19 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
if (!m_enabled) if (!m_enabled)
return; return;
if(m_busy)
{
MainConsole.Instance.OutputFormat ("Already doing a backup, please try later");
return;
}
if (args.Length != 2) if (args.Length != 2)
{ {
MainConsole.Instance.OutputFormat ("Usage: dooarbackup <regionname>"); MainConsole.Instance.OutputFormat ("Usage: dooarbackup <regionname>");
return; return;
} }
if(m_busy)
{
MainConsole.Instance.OutputFormat ("Already doing a backup, please try later");
return;
}
m_masterTimer.Stop();
m_busy = true; m_busy = true;
bool found = false; bool found = false;
@ -289,13 +298,13 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
{ {
if(name == "ALL") if(name == "ALL")
{ {
m_masterTimer.Stop();
for(int i = 0; i < scenes.Length; i++) for(int i = 0; i < scenes.Length; i++)
{ {
s = scenes[i]; s = scenes[i];
DoRegionBackup(s); DoRegionBackup(s);
if (!m_enabled)
return;
} }
m_busy = false;
return; return;
} }
@ -305,37 +314,56 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
if (s.Name == name) if (s.Name == name)
{ {
found = true; found = true;
m_masterTimer.Stop();
DoRegionBackup(s); DoRegionBackup(s);
break; break;
} }
} }
} catch { } }
catch { }
finally
{
if (m_enabled)
m_masterTimer.Start();
m_busy = false;
}
if (!found) if (!found)
MainConsole.Instance.OutputFormat ("No such region {0}. Nothing to backup", name); MainConsole.Instance.OutputFormat ("No such region {0}. Nothing to backup", name);
m_busy = false;
} }
private void ParseDefaultConfig() private void ParseDefaultConfig(IConfig config)
{ {
IConfig config = m_configSource.Configs["AutoBackupModule"];
if (config == null) m_backupDir = ".";
return; string backupDir = config.GetString("AutoBackupDir", ".");
if (backupDir != ".")
{
try
{
DirectoryInfo dirinfo = new DirectoryInfo(backupDir);
if (!dirinfo.Exists)
dirinfo.Create();
}
catch (Exception e)
{
m_enabled = false;
m_log.WarnFormat("[AUTO BACKUP]: Error accessing backup folder {0}. Module disabled. {1}",
backupDir, e);
return;
}
}
m_backupDir = backupDir;
// Borrow an existing timer if one exists for the same interval; otherwise, make a new one.
double interval = config.GetDouble("AutoBackupInterval", 720); double interval = config.GetDouble("AutoBackupInterval", 720);
interval *= 60000.0; interval *= 60000.0;
m_masterTimer.Interval = interval; m_baseInterval = interval;
// How long to keep backup files in days, 0 Disables this feature
m_KeepFilesForDays = config.GetInt("AutoBackupKeepFilesForDays",m_KeepFilesForDays);
m_defaultState.Enabled = config.GetBoolean("AutoBackup", m_defaultState.Enabled); m_defaultState.Enabled = config.GetBoolean("AutoBackup", m_defaultState.Enabled);
// Included Option To Skip Assets
m_defaultState.SkipAssets = config.GetBoolean("AutoBackupSkipAssets",m_defaultState.SkipAssets); m_defaultState.SkipAssets = config.GetBoolean("AutoBackupSkipAssets",m_defaultState.SkipAssets);
// How long to keep backup files in days, 0 Disables this feature
m_defaultState.KeepFilesForDays = config.GetInt("AutoBackupKeepFilesForDays",m_defaultState.KeepFilesForDays);
// Set file naming algorithm // Set file naming algorithm
string stmpNamingType = config.GetString("AutoBackupNaming", m_defaultState.NamingType.ToString()); string stmpNamingType = config.GetString("AutoBackupNaming", m_defaultState.NamingType.ToString());
NamingType tmpNamingType; NamingType tmpNamingType;
@ -354,25 +382,6 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
m_defaultState.Script = config.GetString("AutoBackupScript", m_defaultState.Script); m_defaultState.Script = config.GetString("AutoBackupScript", m_defaultState.Script);
string backupDir = config.GetString("AutoBackupDir", ".");
if (backupDir != ".")
{
try
{
DirectoryInfo dirinfo = new DirectoryInfo(backupDir);
if (!dirinfo.Exists)
dirinfo.Create();
}
catch (Exception e)
{
m_log.Warn(
"[AUTO BACKUP]: BAD NEWS. You won't be able to save backups to directory " +
backupDir +
" because it doesn't exist or there's a permissions issue with it. Here's the exception.",
e);
}
}
m_defaultState.BackupDir = backupDir;
} }
/// <summary> /// <summary>
@ -388,11 +397,9 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return null; return null;
string sRegionName; string sRegionName;
string sRegionLabel;
AutoBackupModuleState state = null; AutoBackupModuleState state = null;
sRegionName = scene.RegionInfo.RegionName; sRegionName = scene.RegionInfo.RegionName;
sRegionLabel = sRegionName;
// Read the config settings and set variables. // Read the config settings and set variables.
IConfig regionConfig = scene.Config.Configs[sRegionName]; IConfig regionConfig = scene.Config.Configs[sRegionName];
@ -406,9 +413,6 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
// Included Option To Skip Assets // Included Option To Skip Assets
state.SkipAssets = regionConfig.GetBoolean("AutoBackupSkipAssets", m_defaultState.SkipAssets); state.SkipAssets = regionConfig.GetBoolean("AutoBackupSkipAssets", m_defaultState.SkipAssets);
// How long to keep backup files in days, 0 Disables this feature
state.KeepFilesForDays = regionConfig.GetInt("AutoBackupKeepFilesForDays", m_defaultState.KeepFilesForDays);
// Set file naming algorithm // Set file naming algorithm
string stmpNamingType = regionConfig.GetString("AutoBackupNaming", m_defaultState.NamingType.ToString()); string stmpNamingType = regionConfig.GetString("AutoBackupNaming", m_defaultState.NamingType.ToString());
NamingType tmpNamingType; NamingType tmpNamingType;
@ -420,58 +424,16 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
tmpNamingType = NamingType.Overwrite; tmpNamingType = NamingType.Overwrite;
else else
{ {
m_log.Warn("Unknown naming type specified for region " + sRegionLabel + ": " + m_log.Warn("Unknown naming type specified for region " + sRegionName + ": " +
stmpNamingType); stmpNamingType);
tmpNamingType = NamingType.Time; tmpNamingType = NamingType.Time;
} }
m_defaultState.NamingType = tmpNamingType; m_defaultState.NamingType = tmpNamingType;
state.Script = regionConfig.GetString("AutoBackupScript", m_defaultState.Script); state.Script = regionConfig.GetString("AutoBackupScript", m_defaultState.Script);
string tmpBackupDir = regionConfig.GetString("AutoBackupDir", ".");
// Let's give the user some convenience and auto-mkdir
if (tmpBackupDir != "." && tmpBackupDir != m_defaultState.BackupDir)
{
try
{
DirectoryInfo dirinfo = new DirectoryInfo(state.BackupDir);
if (!dirinfo.Exists)
{
dirinfo.Create();
}
}
catch (Exception e)
{
m_log.Warn(
"[AUTO BACKUP]: BAD NEWS. You won't be able to save backups to directory " +
state.BackupDir +
" because it doesn't exist or there's a permissions issue with it:",
e);
}
}
state.BackupDir = tmpBackupDir;
return state; return state;
} }
/// <summary>
/// Helper function for ParseConfig.
/// </summary>
/// <param name="settingName"></param>
/// <param name="defaultValue"></param>
/// <param name="global"></param>
/// <param name="local"></param>
/// <returns></returns>
private bool ResolveBoolean(string settingName, bool defaultValue, IConfig global, IConfig local)
{
if(local != null)
{
return local.GetBoolean(settingName, global.GetBoolean(settingName, defaultValue));
}
else
{
return global.GetBoolean(settingName, defaultValue);
}
}
/// <summary> /// <summary>
/// Called when any auto-backup timer expires. This starts the code path for actually performing a backup. /// Called when any auto-backup timer expires. This starts the code path for actually performing a backup.
@ -484,6 +446,9 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return; return;
m_busy = true; m_busy = true;
if(m_doneFirst && m_KeepFilesForDays > 0)
RemoveOldFiles();
foreach (IScene scene in m_Scenes) foreach (IScene scene in m_Scenes)
{ {
if (!m_enabled) if (!m_enabled)
@ -496,6 +461,8 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
m_masterTimer.Start(); m_masterTimer.Start();
m_busy = false; m_busy = false;
} }
m_doneFirst = true;
} }
/// <summary> /// <summary>
@ -526,7 +493,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return; return;
string savePath = BuildOarPath(scene.RegionInfo.RegionName, string savePath = BuildOarPath(scene.RegionInfo.RegionName,
state.BackupDir, m_backupDir,
state.NamingType); state.NamingType);
if (savePath == null) if (savePath == null)
{ {
@ -548,26 +515,32 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
} }
// For the given state, remove backup files older than the states KeepFilesForDays property // For the given state, remove backup files older than the states KeepFilesForDays property
private void RemoveOldFiles(AutoBackupModuleState state) private void RemoveOldFiles()
{ {
// 0 Means Disabled, Keep Files Indefinitely string[] files;
if (state.KeepFilesForDays > 0) try
{ {
string[] files = Directory.GetFiles(state.BackupDir, "*.oar"); files = Directory.GetFiles(m_backupDir, "*.oar");
DateTime CuttOffDate = DateTime.Now.AddDays(0 - state.KeepFilesForDays); }
catch (Exception Ex)
{
m_log.Error("[AUTO BACKUP]: Error reading backup folder " + m_backupDir + ": " + Ex.Message);
return;
}
foreach (string file in files) DateTime CuttOffDate = DateTime.Now.AddDays(-m_KeepFilesForDays);
foreach (string file in files)
{
try
{ {
try FileInfo fi = new FileInfo(file);
{ if (fi.CreationTime < CuttOffDate)
FileInfo fi = new FileInfo(file); fi.Delete();
if (fi.CreationTime < CuttOffDate) }
fi.Delete(); catch (Exception Ex)
} {
catch (Exception Ex) m_log.Error("[AUTO BACKUP]: Error deleting old backup file '" + file + "': " + Ex.Message);
{
m_log.Error("[AUTO BACKUP]: Error deleting old backup file '" + file + "': " + Ex.Message);
}
} }
} }
} }

View File

@ -41,21 +41,17 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
public AutoBackupModuleState() public AutoBackupModuleState()
{ {
Enabled = false; Enabled = false;
BackupDir = ".";
SkipAssets = false; SkipAssets = false;
NamingType = NamingType.Time; NamingType = NamingType.Time;
Script = null; Script = null;
KeepFilesForDays = 0;
} }
public AutoBackupModuleState(AutoBackupModuleState copyFrom) public AutoBackupModuleState(AutoBackupModuleState copyFrom)
{ {
Enabled = copyFrom.Enabled; Enabled = copyFrom.Enabled;
BackupDir = copyFrom.BackupDir;
SkipAssets = copyFrom.SkipAssets; SkipAssets = copyFrom.SkipAssets;
NamingType = copyFrom.NamingType; NamingType = copyFrom.NamingType;
Script = copyFrom.Script; Script = copyFrom.Script;
KeepFilesForDays = copyFrom.KeepFilesForDays;
} }
public bool Enabled public bool Enabled
@ -76,30 +72,17 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
set; set;
} }
public string BackupDir
{
get;
set;
}
public NamingType NamingType public NamingType NamingType
{ {
get; get;
set; set;
} }
public int KeepFilesForDays
{
get;
set;
}
public new string ToString() public new string ToString()
{ {
string retval = ""; string retval = "";
retval += "[AUTO BACKUP]: AutoBackup: " + (Enabled ? "ENABLED" : "DISABLED") + "\n"; retval += "[AUTO BACKUP]: AutoBackup: " + (Enabled ? "ENABLED" : "DISABLED") + "\n";
retval += "[AUTO BACKUP]: Naming Type: " + NamingType.ToString() + "\n"; retval += "[AUTO BACKUP]: Naming Type: " + NamingType.ToString() + "\n";
retval += "[AUTO BACKUP]: Backup Dir: " + BackupDir + "\n";
retval += "[AUTO BACKUP]: Script: " + Script + "\n"; retval += "[AUTO BACKUP]: Script: " + Script + "\n";
return retval; return retval;
} }