From ea0eaab70cbb17e49d640dfbbabcfc6506ebe942 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 30 Jun 2016 10:33:15 +1000 Subject: [PATCH] Add /server backup table [name] (Thanks venom983) --- Commands/other/CmdServer.cs | 102 ++++++++++++++++++++---------------- Database/Backup.cs | 13 ++--- 2 files changed, 61 insertions(+), 54 deletions(-) diff --git a/Commands/other/CmdServer.cs b/Commands/other/CmdServer.cs index c47823c7b..e4f276f8f 100644 --- a/Commands/other/CmdServer.cs +++ b/Commands/other/CmdServer.cs @@ -1,19 +1,19 @@ /* - Copyright 2011 MCForge - - Dual-licensed under the Educational Community License, Version 2.0 and - the GNU General Public License, Version 3 (the "Licenses"); you may - not use this file except in compliance with the Licenses. You may - obtain a copy of the Licenses at - - http://www.opensource.org/licenses/ecl2.php - http://www.gnu.org/licenses/gpl-3.0.html - - Unless required by applicable law or agreed to in writing, - software distributed under the Licenses are distributed on an "AS IS" - BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - or implied. See the Licenses for the specific language governing - permissions and limitations under the Licenses. + Copyright 2011 MCForge + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. */ using System; using System.Collections.Generic; @@ -32,7 +32,8 @@ namespace MCGalaxy.Commands { public CmdServer() { } public override void Use(Player p, string message) { - switch (message) { + string[] args = message.ToLower().Split(' '); + switch (args[0]) { case "public": Server.pub = true; Player.Message(p, "Server is now public!"); @@ -74,32 +75,41 @@ namespace MCGalaxy.Commands { Player.Message(p, "Settings reloaded! You may need to restart the server, however."); break; case "backup": - case "backup all": - // Backup Everything. - // Create SQL statements for this. The SQL will assume the settings for the current configuration are correct. - // This means we use the currently defined port, database, user, password, and pooling. - // Also important to save everything to a .zip file (Though we can rename the extention.) - // When backing up, one option is to save all non-main program files. - // This means all folders, and files in these folders. - Player.Message(p, "Server backup (Everything): Started. Please wait while backup finishes."); - Save(true, true, p); - break; - case "backup db": - // Backup database only. - // Create SQL statements for this. The SQL will assume the settings for the current configuration are correct. - // This means we use the currently defined port, database, user, password, and pooling. - // Also important to save everything to a .zip file (Though we can rename the extention.) - // When backing up, one option is to save all non-main program files. - // This means all folders, and files in these folders. - Player.Message(p, "Server backup (Database): Started. Please wait while backup finishes."); - Save(false, true, p); - break; - case "backup allbutdb": - // Important to save everything to a .zip file (Though we can rename the extention.) - // When backing up, one option is to save all non-main program files. - // This means all folders, and files in these folders. - Player.Message(p, "Server backup (Everything but Database): Started. Please wait while backup finishes."); - Save(true, false, p); + if (args.Length == 1 || args[1] == "all") { + // Backup Everything. + // Create SQL statements for this. The SQL will assume the settings for the current configuration are correct. + // This means we use the currently defined port, database, user, password, and pooling. + // Also important to save everything to a .zip file (Though we can rename the extention.) + // When backing up, one option is to save all non-main program files. + // This means all folders, and files in these folders. + Player.Message(p, "Server backup (Everything) started. Please wait while backup finishes."); + Save(true, true, p); + } else if (args[1] == "db") { + // Backup database only. + // Create SQL statements for this. The SQL will assume the settings for the current configuration are correct. + // This means we use the currently defined port, database, user, password, and pooling. + // Also important to save everything to a .zip file (Though we can rename the extention.) + // When backing up, one option is to save all non-main program files. + // This means all folders, and files in these folders. + Player.Message(p, "Server backup (Database) started. Please wait while backup finishes."); + Save(false, true, p); + } else if (args[1] == "allbutdb") { + // Important to save everything to a .zip file (Though we can rename the extention.) + // When backing up, one option is to save all non-main program files. + // This means all folders, and files in these folders. + Player.Message(p, "Server backup (Everything but Database) started. Please wait while backup finishes."); + Save(true, false, p); + } else if (args[1] == "table") { + if (args.Length == 2) { Player.Message(p, "You need to provide the table name to backup."); return; } + if (!ValidName(p, args[2], "table")) return; + + Player.Message(p, "Backing up table {0} started. Please wait while backup finishes.", args[2]); + using (StreamWriter sql = new StreamWriter(args[2] + ".sql")) + Database.BackupTable(args[2], sql); + Player.Message(p, "Finished backing up table {0}.", args[2]); + } else { + Help(p); + } break; case "restore": if (!CheckPerms(p)) { @@ -137,8 +147,8 @@ namespace MCGalaxy.Commands { } void SetToDefault() { - foreach (var elem in Server.serverConfig) - elem.Field.SetValue(null, elem.Attrib.DefaultValue); + foreach (var elem in Server.serverConfig) + elem.Field.SetValue(null, elem.Attrib.DefaultValue); Server.tempBans = new List(); Server.ircafkset = new List(); @@ -175,10 +185,10 @@ namespace MCGalaxy.Commands { Player.Message(p, "all - Make a backup of the server and all SQL data. (Default)"); Player.Message(p, "db - Just backup the database."); Player.Message(p, "allbutdb - Backup everything BUT the database."); + Player.Message(p, "/server table [name] - Make a backups of that database table"); } - static void CreatePackage(object par) - { + static void CreatePackage(object par) { List param = (List)par; CreatePackage((string)param[0], (bool)param[1], (bool)param[2], (Player)param[3]); } diff --git a/Database/Backup.cs b/Database/Backup.cs index bd2275e94..c9ab4d110 100644 --- a/Database/Backup.cs +++ b/Database/Backup.cs @@ -48,7 +48,7 @@ namespace MCGalaxy.SQL { BackupTable(tableName, sql); } - static void BackupTable(string tableName, StreamWriter sql) { + public static void BackupTable(string tableName, StreamWriter sql) { //For each table, we iterate through all rows, (and save them) sql.WriteLine("-- --------------------------------------------------------"); sql.WriteLine(); @@ -184,19 +184,16 @@ namespace MCGalaxy.SQL { return schema; } - private static List getTables() - { + static List getTables() { List tableNames = new List(); - using (DataTable tables = fillData((Server.useMySQL ? "SHOW TABLES" : "SELECT * FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"))) - { - foreach (DataRow row in tables.Rows) - { + using (DataTable tables = fillData((Server.useMySQL ? "SHOW TABLES" : "SELECT * FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"))) { + foreach (DataRow row in tables.Rows) { string tableName = row.Field((Server.useMySQL ? 0 : 1)); tableNames.Add(tableName); } } return tableNames; - }// end:CopyDatabase() + } internal static void fillDatabase(Stream stream) {