From f3c9bc18b204e0e50ddc98ce6120774c5d447733 Mon Sep 17 00:00:00 2001 From: Julian van de Groep Date: Wed, 6 Nov 2019 19:28:27 +0100 Subject: [PATCH] Use a blacklist instead of a whitelist. --- BackupManager/BackupManager.csproj | 4 ++++ BackupManager/Config.cs | 2 +- BackupManager/Program.cs | 34 +++++++++++++++++++++++++++--- README.md | 2 +- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/BackupManager/BackupManager.csproj b/BackupManager/BackupManager.csproj index 2eba7d9..a6eb3a3 100644 --- a/BackupManager/BackupManager.csproj +++ b/BackupManager/BackupManager.csproj @@ -5,4 +5,8 @@ netcoreapp2.2 + + + + diff --git a/BackupManager/Config.cs b/BackupManager/Config.cs index 669c507..327c6dd 100644 --- a/BackupManager/Config.cs +++ b/BackupManager/Config.cs @@ -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; } diff --git a/BackupManager/Program.cs b/BackupManager/Program.cs index 17edfc5..2a5fee6 100644 --- a/BackupManager/Program.cs +++ b/BackupManager/Program.cs @@ -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 databases = new List(); + 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); diff --git a/README.md b/README.md index a1cb3bc..b7f5823 100644 --- a/README.md +++ b/README.md @@ -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'; ```