107 lines
3.9 KiB
C#
107 lines
3.9 KiB
C#
using System.Data;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Data.SqlClient;
|
|
using WebAPI.Models;
|
|
|
|
namespace WebAPI.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AdminController : Controller
|
|
{
|
|
private readonly GoogleDriveHelper _googleDriveHelper;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly LogsController _logsController;
|
|
|
|
public AdminController(
|
|
GoogleDriveHelper googleDriveHelper,
|
|
GoogleSheetsHelper googleSheetsHelper,
|
|
IConfiguration configuration)
|
|
{
|
|
_googleDriveHelper = googleDriveHelper;
|
|
_configuration = configuration;
|
|
_logsController = new LogsController(googleSheetsHelper, _configuration);
|
|
}
|
|
|
|
[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?> { _configuration["GDriveBackupDirectory"] },
|
|
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());
|
|
}
|
|
}
|
|
} |