Use a blacklist instead of a whitelist.

This commit is contained in:
flash 2019-11-06 19:28:27 +01:00
parent 1e8e180a78
commit f3c9bc18b2
4 changed files with 37 additions and 5 deletions

View file

@ -5,4 +5,8 @@
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.18" />
</ItemGroup>
</Project>

View file

@ -9,7 +9,7 @@
public string MySqlHost { get; set; } = @"localhost";
public string MySqlUser { get; set; }
public string MySqlPass { get; set; }
public string MySqlDatabases { get; set; } = @"misuzu";
public string MySqlExcludeDatabases { get; set; } = @"mysql information_schema performance_schema";
public string MisuzuPath { get; set; }

View file

@ -1,4 +1,6 @@
using System;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
@ -92,6 +94,34 @@ namespace BackupManager {
if (!Directory.Exists(BackupStore))
Directory.CreateDirectory(BackupStore);
Log(@"Fetching database list...");
MySqlConnectionStringBuilder connStr = new MySqlConnectionStringBuilder {
Server = Config.MySqlHost,
UserID = Config.MySqlUser,
Password = Config.MySqlPass,
CharacterSet = @"utf8mb4",
};
List<string> databases = new List<string>();
string[] exclude = Config.MySqlExcludeDatabases.Split(' ');
using (MySqlConnection conn = new MySqlConnection(connStr.ToString())) {
conn.Open();
using (MySqlCommand comm = new MySqlCommand(@"SHOW DATABASES;", conn))
using (MySqlDataReader read = comm.ExecuteReader()) {
while (read.Read()) {
string database = read.GetString(0);
if (string.IsNullOrEmpty(database) || exclude.Contains(database))
continue;
databases.Add(database);
}
}
}
Log(@"Creating backup archive...");
string archivePath = Path.GetTempFileName();
@ -110,8 +140,6 @@ namespace BackupManager {
sw.WriteLine(@"default-character-set=utf8mb4");
}
string[] databases = Config.MySqlDatabases.Split(' ');
foreach (string database in databases)
CreateDbDump(archive, sqldefaults, database);

View file

@ -6,5 +6,5 @@ Provided for transparency.
## Grant line for MySQL backup user
```
GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER, EXECUTE ON *.* TO 'user'@'localhost';
GRANT SELECT, LOCK TABLES, SHOW VIEW, SHOW DATABASES, EVENT, TRIGGER, EXECUTE ON *.* TO 'user'@'localhost';
```