2023-09-01 18:23:53 +02:00
|
|
|
using System.Data;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
using Google.Apis.Sheets.v4;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-11-29 22:14:37 +01:00
|
|
|
using Microsoft.Data.SqlClient;
|
2023-09-01 18:23:53 +02:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using WebAPI.dataParsers;
|
|
|
|
|
using WebAPI.Exports;
|
|
|
|
|
using WebAPI.Models;
|
|
|
|
|
using static Google.Apis.Drive.v3.FilesResource;
|
|
|
|
|
|
|
|
|
|
namespace WebAPI.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
|
|
|
|
public class AdminController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly AppDbContext db;
|
|
|
|
|
private GoogleDriveHelper googleDriveHelper;
|
|
|
|
|
private GoogleSheetsHelper googleSheetsHelper;
|
|
|
|
|
private readonly IConfiguration configuration;
|
|
|
|
|
private readonly LogsController logsController;
|
|
|
|
|
|
|
|
|
|
public AdminController(
|
|
|
|
|
AppDbContext _db,
|
|
|
|
|
GoogleSheetsHelper _googleSheetsHelper,
|
|
|
|
|
GoogleDriveHelper _googleDriveHelper,
|
|
|
|
|
IConfiguration _configuration)
|
|
|
|
|
{
|
|
|
|
|
db = _db;
|
|
|
|
|
googleSheetsHelper = _googleSheetsHelper;
|
|
|
|
|
googleDriveHelper = _googleDriveHelper;
|
|
|
|
|
configuration = _configuration;
|
|
|
|
|
logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 22:14:37 +01:00
|
|
|
[HttpGet]
|
2023-09-01 18:23:53 +02:00
|
|
|
[Route("BackupDatabase/{apiKey}")]
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
public IActionResult BackupDatabase(string apiKey)
|
2023-11-29 22:14:37 +01:00
|
|
|
{
|
2023-09-01 18:23:53 +02:00
|
|
|
if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"])
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
try
|
2023-11-29 22:14:37 +01:00
|
|
|
{
|
|
|
|
|
string databaseName = "diunabi-morska";
|
|
|
|
|
string localDatabasePath = $"{configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak";
|
|
|
|
|
var formatMediaName = $"DatabaseToolkitBackup_{databaseName}";
|
|
|
|
|
var formatName = $"Full Backup of {databaseName}";
|
|
|
|
|
|
|
|
|
|
var connection = new SqlConnection(configuration.GetConnectionString("SQLDatabase"));
|
|
|
|
|
|
|
|
|
|
var 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();
|
|
|
|
|
|
2023-09-01 18:23:53 +02:00
|
|
|
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
|
|
|
|
|
body.Name = Path.GetFileName(localDatabasePath);
|
|
|
|
|
body.Parents = new List<string?> { configuration["GDriveBackupDirectory"] };
|
|
|
|
|
body.MimeType = "application/octet-stream";
|
|
|
|
|
|
|
|
|
|
var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
2023-11-29 22:14:37 +01:00
|
|
|
if (googleDriveHelper.Service is null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Google Drive API not initialized");
|
|
|
|
|
}
|
2023-09-01 18:23:53 +02:00
|
|
|
CreateMediaUpload request = googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType);
|
|
|
|
|
request.Fields = "id";
|
|
|
|
|
|
|
|
|
|
var response = request.Upload();
|
2023-11-29 22:14:37 +01:00
|
|
|
|
|
|
|
|
|
2023-09-01 18:23:53 +02:00
|
|
|
logsController.AddEntry(new LogEntry
|
|
|
|
|
{
|
|
|
|
|
Title = "Backup success",
|
|
|
|
|
Type = LogEntryType.info,
|
|
|
|
|
LogType = LogType.backup,
|
|
|
|
|
CreatedAt = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2023-11-29 22:14:37 +01:00
|
|
|
{
|
2023-09-01 18:23:53 +02:00
|
|
|
logsController.AddEntry(new LogEntry
|
|
|
|
|
{
|
|
|
|
|
Title = "Backup error",
|
|
|
|
|
Type = LogEntryType.error,
|
|
|
|
|
LogType = LogType.backup,
|
|
|
|
|
Message = e.ToString(),
|
|
|
|
|
CreatedAt = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
return BadRequest(e.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|