2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
// any third-party rights.
|
|
|
|
//
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-01-26 11:17:12 +00:00
|
|
|
using ConfigurationManager = System.Configuration.ConfigurationManager;
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
namespace ASC.Data.Backup;
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
public class DbBackupProvider : IBackupProvider
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
public string Name => "databases";
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
private readonly List<string> _processedTables = new List<string>();
|
|
|
|
private readonly DbHelper _dbHelper;
|
|
|
|
private readonly TempStream _tempStream;
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public DbBackupProvider(DbHelper dbHelper, TempStream tempStream)
|
|
|
|
{
|
|
|
|
_dbHelper = dbHelper;
|
|
|
|
_tempStream = tempStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
public event EventHandler<ProgressChangedEventArgs> ProgressChanged;
|
2022-02-09 16:29:19 +00:00
|
|
|
|
2022-10-27 10:49:42 +00:00
|
|
|
public Task<IEnumerable<XElement>> GetElements(int tenant, string[] configs, IDataWriteOperator writer)
|
2022-02-09 18:49:52 +00:00
|
|
|
{
|
|
|
|
_processedTables.Clear();
|
|
|
|
var xml = new List<XElement>();
|
|
|
|
var connectionKeys = new Dictionary<string, string>();
|
2022-02-09 16:29:19 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
foreach (var connectionString in GetConnectionStrings(configs))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
//do not save the base, having the same provider and connection string is not to duplicate
|
|
|
|
//data, but also expose the ref attribute of repetitive bases for the correct recovery
|
|
|
|
var node = new XElement(connectionString.Name);
|
|
|
|
xml.Add(node);
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-04-14 19:42:15 +00:00
|
|
|
var connectionKey = connectionString.ProviderName + connectionString.ConnectionString;
|
|
|
|
if (connectionKeys.TryGetValue(connectionKey, out var value))
|
|
|
|
{
|
|
|
|
node.Add(new XAttribute("ref", value));
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
else
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
connectionKeys.Add(connectionKey, connectionString.Name);
|
|
|
|
node.Add(BackupDatabase(tenant, connectionString, writer));
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-27 10:49:42 +00:00
|
|
|
|
|
|
|
return Task.FromResult(xml.AsEnumerable());
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 10:49:42 +00:00
|
|
|
public Task LoadFrom(IEnumerable<XElement> elements, int tenant, string[] configs, IDataReadOperator reader)
|
2022-02-09 18:49:52 +00:00
|
|
|
{
|
|
|
|
_processedTables.Clear();
|
|
|
|
|
|
|
|
foreach (var connectionString in GetConnectionStrings(configs))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
RestoreDatabase(connectionString, elements, reader);
|
2022-10-27 10:49:42 +00:00
|
|
|
}
|
|
|
|
return Task.CompletedTask;
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<ConnectionStringSettings> GetConnectionStrings(string[] configs)
|
|
|
|
{
|
|
|
|
/* if (configs.Length == 0)
|
|
|
|
{
|
|
|
|
configs = new string[] { AppDomain.CurrentDomain.SetupInformation.ConfigurationFile };
|
|
|
|
}
|
|
|
|
var connectionStrings = new List<ConnectionStringSettings>();
|
|
|
|
foreach (var config in configs)
|
|
|
|
{
|
|
|
|
connectionStrings.AddRange(GetConnectionStrings(GetConfiguration(config)));
|
|
|
|
}
|
|
|
|
return connectionStrings.GroupBy(cs => cs.Name).Select(g => g.First());*/
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public IEnumerable<ConnectionStringSettings> GetConnectionStrings(Configuration cfg)
|
|
|
|
{
|
|
|
|
var connectionStrings = new List<ConnectionStringSettings>();
|
|
|
|
foreach (ConnectionStringSettings connectionString in cfg.ConnectionStrings.ConnectionStrings)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
if (connectionString.Name == "LocalSqlServer" || connectionString.Name == "readonly")
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
continue;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
connectionStrings.Add(connectionString);
|
|
|
|
if (connectionString.ConnectionString.Contains("|DataDirectory|"))
|
|
|
|
{
|
|
|
|
connectionString.ConnectionString = connectionString.ConnectionString.Replace("|DataDirectory|", Path.GetDirectoryName(cfg.FilePath) + '\\');
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
return connectionStrings;
|
|
|
|
}
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
private void OnProgressChanged(string status, int progress)
|
|
|
|
{
|
|
|
|
ProgressChanged?.Invoke(this, new ProgressChangedEventArgs(status, progress));
|
|
|
|
}
|
|
|
|
|
|
|
|
private Configuration GetConfiguration(string config)
|
|
|
|
{
|
|
|
|
if (config.Contains(Path.DirectorySeparatorChar) && !Uri.IsWellFormedUriString(config, UriKind.Relative))
|
2022-02-09 18:33:50 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var map = new ExeConfigurationFileMap
|
2022-02-09 18:33:50 +00:00
|
|
|
{
|
2022-02-15 16:56:43 +00:00
|
|
|
ExeConfigFilename = string.Equals(Path.GetExtension(config), ".config", StringComparison.OrdinalIgnoreCase) ? config : CrossPlatform.PathCombine(config, "Web.config")
|
2022-02-09 18:49:52 +00:00
|
|
|
};
|
|
|
|
return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
|
2022-02-09 18:33:50 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
return ConfigurationManager.OpenExeConfiguration(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<XElement> BackupDatabase(int tenant, ConnectionStringSettings connectionString, IDataWriteOperator writer)
|
|
|
|
{
|
|
|
|
var xml = new List<XElement>();
|
|
|
|
var errors = 0;
|
|
|
|
var timeout = TimeSpan.FromSeconds(1);
|
|
|
|
var tables = _dbHelper.GetTables();
|
|
|
|
|
|
|
|
for (var i = 0; i < tables.Count; i++)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var table = tables[i];
|
|
|
|
OnProgressChanged(table, (int)(i / (double)tables.Count * 100));
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (_processedTables.Contains(table, StringComparer.InvariantCultureIgnoreCase))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-04-14 19:42:15 +00:00
|
|
|
xml.Add(new XElement(table));
|
|
|
|
DataTable dataTable;
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
2020-05-28 13:28:51 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
dataTable = _dbHelper.GetTable(table, tenant);
|
|
|
|
break;
|
2020-05-28 13:28:51 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
catch
|
2020-05-28 13:28:51 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
errors++;
|
2022-03-17 15:01:39 +00:00
|
|
|
if (20 < errors)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
Thread.Sleep(timeout);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
foreach (DataColumn c in dataTable.Columns)
|
|
|
|
{
|
|
|
|
if (c.DataType == typeof(DateTime))
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
c.DateTimeMode = DataSetDateTime.Unspecified;
|
2020-05-28 13:28:51 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
using (var file = _tempStream.Create())
|
|
|
|
{
|
2022-04-14 19:42:15 +00:00
|
|
|
dataTable.WriteXml(file, XmlWriteMode.WriteSchema);
|
|
|
|
writer.WriteEntry($"{Name}\\{connectionString.Name}\\{table}".ToLower(), file);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
_processedTables.Add(table);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RestoreDatabase(ConnectionStringSettings connectionString, IEnumerable<XElement> elements, IDataReadOperator reader)
|
|
|
|
{
|
2022-04-14 19:42:15 +00:00
|
|
|
var dbName = connectionString.Name;
|
|
|
|
var dbElement = elements.SingleOrDefault(e => string.Equals(e.Name.LocalName, connectionString.Name, StringComparison.OrdinalIgnoreCase));
|
2022-02-09 18:49:52 +00:00
|
|
|
if (dbElement != null && dbElement.Attribute("ref") != null)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-04-14 19:42:15 +00:00
|
|
|
dbName = dbElement.Attribute("ref").Value;
|
|
|
|
dbElement = elements.Single(e => string.Equals(e.Name.LocalName, dbElement.Attribute("ref").Value, StringComparison.OrdinalIgnoreCase));
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (dbElement == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
var tables = _dbHelper.GetTables();
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
for (var i = 0; i < tables.Count; i++)
|
|
|
|
{
|
|
|
|
var table = tables[i];
|
|
|
|
OnProgressChanged(table, (int)(i / (double)tables.Count * 100));
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (_processedTables.Contains(table, StringComparer.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (dbElement.Element(table) != null)
|
2022-04-14 19:42:15 +00:00
|
|
|
{
|
|
|
|
using (var stream = reader.GetEntry($"{Name}\\{dbName}\\{table}".ToLower()))
|
2020-05-26 08:52:47 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var data = new DataTable();
|
|
|
|
data.ReadXml(stream);
|
|
|
|
_dbHelper.SetTable(data);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
_processedTables.Add(table);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|