Add /server backup table [name] (Thanks venom983)

This commit is contained in:
UnknownShadow200 2016-06-30 10:33:15 +10:00
parent 6934c4bcca
commit ea0eaab70c
2 changed files with 61 additions and 54 deletions

View File

@ -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":
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.");
Player.Message(p, "Server backup (Everything) started. Please wait while backup finishes.");
Save(true, true, p);
break;
case "backup db":
} 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.");
Player.Message(p, "Server backup (Database) started. Please wait while backup finishes.");
Save(false, true, p);
break;
case "backup allbutdb":
} 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.");
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)) {
@ -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 <backup> table [name] - Make a backups of that database table");
}
static void CreatePackage(object par)
{
static void CreatePackage(object par) {
List<object> param = (List<object>)par;
CreatePackage((string)param[0], (bool)param[1], (bool)param[2], (Player)param[3]);
}

View File

@ -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<string> getTables()
{
static List<string> getTables() {
List<string> tableNames = new List<string>();
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<string>((Server.useMySQL ? 0 : 1));
tableNames.Add(tableName);
}
}
return tableNames;
}// end:CopyDatabase()
}
internal static void fillDatabase(Stream stream)
{