Remove unused backup enpoint
This commit is contained in:
@@ -9,103 +9,14 @@ namespace WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AdminController : Controller
|
||||
{
|
||||
private readonly GoogleDriveHelper _googleDriveHelper;
|
||||
public class AdminController : Controller {
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly LogsController _logsController;
|
||||
|
||||
public AdminController(
|
||||
GoogleDriveHelper googleDriveHelper,
|
||||
GoogleSheetsHelper googleSheetsHelper,
|
||||
IConfiguration configuration,
|
||||
FirestoreDb firestoreDb)
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_googleDriveHelper = googleDriveHelper;
|
||||
_configuration = configuration;
|
||||
_logsController = new LogsController(firestoreDb);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("BackupDatabase/{apiKey}")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult BackupDatabase(string apiKey)
|
||||
{
|
||||
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
const string databaseName = "diunabi-morska";
|
||||
var localDatabasePath = $"{_configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak";
|
||||
const string formatMediaName = $"DatabaseToolkitBackup_{databaseName}";
|
||||
const string formatName = $"Full Backup of {databaseName}";
|
||||
|
||||
|
||||
var connection = new SqlConnection(_configuration.GetConnectionString("SQLDatabase"));
|
||||
|
||||
const string sql = """
|
||||
BACKUP DATABASE @databaseName
|
||||
TO DISK = @localDatabasePath
|
||||
WITH FORMAT,
|
||||
MEDIANAME = @formatMediaName,
|
||||
NAME = @formatName
|
||||
""";
|
||||
|
||||
connection.Open();
|
||||
var command = new SqlCommand(sql, connection);
|
||||
|
||||
command.CommandType = CommandType.Text;
|
||||
command.CommandTimeout = 7200;
|
||||
command.Parameters.AddWithValue("@databaseName", databaseName);
|
||||
command.Parameters.AddWithValue("@localDatabasePath", localDatabasePath);
|
||||
command.Parameters.AddWithValue("@formatMediaName", formatMediaName);
|
||||
command.Parameters.AddWithValue("@formatName", formatName);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
var body = new Google.Apis.Drive.v3.Data.File
|
||||
{
|
||||
Name = Path.GetFileName(localDatabasePath),
|
||||
Parents = new List<string?> { "1ANm0pokPFgBw7vIaxLgDWXqznl3UjVvJ" },
|
||||
MimeType = "application/octet-stream"
|
||||
};
|
||||
|
||||
var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read);
|
||||
|
||||
if (_googleDriveHelper.Service is null)
|
||||
{
|
||||
throw new Exception("Google Drive API not initialized");
|
||||
}
|
||||
|
||||
var request = _googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType);
|
||||
request.Fields = "id";
|
||||
|
||||
request.Upload();
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = "Backup success",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Backup,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = "Backup error",
|
||||
Type = LogEntryType.Error,
|
||||
LogType = LogType.Backup,
|
||||
Message = e.ToString(),
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
DECLARE @JustForDebug TINYINT = 1;
|
||||
DECLARE @JustForDebug TINYINT = 0;
|
||||
|
||||
-- SETUP VARIABLES
|
||||
DECLARE @Type NVARCHAR(3) = 'FK2';
|
||||
DECLARE @Month INT = 5;
|
||||
DECLARE @Type NVARCHAR(3) = 'K5';
|
||||
DECLARE @Month INT = 6;
|
||||
DECLARE @Year INT = 2025;
|
||||
DECLARE @MonthName NVARCHAR(20) = 'Maj_2025';
|
||||
DECLARE @MonthName NVARCHAR(20) = 'Czerwiec_2025';
|
||||
|
||||
IF @Type NOT IN ('K5', 'PU', 'AK', 'FK', 'D1', 'FK2')
|
||||
BEGIN
|
||||
@@ -16,7 +16,7 @@ DECLARE @Range NVARCHAR(20);
|
||||
SET @Range =
|
||||
CASE @Type
|
||||
WHEN 'D1' THEN 'A7:R200'
|
||||
WHEN 'FK2' THEN 'A8:I1652'
|
||||
WHEN 'FK2' THEN 'A8:J1652'
|
||||
ELSE 'E4:DA5'
|
||||
END;
|
||||
DECLARE @ImportType NVARCHAR(20);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
-- T3-MultiSourceSummary
|
||||
DECLARE @JustForDebug TINYINT = 1;
|
||||
DECLARE @JustForDebug TINYINT = 0;
|
||||
|
||||
-- SETUP VARIABLES
|
||||
DECLARE @Month INT = 5;
|
||||
DECLARE @Month INT = 6;
|
||||
DECLARE @Year INT = 2025;
|
||||
|
||||
DECLARE @Number INT = (SELECT COUNT(id) + 1 FROM [diunabi-morska].[dbo].[Layers]);
|
||||
|
||||
@@ -4,7 +4,7 @@ DECLARE @JustForDebug TINYINT = 1;
|
||||
|
||||
-- SETUP VARIABLES
|
||||
DECLARE @Type NVARCHAR(3) = 'FK';
|
||||
DECLARE @Month INT = 5;
|
||||
DECLARE @Month INT = 6;
|
||||
DECLARE @Year INT = 2025;
|
||||
|
||||
IF @Type NOT IN ('K5', 'PU', 'AK', 'FK')
|
||||
|
||||
@@ -4,7 +4,7 @@ DECLARE @JustForDebug TINYINT = 0;
|
||||
|
||||
-- SETUP VARIABLES
|
||||
DECLARE @Type NVARCHAR(3) = 'FK2';
|
||||
DECLARE @Month INT = 5;
|
||||
DECLARE @Month INT = 6;
|
||||
DECLARE @Year INT = 2025;
|
||||
|
||||
DECLARE @Number INT = (SELECT COUNT(id) + 1 FROM [diunabi-morska].[dbo].[Layers]);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
-- T5-LastValue
|
||||
DECLARE @JustForDebug TINYINT = 1;
|
||||
DECLARE @JustForDebug TINYINT = 0;
|
||||
|
||||
-- SETUP VARIABLES
|
||||
DECLARE @Month INT = 3;
|
||||
DECLARE @Month INT = 4;
|
||||
DECLARE @Year INT = 2025;
|
||||
|
||||
DECLARE @Number INT = (SELECT COUNT(id) + 1 FROM [diunabi-morska].[dbo].[Layers]);
|
||||
|
||||
@@ -21,8 +21,6 @@
|
||||
"exportDirectory": "1eTyCUzYbzVQB8f8sbNmvnebFXyW2-axt",
|
||||
"appLogsFile": "13PuDvS3_HAYoSLOCgKexzlzIDLUilkApUF8QiJMTae0",
|
||||
"apiLocalUrl": "localhost:5400",
|
||||
"dbBackupFile": "/home/mz/backups/diunabi-morska",
|
||||
"GDriveBackupDirectory": "1ANm0pokPFgBw7vIaxLgDWXqznl3UjVvJ",
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
|
||||
@@ -21,8 +21,6 @@
|
||||
"exportDirectory": "#{export-directory}#",
|
||||
"appLogsFile": "#{app-logs-file}#",
|
||||
"apiLocalUrl": "#{api-local-url}#",
|
||||
"dbBackupFile": "#{db-backup-file}#",
|
||||
"GDriveBackupDirectory": "#{gdrive-backup-directory}#",
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
|
||||
Reference in New Issue
Block a user