Example #1
0
    /// <summary>
    /// This function is used to backup data from database
    /// </summary>
    /// <returns></returns>
    public async Task <DataBackupInfo> BackupMySqlDatabase()
    {
        var op = Operation.Begin("Exporting Data");

        RemoveOldSqlBackup();

        var dbName = await GetCurrentDbName();

        _logger.LogInformation("Starting Export Database. Name: {DbName}", dbName);

        var timeStamp = DateTime.Now.ToString("yyyy-MM-dd");
        var fileName  = $"{dbName}_{timeStamp}.sql";
        var fullName  = Path.Combine(DataDir, fileName);

        _logger.LogDebug("Creating MySql Connection..");
        var connection = _queryService.CreateMysqlConnectionCore();

        var cmd = new MySqlCommand
        {
            Connection = connection
        };

        var mb = new MySqlBackup(cmd)
        {
            ExportInfo =
            {
                AddDropTable       = false,
                AddDropDatabase    = false,
                ResetAutoIncrement = true
            }
        };

        mb.ExportProgressChanged += (
            sender,
            args
            ) => {
            _logger.LogDebug(
                "Processing backup Table: {TableName}. Table Rows: {Rows}. Current Index: {Index}",
                args.CurrentTableName,
                args.TotalRowsInCurrentTable,
                args.CurrentRowIndexInCurrentTable
                );
        };

        mb.ExportCompleted += (
            sender,
            args
            ) => {
            _logger.LogDebug(
                "Backup Table complete. Elapsed: {TableName}",
                args.TimeUsed
                );
        };

        _logger.LogDebug("Opening MySql Connection..");
        await connection.OpenAsync();

        _logger.LogDebug(
            "Exporting database {DbName} to file: {SqlFile}",
            dbName,
            fullName
            );
        mb.ExportToFile(fullName);

        var zipFileName = fullName.CreateZip(compressionMethod: CompressionMethod.BZip2);

        _logger.LogDebug(
            "Database: {DbName} exported to file: {SqlZip}",
            dbName,
            zipFileName
            );

        _logger.LogDebug("Closing MySql Connection..");
        await connection.CloseAsync();

        var backupInfo = new DataBackupInfo
        {
            FileName          = fileName,
            FullName          = fullName,
            FileNameZip       = zipFileName,
            FullNameZip       = zipFileName,
            FileSizeSql       = fullName.FileSize().SizeFormat(),
            FileSizeSqlRaw    = fullName.FileSize(),
            FileSizeSqlZip    = zipFileName.FileSize().SizeFormat(),
            FileSizeSqlZipRaw = zipFileName.FileSize()
        };

        op.Complete();

        return(backupInfo);
    }
 public MySqlConnection CreateConnection()
 {
     return(_queryService.CreateMysqlConnectionCore());
 }
    public async Task <StepHistory> GetStepHistoryCore(
        long chatId,
        long userId,
        StepHistoryName name
        )
    {
        var data = await _queryService
                   .CreateMysqlConnectionCore()
                   .QueryAsync <StepHistory>(
            history =>
            history.ChatId == chatId &&
            history.UserId == userId &&
            history.Name == name
            );

        return(data.FirstOrDefault());
    }