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;
|
2021-08-31 09:40:28 +00:00
|
|
|
using System.ServiceModel;
|
|
|
|
|
2020-05-28 13:28:51 +00:00
|
|
|
using ASC.Common;
|
2020-05-20 15:14:44 +00:00
|
|
|
using ASC.Common.Logging;
|
2020-06-01 10:33:39 +00:00
|
|
|
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;
|
2021-08-31 09:40:28 +00:00
|
|
|
using ASC.Data.Backup.Utils;
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
2022-01-18 16:04:35 +00:00
|
|
|
namespace ASC.Data.Backup.Services
|
2020-10-14 14:26:18 +00:00
|
|
|
{
|
2020-10-19 15:53:15 +00:00
|
|
|
[Scope]
|
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; }
|
2021-05-13 08:57:40 +00:00
|
|
|
private BackupWorker BackupWorker { get; set; }
|
|
|
|
private BackupRepository BackupRepository { get; }
|
|
|
|
private ConfigurationExtension Configuration { get; }
|
|
|
|
|
|
|
|
public BackupService(
|
|
|
|
IOptionsMonitor<ILog> options,
|
|
|
|
BackupStorageFactory backupStorageFactory,
|
|
|
|
BackupWorker backupWorker,
|
2020-10-14 14:26:18 +00:00
|
|
|
BackupRepository backupRepository,
|
2020-11-24 10:15:11 +00:00
|
|
|
ConfigurationExtension configuration)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2020-05-29 14:48:27 +00:00
|
|
|
Log = options.CurrentValue;
|
|
|
|
BackupStorageFactory = backupStorageFactory;
|
2021-05-13 08:57:40 +00:00
|
|
|
BackupWorker = backupWorker;
|
|
|
|
BackupRepository = backupRepository;
|
|
|
|
Configuration = configuration;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 08:57:40 +00:00
|
|
|
public void StartBackup(StartBackupRequest request)
|
|
|
|
{
|
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();
|
2021-05-13 08:57:40 +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))
|
|
|
|
{
|
2021-05-13 08:57:40 +00:00
|
|
|
backupHistory.Add(new BackupHistoryRecord
|
|
|
|
{
|
|
|
|
Id = record.Id,
|
|
|
|
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-11-24 10:15:11 +00:00
|
|
|
if (request.StorageType == BackupStorageType.Local)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId))
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:05:14 +00:00
|
|
|
if (!request.BackupId.Equals(Guid.Empty))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2020-06-25 16:05:14 +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;
|
2020-06-25 16:05:14 +00:00
|
|
|
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();
|
|
|
|
}
|
2021-05-13 08:57:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:49:17 +00:00
|
|
|
public BackupProgress GetBackupProgress(int tenantId)
|
2021-05-13 08:57:40 +00:00
|
|
|
{
|
2020-10-14 14:26:18 +00:00
|
|
|
return BackupWorker.GetBackupProgress(tenantId);
|
2021-05-13 08:57:40 +00:00
|
|
|
}
|
2020-06-17 07:49:17 +00:00
|
|
|
|
2020-06-25 16:05:14 +00:00
|
|
|
public BackupProgress GetTransferProgress(int tenantId)
|
2021-05-13 08:57:40 +00:00
|
|
|
{
|
2020-10-14 14:26:18 +00:00
|
|
|
return BackupWorker.GetTransferProgress(tenantId);
|
2021-05-13 08:57:40 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
|
|
|
public BackupProgress GetRestoreProgress(int tenantId)
|
2021-05-13 08:57:40 +00:00
|
|
|
{
|
2020-10-14 14:26:18 +00:00
|
|
|
return BackupWorker.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()
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
|
|
|
var settings = Configuration.GetSetting<BackupSettings>("backup");
|
|
|
|
return settings.WebConfigs.Elements.Select(configElement =>
|
|
|
|
{
|
|
|
|
var config = Utils.ConfigurationProvider.Open(PathHelper.ToRootedConfigPath(configElement.Path));
|
|
|
|
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(
|
2021-08-31 09:40:28 +00:00
|
|
|
new BackupSchedule()
|
|
|
|
{
|
|
|
|
TenantId = request.TenantId,
|
|
|
|
Cron = request.Cron,
|
|
|
|
BackupMail = request.BackupMail,
|
|
|
|
BackupsStored = request.NumberOfBackupsStored,
|
|
|
|
StorageType = request.StorageType,
|
|
|
|
StorageBasePath = request.StorageBasePath,
|
|
|
|
StorageParams = JsonConvert.SerializeObject(request.StorageParams)
|
2020-05-29 14:48:27 +00:00
|
|
|
});
|
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);
|
2021-08-31 09:40:28 +00:00
|
|
|
if (schedule != null)
|
|
|
|
{
|
|
|
|
var tmp = new ScheduleResponse
|
|
|
|
{
|
|
|
|
StorageType = schedule.StorageType,
|
|
|
|
StorageBasePath = schedule.StorageBasePath,
|
|
|
|
BackupMail = schedule.BackupMail,
|
|
|
|
NumberOfBackupsStored = schedule.BackupsStored,
|
|
|
|
Cron = schedule.Cron,
|
|
|
|
LastBackupTime = schedule.LastBackupTime,
|
|
|
|
StorageParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(schedule.StorageParams)
|
|
|
|
};
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
2020-06-08 10:40:26 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|