DocSpace-buildtools/common/services/ASC.Data.Backup/Service/BackupService.cs

311 lines
12 KiB
C#
Raw Normal View History

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;
2020-05-29 14:48:27 +00:00
using System.ServiceModel;
using ASC.Common;
2020-06-15 15:33:53 +00:00
using ASC.Common.Caching;
2020-05-20 15:14:44 +00:00
using ASC.Common.Logging;
using ASC.Common.Utils;
2020-06-23 10:48:36 +00:00
using ASC.Data.Backup.Contracts;
2020-05-20 15:14:44 +00:00
using ASC.Data.Backup.EF.Model;
using ASC.Data.Backup.Storage;
2020-05-29 14:48:27 +00:00
using ASC.Data.Backup.Utils;
using Microsoft.Extensions.Configuration;
2020-05-29 14:48:27 +00:00
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
2020-05-20 15:14:44 +00:00
namespace ASC.Data.Backup.Service
{
public class BackupServiceNotifier
2020-06-15 15:33:53 +00:00
{
2020-08-12 09:58:08 +00:00
private ICacheNotify<BackupProgress> СacheBackupProgress { get; }
public ICache Cache { get; }
2020-06-15 15:33:53 +00:00
public BackupServiceNotifier(ICacheNotify<BackupProgress> сacheBackupProgress)
2020-06-15 15:33:53 +00:00
{
Cache = AscCache.Memory;
СacheBackupProgress = сacheBackupProgress;
2020-06-16 16:04:40 +00:00
СacheBackupProgress.Subscribe((a) =>
{
Cache.Insert(GetCacheKey(a.TenantId, a.BackupProgressEnum), a, DateTime.UtcNow.AddDays(1));
},
CacheNotifyAction.InsertOrUpdate);
}
2020-06-15 15:33:53 +00:00
public BackupProgress GetBackupProgress(int tenantId)
{
return Cache.Get<BackupProgress>(GetCacheKey(tenantId, BackupProgressEnum.Backup));
}
public BackupProgress GetTransferProgress(int tenantID)
{
return Cache.Get<BackupProgress>(GetCacheKey(tenantID, BackupProgressEnum.Transfer));
2020-06-15 15:33:53 +00:00
}
public BackupProgress GetRestoreProgress(int tenantId)
{
return Cache.Get<BackupProgress>(GetCacheKey(tenantId, BackupProgressEnum.Restore));
}
2020-09-30 14:47:42 +00:00
private string GetCacheKey(int tenantId, BackupProgressEnum backupProgressEnum)
{
return $"{backupProgressEnum}backup{tenantId}";
}
2020-06-15 15:33:53 +00:00
}
2020-06-23 10:48:36 +00:00
public class BackupService : IBackupService
2020-05-20 15:14:44 +00:00
{
2020-05-29 14:48:27 +00:00
private ILog Log { get; set; }
private BackupStorageFactory BackupStorageFactory { get; set; }
private BackupWorker BackupWorker { get; set; }
private BackupRepository BackupRepository { get; }
private BackupServiceNotifier BackupServiceNotifier { get; }
private IConfiguration Configuration { get; }
2020-05-29 14:48:27 +00:00
public BackupService(
IOptionsMonitor<ILog> options,
BackupStorageFactory backupStorageFactory,
BackupWorker backupWorker,
BackupRepository backupRepository,
BackupServiceNotifier backupServiceNotifier,
2020-06-15 15:33:53 +00:00
IConfiguration configuration)
2020-05-20 15:14:44 +00:00
{
2020-05-29 14:48:27 +00:00
Log = options.CurrentValue;
BackupStorageFactory = backupStorageFactory;
BackupWorker = backupWorker;
BackupRepository = backupRepository;
BackupServiceNotifier = backupServiceNotifier;
Configuration = configuration;
2020-05-20 15:14:44 +00:00
}
2020-06-15 15:33:53 +00:00
public void StartBackup(StartBackupRequest request)
2020-06-08 10:40:26 +00:00
{
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.StartBackup(request);
2020-05-20 15:14:44 +00:00
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
2020-06-15 15:33:53 +00:00
}
2020-05-20 15:14:44 +00:00
}
public void DeleteBackup(Guid id)
{
2020-05-29 14:48:27 +00:00
var backupRecord = BackupRepository.GetBackupRecord(id);
BackupRepository.DeleteBackupRecord(backupRecord.Id);
2020-05-20 15:14:44 +00:00
2020-05-29 14:48:27 +00:00
var storage = BackupStorageFactory.GetBackupStorage(backupRecord);
2020-05-20 15:14:44 +00:00
if (storage == null) return;
storage.Delete(backupRecord.StoragePath);
}
public void DeleteAllBackups(int tenantId)
{
2020-05-29 14:48:27 +00:00
foreach (var backupRecord in BackupRepository.GetBackupRecordsByTenantId(tenantId))
2020-05-20 15:14:44 +00:00
{
try
{
2020-05-29 14:48:27 +00:00
BackupRepository.DeleteBackupRecord(backupRecord.Id);
var storage = BackupStorageFactory.GetBackupStorage(backupRecord);
2020-05-20 15:14:44 +00:00
if (storage == null) continue;
storage.Delete(backupRecord.StoragePath);
}
catch (Exception error)
{
2020-05-29 14:48:27 +00:00
Log.Warn("error while removing backup record: {0}", error);
2020-05-20 15:14:44 +00:00
}
}
}
public List<BackupHistoryRecord> GetBackupHistory(int tenantId)
{
var backupHistory = new List<BackupHistoryRecord>();
2020-05-29 14:48:27 +00:00
foreach (var record in BackupRepository.GetBackupRecordsByTenantId(tenantId))
2020-05-20 15:14:44 +00:00
{
2020-05-29 14:48:27 +00:00
var storage = BackupStorageFactory.GetBackupStorage(record);
2020-05-20 15:14:44 +00:00
if (storage == null) continue;
if (storage.IsExists(record.StoragePath))
{
2020-05-29 14:48:27 +00:00
backupHistory.Add(new BackupHistoryRecord
{
Id = record.Id,
2020-05-29 14:48:27 +00:00
FileName = record.Name,
StorageType = record.StorageType,
CreatedOn = record.CreatedOn,
ExpiresOn = record.ExpiresOn
2020-05-29 14:48:27 +00:00
});
2020-05-20 15:14:44 +00:00
}
else
{
2020-05-29 14:48:27 +00:00
BackupRepository.DeleteBackupRecord(record.Id);
2020-05-20 15:14:44 +00:00
}
}
return backupHistory;
}
2020-06-15 15:33:53 +00:00
public void StartTransfer(StartTransferRequest request)
2020-05-20 15:14:44 +00:00
{
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.StartTransfer(request.TenantId, request.TargetRegion, request.BackupMail, request.NotifyUsers);
2020-05-20 15:14:44 +00:00
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
}
}
2020-06-15 15:33:53 +00:00
public void StartRestore(StartRestoreRequest request)
2020-05-20 15:14:44 +00:00
{
2020-06-08 10:40:26 +00:00
if ((BackupStorageType)request.StorageType == BackupStorageType.Local)
2020-05-20 15:14:44 +00:00
{
if (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId))
{
throw new FileNotFoundException();
}
}
if (!request.BackupId.Equals(Guid.Empty))
2020-05-20 15:14:44 +00:00
{
var backupRecord = BackupRepository.GetBackupRecord(request.BackupId);
2020-05-20 15:14:44 +00:00
if (backupRecord == null)
{
throw new FileNotFoundException();
}
request.FilePathOrId = backupRecord.StoragePath;
request.StorageType = backupRecord.StorageType;
request.StorageParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(backupRecord.StorageParams);
2020-05-20 15:14:44 +00:00
}
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.StartRestore(request);
2020-05-20 15:14:44 +00:00
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
}
2020-06-17 07:49:17 +00:00
}
public BackupProgress GetBackupProgress(int tenantId)
{
return BackupServiceNotifier.GetBackupProgress(tenantId);
2020-06-17 07:49:17 +00:00
}
public BackupProgress GetTransferProgress(int tenantId)
2020-06-17 07:49:17 +00:00
{
return BackupServiceNotifier.GetTransferProgress(tenantId);
2020-06-17 07:49:17 +00:00
}
2020-05-20 15:14:44 +00:00
public BackupProgress GetRestoreProgress(int tenantId)
2020-06-17 07:49:17 +00:00
{
return BackupServiceNotifier.GetRestoreProgress(tenantId);
2020-05-20 15:14:44 +00:00
}
public string GetTmpFolder()
{
2020-05-29 14:48:27 +00:00
return BackupWorker.TempFolder;
2020-05-20 15:14:44 +00:00
}
public List<TransferRegion> GetTransferRegions()
{
var settings = Configuration.GetSetting<BackupSettings>("backup");
return settings.WebConfigs.Elements.Select(configElement =>
{
var config = Utils.ConfigurationProvider.Open(PathHelper.ToRootedConfigPath(configElement.Path));
2020-06-16 16:04:40 +00:00
var baseDomain = config.AppSettings.Settings["core:base-domain"].Value;
return new TransferRegion
{
Name = configElement.Region,
BaseDomain = baseDomain,
IsCurrentRegion = configElement.Region.Equals(settings.WebConfigs.CurrentRegion, StringComparison.InvariantCultureIgnoreCase)
};
})
.ToList();
2020-05-20 15:14:44 +00:00
}
public void CreateSchedule(CreateScheduleRequest request)
{
2020-05-29 14:48:27 +00:00
BackupRepository.SaveBackupSchedule(
new BackupSchedule()
{
TenantId = request.TenantId,
Cron = request.Cron,
BackupMail = request.BackupMail,
BackupsStored = request.NumberOfBackupsStored,
StorageType = request.StorageType,
2020-05-29 14:48:27 +00:00
StorageBasePath = request.StorageBasePath,
StorageParams = JsonConvert.SerializeObject(request.StorageParams)
});
2020-05-20 15:14:44 +00:00
}
public void DeleteSchedule(int tenantId)
{
2020-05-29 14:48:27 +00:00
BackupRepository.DeleteBackupSchedule(tenantId);
2020-05-20 15:14:44 +00:00
}
public ScheduleResponse GetSchedule(int tenantId)
{
2020-05-29 14:48:27 +00:00
var schedule = BackupRepository.GetBackupSchedule(tenantId);
2020-06-15 15:33:53 +00:00
if (schedule != null)
2020-06-08 10:40:26 +00:00
{
var tmp = new ScheduleResponse
{
StorageType = schedule.StorageType,
2020-06-08 10:40:26 +00:00
StorageBasePath = schedule.StorageBasePath,
BackupMail = schedule.BackupMail,
NumberOfBackupsStored = schedule.BackupsStored,
Cron = schedule.Cron,
LastBackupTime = schedule.LastBackupTime,
StorageParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(schedule.StorageParams)
2020-06-08 10:40:26 +00:00
};
return tmp;
}
else
{
return null;
}
2020-05-20 15:14:44 +00:00
}
}
public static class BackupServiceExtension
{
public static DIHelper AddBackupService(this DIHelper services)
{
2020-07-17 10:52:28 +00:00
if (services.TryAddScoped<BackupService>())
{
2020-10-12 19:39:23 +00:00
services.TryAddSingleton<BackupServiceNotifier>();
2020-07-17 10:52:28 +00:00
return services
2020-09-23 10:55:39 +00:00
.AddBackupWorkerService()
.AddBackupStorageFactory()
.AddBackupRepositoryService();
2020-07-17 10:52:28 +00:00
}
return services;
}
}
2020-05-20 15:14:44 +00:00
}