using System.Data; using System.Globalization; using System.Xml.Serialization; using Google.Apis.Sheets.v4; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; 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); } [HttpGet] [Route("BackupDatabase/{apiKey}")] [AllowAnonymous] public IActionResult BackupDatabase(string apiKey) { if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"]) { return Unauthorized(); } try { 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(); Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File(); body.Name = Path.GetFileName(localDatabasePath); body.Parents = new List { configuration["GDriveBackupDirectory"] }; body.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"); } CreateMediaUpload request = googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType); request.Fields = "id"; var response = 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()); } } } }