2020-05-20 15:14:44 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2020
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml.Linq;
|
2020-06-08 10:40:26 +00:00
|
|
|
|
|
|
|
using ASC.Common;
|
2020-05-20 15:14:44 +00:00
|
|
|
using ASC.Common.Caching;
|
|
|
|
using ASC.Common.Logging;
|
2021-04-16 13:26:26 +00:00
|
|
|
using ASC.Common.Utils;
|
2020-05-20 15:14:44 +00:00
|
|
|
using ASC.Core;
|
|
|
|
using ASC.Core.Billing;
|
|
|
|
using ASC.Core.Tenants;
|
|
|
|
using ASC.Data.Backup.Extensions;
|
|
|
|
using ASC.Data.Backup.Tasks.Modules;
|
2020-06-15 08:19:40 +00:00
|
|
|
using ASC.Data.Storage;
|
|
|
|
|
2020-05-20 15:14:44 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
namespace ASC.Data.Backup.Tasks
|
2020-10-19 15:53:15 +00:00
|
|
|
{
|
|
|
|
[Scope]
|
2020-05-20 15:14:44 +00:00
|
|
|
public class RestorePortalTask : PortalTaskBase
|
|
|
|
{
|
2020-06-10 12:35:10 +00:00
|
|
|
private ColumnMapper ColumnMapper { get; set; }
|
2020-05-20 15:14:44 +00:00
|
|
|
|
|
|
|
public string BackupFilePath { get; private set; }
|
|
|
|
public string UpgradesPath { get; private set; }
|
|
|
|
public bool UnblockPortalAfterCompleted { get; set; }
|
|
|
|
public bool ReplaceDate { get; set; }
|
|
|
|
public bool Dump { get; set; }
|
2020-06-08 10:40:26 +00:00
|
|
|
private CoreBaseSettings CoreBaseSettings { get; set; }
|
2020-09-15 15:16:37 +00:00
|
|
|
private LicenseReader LicenseReader { get; set; }
|
|
|
|
public TenantManager TenantManager { get; }
|
2020-06-08 10:40:26 +00:00
|
|
|
private AscCacheNotify AscCacheNotify { get; set; }
|
|
|
|
private IOptionsMonitor<ILog> Options { get; set; }
|
|
|
|
|
2020-09-15 15:16:37 +00:00
|
|
|
public RestorePortalTask(
|
|
|
|
DbFactory dbFactory,
|
|
|
|
IOptionsMonitor<ILog> options,
|
|
|
|
StorageFactory storageFactory,
|
|
|
|
StorageFactoryConfig storageFactoryConfig,
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
LicenseReader licenseReader,
|
|
|
|
TenantManager tenantManager,
|
|
|
|
AscCacheNotify ascCacheNotify,
|
|
|
|
ModuleProvider moduleProvider)
|
2020-06-10 12:35:10 +00:00
|
|
|
: base(dbFactory, options, storageFactory, storageFactoryConfig, moduleProvider)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2020-06-08 10:40:26 +00:00
|
|
|
CoreBaseSettings = coreBaseSettings;
|
2020-09-15 15:16:37 +00:00
|
|
|
LicenseReader = licenseReader;
|
|
|
|
TenantManager = tenantManager;
|
2020-06-08 10:40:26 +00:00
|
|
|
AscCacheNotify = ascCacheNotify;
|
|
|
|
Options = options;
|
2020-06-15 08:19:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 10:40:26 +00:00
|
|
|
public void Init(string toConfigPath, string fromFilePath, int tenantId = -1, ColumnMapper columnMapper = null, string upgradesPath = null)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
|
|
|
if (fromFilePath == null)
|
|
|
|
throw new ArgumentNullException("fromFilePath");
|
|
|
|
|
|
|
|
if (!File.Exists(fromFilePath))
|
|
|
|
throw new FileNotFoundException("file not found at given path");
|
|
|
|
|
|
|
|
BackupFilePath = fromFilePath;
|
|
|
|
UpgradesPath = upgradesPath;
|
2020-06-10 12:35:10 +00:00
|
|
|
ColumnMapper = columnMapper ?? new ColumnMapper();
|
2020-06-08 10:40:26 +00:00
|
|
|
Init(tenantId, toConfigPath);
|
2020-06-15 08:19:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 15:14:44 +00:00
|
|
|
public override void RunJob()
|
|
|
|
{
|
|
|
|
Logger.Debug("begin restore portal");
|
|
|
|
|
|
|
|
Logger.Debug("begin restore data");
|
|
|
|
|
|
|
|
using (var dataReader = new ZipReadOperator(BackupFilePath))
|
|
|
|
{
|
|
|
|
using (var entry = dataReader.GetEntry(KeyHelper.GetDumpKey()))
|
|
|
|
{
|
2020-06-08 10:40:26 +00:00
|
|
|
Dump = entry != null && CoreBaseSettings.Standalone;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Dump)
|
|
|
|
{
|
2020-06-10 12:35:10 +00:00
|
|
|
RestoreFromDump(dataReader);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var modulesToProcess = GetModulesToProcess().ToList();
|
|
|
|
SetStepsCount(ProcessStorage ? modulesToProcess.Count + 1 : modulesToProcess.Count);
|
|
|
|
|
|
|
|
foreach (var module in modulesToProcess)
|
|
|
|
{
|
2020-06-10 12:35:10 +00:00
|
|
|
var restoreTask = new RestoreDbModuleTask(Options, module, dataReader, ColumnMapper, DbFactory, ReplaceDate, Dump, StorageFactory, StorageFactoryConfig, ModuleProvider);
|
2020-05-20 15:14:44 +00:00
|
|
|
restoreTask.ProgressChanged += (sender, args) => SetCurrentStepProgress(args.Progress);
|
|
|
|
foreach (var tableName in IgnoredTables)
|
|
|
|
{
|
|
|
|
restoreTask.IgnoreTable(tableName);
|
|
|
|
}
|
|
|
|
restoreTask.RunJob();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Debug("end restore data");
|
|
|
|
|
|
|
|
if (ProcessStorage)
|
2020-09-15 15:16:37 +00:00
|
|
|
{
|
|
|
|
if (CoreBaseSettings.Standalone)
|
|
|
|
{
|
|
|
|
Logger.Debug("clear cache");
|
|
|
|
AscCacheNotify.ClearCache();
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:14:44 +00:00
|
|
|
DoRestoreStorage(dataReader);
|
|
|
|
}
|
|
|
|
if (UnblockPortalAfterCompleted)
|
|
|
|
{
|
2020-06-10 12:35:10 +00:00
|
|
|
SetTenantActive(ColumnMapper.GetTenantMapping());
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:40:26 +00:00
|
|
|
if (CoreBaseSettings.Standalone)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
|
|
|
Logger.Debug("refresh license");
|
|
|
|
try
|
|
|
|
{
|
2020-06-08 10:40:26 +00:00
|
|
|
LicenseReader.RejectLicense();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.Error(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Debug("clear cache");
|
2020-06-08 10:40:26 +00:00
|
|
|
AscCacheNotify.ClearCache();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Debug("end restore portal");
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:35:10 +00:00
|
|
|
private void RestoreFromDump(IDataReadOperator dataReader)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
|
|
|
var keyBase = KeyHelper.GetDatabaseSchema();
|
2021-05-17 18:27:39 +00:00
|
|
|
var keys = dataReader.GetEntries(keyBase).Select(r => Path.GetFileName(r)).ToList();
|
2020-05-20 15:14:44 +00:00
|
|
|
var upgrades = new List<string>();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(UpgradesPath) && Directory.Exists(UpgradesPath))
|
|
|
|
{
|
|
|
|
upgrades = Directory.GetFiles(UpgradesPath).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
var stepscount = keys.Count * 2 + upgrades.Count;
|
|
|
|
|
2020-09-15 15:16:37 +00:00
|
|
|
SetStepsCount(ProcessStorage ? stepscount + 1 : stepscount);
|
|
|
|
|
|
|
|
if (ProcessStorage)
|
|
|
|
{
|
|
|
|
var storageModules = StorageFactoryConfig.GetModuleList(ConfigPath).Where(IsStorageModuleAllowed);
|
|
|
|
var tenants = TenantManager.GetTenants(false);
|
|
|
|
|
|
|
|
stepscount += storageModules.Count() * tenants.Count;
|
|
|
|
|
|
|
|
SetStepsCount(stepscount + 1);
|
|
|
|
|
|
|
|
DoDeleteStorage(storageModules, tenants);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetStepsCount(stepscount);
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < keys.Count; i += TasksLimit)
|
|
|
|
{
|
|
|
|
var tasks = new List<Task>(TasksLimit * 2);
|
|
|
|
|
|
|
|
for (var j = 0; j < TasksLimit && i + j < keys.Count; j++)
|
|
|
|
{
|
2021-05-17 18:27:39 +00:00
|
|
|
var key1 = Path.Combine(KeyHelper.GetDatabaseSchema(), keys[i + j]);
|
2020-05-20 15:14:44 +00:00
|
|
|
tasks.Add(RestoreFromDumpFile(dataReader, key1).ContinueWith(r => RestoreFromDumpFile(dataReader, KeyHelper.GetDatabaseData(key1.Substring(keyBase.Length + 1)))));
|
|
|
|
}
|
|
|
|
|
|
|
|
Task.WaitAll(tasks.ToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
var comparer = new SqlComparer();
|
|
|
|
foreach (var u in upgrades.OrderBy(Path.GetFileName, comparer))
|
|
|
|
{
|
2020-06-10 12:35:10 +00:00
|
|
|
RunMysqlFile(u, true);
|
2020-05-20 15:14:44 +00:00
|
|
|
SetStepCompleted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task RestoreFromDumpFile(IDataReadOperator dataReader, string fileName)
|
|
|
|
{
|
|
|
|
Logger.DebugFormat("Restore from {0}", fileName);
|
|
|
|
using (var stream = dataReader.GetEntry(fileName))
|
|
|
|
{
|
|
|
|
await RunMysqlFile(stream);
|
|
|
|
}
|
|
|
|
SetStepCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SqlComparer : IComparer<string>
|
|
|
|
{
|
|
|
|
public int Compare(string x, string y)
|
|
|
|
{
|
|
|
|
if (x == y)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(x))
|
|
|
|
{
|
|
|
|
var splittedX = x.Split('.');
|
|
|
|
if (splittedX.Length <= 2) return -1;
|
|
|
|
if (splittedX[1] == "upgrade") return 1;
|
|
|
|
|
|
|
|
if (splittedX[1].StartsWith("upgrade") && !string.IsNullOrEmpty(y))
|
|
|
|
{
|
|
|
|
var splittedY = y.Split('.');
|
|
|
|
if (splittedY.Length <= 2) return 1;
|
|
|
|
if (splittedY[1] == "upgrade") return -1;
|
|
|
|
|
|
|
|
if (splittedY[1].StartsWith("upgrade"))
|
|
|
|
{
|
|
|
|
return string.Compare(x, y, StringComparison.Ordinal);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Compare(x, y, StringComparison.Ordinal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DoRestoreStorage(IDataReadOperator dataReader)
|
|
|
|
{
|
|
|
|
Logger.Debug("begin restore storage");
|
|
|
|
|
|
|
|
var fileGroups = GetFilesToProcess(dataReader).GroupBy(file => file.Module).ToList();
|
|
|
|
var groupsProcessed = 0;
|
|
|
|
foreach (var group in fileGroups)
|
|
|
|
{
|
|
|
|
foreach (var file in group)
|
|
|
|
{
|
2020-06-10 12:35:10 +00:00
|
|
|
var storage = StorageFactory.GetStorage(ConfigPath, Dump ? file.Tenant.ToString() : ColumnMapper.GetTenantMapping().ToString(), group.Key);
|
2020-05-20 15:14:44 +00:00
|
|
|
var quotaController = storage.QuotaController;
|
2020-10-12 19:39:23 +00:00
|
|
|
storage.SetQuotaController(null);
|
2020-05-20 15:14:44 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var adjustedPath = file.Path;
|
2020-06-08 10:40:26 +00:00
|
|
|
var module = ModuleProvider.GetByStorageModule(file.Module, file.Domain);
|
2020-06-10 12:35:10 +00:00
|
|
|
if (module == null || module.TryAdjustFilePath(Dump, ColumnMapper, ref adjustedPath))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
|
|
|
var key = file.GetZipKey();
|
|
|
|
if (Dump)
|
|
|
|
{
|
2021-04-16 13:26:26 +00:00
|
|
|
key = CrossPlatform.PathCombine(KeyHelper.GetStorage(), key);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2020-09-29 10:28:03 +00:00
|
|
|
using var stream = dataReader.GetEntry(key);
|
|
|
|
try
|
|
|
|
{
|
2020-10-12 19:39:23 +00:00
|
|
|
storage.Save(file.Domain, adjustedPath, module != null ? module.PrepareData(key, stream, ColumnMapper) : stream);
|
2020-09-29 10:28:03 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
Logger.WarnFormat("can't restore file ({0}:{1}): {2}", file.Module, file.Path, error);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (quotaController != null)
|
|
|
|
{
|
2020-10-12 19:39:23 +00:00
|
|
|
storage.SetQuotaController(quotaController);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetCurrentStepProgress((int)(++groupsProcessed * 100 / (double)fileGroups.Count));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileGroups.Count == 0)
|
|
|
|
{
|
|
|
|
SetStepCompleted();
|
|
|
|
}
|
|
|
|
Logger.Debug("end restore storage");
|
2020-09-15 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void DoDeleteStorage(IEnumerable<string> storageModules, IEnumerable<Tenant> tenants)
|
|
|
|
{
|
|
|
|
Logger.Debug("begin delete storage");
|
|
|
|
|
|
|
|
foreach (var tenant in tenants)
|
|
|
|
{
|
|
|
|
foreach (var module in storageModules)
|
|
|
|
{
|
|
|
|
var storage = StorageFactory.GetStorage(ConfigPath, tenant.TenantId.ToString(), module);
|
|
|
|
var domains = StorageFactoryConfig.GetDomainList(ConfigPath, module).ToList();
|
|
|
|
|
|
|
|
domains.Add(string.Empty); //instead storage.DeleteFiles("\\", "*.*", true);
|
|
|
|
|
|
|
|
foreach (var domain in domains)
|
|
|
|
{
|
|
|
|
ActionInvoker.Try(
|
|
|
|
state =>
|
|
|
|
{
|
|
|
|
if (storage.IsDirectory((string)state))
|
|
|
|
{
|
|
|
|
storage.DeleteFiles((string)state, "\\", "*.*", true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
domain,
|
|
|
|
5,
|
|
|
|
onFailure: error => Logger.WarnFormat("Can't delete files for domain {0}: \r\n{1}", domain, error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStepCompleted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Debug("end delete storage");
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 12:35:10 +00:00
|
|
|
private IEnumerable<BackupFileInfo> GetFilesToProcess(IDataReadOperator dataReader)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2020-09-29 10:28:03 +00:00
|
|
|
using var stream = dataReader.GetEntry(KeyHelper.GetStorageRestoreInfoZipKey());
|
|
|
|
if (stream == null)
|
|
|
|
{
|
|
|
|
return Enumerable.Empty<BackupFileInfo>();
|
|
|
|
}
|
|
|
|
var restoreInfo = XElement.Load(new StreamReader(stream));
|
|
|
|
return restoreInfo.Elements("file").Select(BackupFileInfo.FromXElement).ToList();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 12:35:10 +00:00
|
|
|
private void SetTenantActive(int tenantId)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2020-09-29 10:28:03 +00:00
|
|
|
using var connection = DbFactory.OpenConnection();
|
|
|
|
var commandText = string.Format(
|
|
|
|
"update tenants_tenants " +
|
|
|
|
"set " +
|
|
|
|
" status={0}, " +
|
|
|
|
" last_modified='{1}', " +
|
|
|
|
" statuschanged='{1}' " +
|
|
|
|
"where id = '{2}'",
|
|
|
|
(int)TenantStatus.Active,
|
|
|
|
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
|
|
tenantId);
|
|
|
|
|
2020-10-12 19:39:23 +00:00
|
|
|
connection.CreateCommand().WithTimeout(120).ExecuteNonQuery();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|