DB: Add /server import [name] which imports the SQL from a table previously backed up using /server backup table [name].

This commit is contained in:
UnknownShadow200 2016-10-15 14:40:30 +11:00
parent d91da8c173
commit b2f63a639c
3 changed files with 114 additions and 112 deletions

View File

@ -14,11 +14,10 @@
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;
using System.IO;
using System.IO.Packaging;
using System.Threading;
using MCGalaxy.SQL;
@ -34,104 +33,105 @@ namespace MCGalaxy.Commands {
public override void Use(Player p, string message) {
string[] args = message.Split(' ');
switch (args[0].ToLower()) {
case "public":
Server.pub = true;
Player.Message(p, "Server is now public!");
Server.s.Log("Server is now public!");
break;
case "private":
Server.pub = false;
Player.Message(p, "Server is now private!");
Server.s.Log("Server is now private!");
break;
case "reset": //made so ONLY the owner or console can use this command.
if (!CheckPerms(p)) {
Player.Message(p, "Only Console or the Server Owner can reset the server."); return;
}
//restting to default properties is dangerous... but recoverable.
//We save the old files to <name>.bkp, then delete them.
//Files needed:
// Property files
// Group
// Server
// Rank
// Command
Player.Message(p, "Backing up and deleting current property files.");
foreach (string name in Directory.GetFiles("properties"))
{
File.Copy(name, name + ".bkp"); // create backup first.
File.Delete(name);
}
Player.Message(p, "Done! Restoring defaults...");
//We set he defaults here, then go to reload the settings.
SetToDefault();
goto case "reload";
case "reload": // For security, only the owner and Console can use this.
if (!CheckPerms(p)) {
Player.Message(p, "Only Console or the Server Owner can reload the server settings."); return;
}
Player.Message(p, "Reloading settings...");
Server.LoadAllSettings();
Player.Message(p, "Settings reloaded! You may need to restart the server, however.");
break;
case "backup":
string type = args.Length == 1 ? "" : args[1].ToLower();
if (type == "" || type == "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 (type == "database" || type == "sql" || type == "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 (type == "allbutdb" || type == "files" || type == "file") {
// 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 (type == "lite") {
Player.Message(p, "Server backup (Everything but BlockDB tables and undo files) started. Please wait while backup finishes.");
Save(true, true, p, true);
} else if (type == "table") {
if (args.Length == 2) { Player.Message(p, "You need to provide the table name to backup."); return; }
if (!Formatter.ValidName(p, args[2], "table")) return;
if (!Database.TableExists(args[2])) { Player.Message(p, "Table \"{0}\" does not exist.", args[2]); return; }
Player.Message(p, "Backing up table {0} started. Please wait while backup finishes.", args[2]);
using (StreamWriter sql = new StreamWriter(args[2] + ".sql"))
Backup.BackupTable(args[2], sql);
Player.Message(p, "Finished backing up table {0}.", args[2]);
} else {
Help(p);
}
break;
case "restore":
if (!CheckPerms(p)) {
Player.Message(p, "Only Console or the Server Owner can restore the server.");
return;
}
Thread extract = new Thread(new ParameterizedThreadStart(Backup.ExtractPackage));
extract.Name = "MCG_RestoreServer";
extract.Start(p);
break;
default:
if (message != "")
Player.Message(p, "/server " + message + " is not currently implemented.");
Help(p);
break;
case "public": SetPublic(p, args); break;
case "private": SetPrivate(p, args); break;
case "reset": DoReset(p, args); break;
case "reload": DoReload(p, args); break;
case "backup": DoBackup(p, args); break;
case "restore": DoRestore(p, args); break;
case "import": DoImport(p, args); break;
default: Help(p); break;
}
}
void SetPublic(Player p, string[] args) {
Server.pub = true;
Player.Message(p, "Server is now public!");
Server.s.Log("Server is now public!");
}
void SetPrivate(Player p, string[] args) {
Server.pub = false;
Player.Message(p, "Server is now private!");
Server.s.Log("Server is now private!");
}
void DoReset(Player p, string[] args) {
if (!CheckPerms(p)) {
Player.Message(p, "Only Console or the Server Owner can reset the server."); return;
}
Player.Message(p, "Backing up and deleting current property files.");
foreach (string name in Directory.GetFiles("properties")) {
File.Copy(name, name + ".bkp"); // create backup first
File.Delete(name);
}
Player.Message(p, "Done! Restoring defaults...");
SetToDefault();
DoReload(p, args);
}
void DoReload(Player p, string[] args) {
if (!CheckPerms(p)) {
Player.Message(p, "Only Console or the Server Owner can reload the server settings."); return;
}
Player.Message(p, "Reloading settings...");
Server.LoadAllSettings();
Player.Message(p, "Settings reloaded! You may need to restart the server, however.");
}
void DoBackup(Player p, string[] args) {
string type = args.Length == 1 ? "" : args[1].ToLower();
if (type == "" || type == "all") {
Player.Message(p, "Server backup (Everything) started. Please wait while backup finishes.");
Save(true, true, p);
} else if (type == "database" || type == "sql" || type == "db") {
// Creates CREATE TABLE and INSERT statements for all tables and rows in the database
Player.Message(p, "Server backup (Database) started. Please wait while backup finishes.");
Save(false, true, p);
} else if (type == "allbutdb" || type == "files" || type == "file") {
// Saves all files and folders to a .zip
Player.Message(p, "Server backup (Everything but Database) started. Please wait while backup finishes.");
Save(true, false, p);
} else if (type == "lite") {
Player.Message(p, "Server backup (Everything but BlockDB tables and undo files) started. Please wait while backup finishes.");
Save(true, true, p, true);
} else if (type == "table") {
if (args.Length == 2) { Player.Message(p, "You need to provide the table name to backup."); return; }
if (!Formatter.ValidName(p, args[2], "table")) return;
if (!Database.TableExists(args[2])) { Player.Message(p, "Table \"{0}\" does not exist.", args[2]); return; }
Player.Message(p, "Backing up table {0} started. Please wait while backup finishes.", args[2]);
using (StreamWriter sql = new StreamWriter(args[2] + ".sql"))
Backup.BackupTable(args[2], sql);
Player.Message(p, "Finished backing up table {0}.", args[2]);
} else {
Help(p);
}
}
static void DoRestore(Player p, string[] args) {
if (!CheckPerms(p)) {
Player.Message(p, "Only Console or the Server Owner can restore the server.");
return;
}
Thread extract = new Thread(new ParameterizedThreadStart(Backup.ExtractPackage));
extract.Name = "MCG_RestoreServer";
extract.Start(p);
}
void DoImport(Player p, string[] args) {
if (args.Length == 1) { Player.Message(p, "You need to provide the table name to import."); return; }
if (!Formatter.ValidName(p, args[1], "table")) return;
if (!File.Exists(args[1] + ".sql")) { Player.Message(p, "File \"{0}\".sql does not exist.", args[1]); return; }
Player.Message(p, "Importing table {0} started. Please wait while import finishes.", args[1]);
using (Stream fs = File.OpenRead(args[1] + ".sql"))
Backup.ImportSql(fs);
Player.Message(p, "Finished importing table {0}.", args[1]);
}
static bool CheckPerms(Player p) {
if (p == null) return true;
@ -182,13 +182,13 @@ namespace MCGalaxy.Commands {
Player.Message(p, "%T/server reload %H- Reload the server files. (May require restart) (Owner only)");
Player.Message(p, "%T/server public/private %H- Make the server public or private.");
Player.Message(p, "%T/server restore %H- Restore the server from a backup.");
Player.Message(p, "%T/server backup [all/db/files/lite] %H- Make a backup. (Default all)");
Player.Message(p, "%HBackup options:");
Player.Message(p, " %Hall - Make a backup of the server and all SQL data.");
Player.Message(p, " %Hdb - Just backup the database.");
Player.Message(p, " %Hfiles - Backup everything BUT the database.");
Player.Message(p, "%T/server backup all/db/files/lite %H- Make a backup.");
Player.Message(p, " %Hall - Backups everything (default)");
Player.Message(p, " %Hdb - Only backups the database.");
Player.Message(p, " %Hfiles - Backups everything, except the database.");
Player.Message(p, " %Hlite - Backups everything, except BlockDB and undo files.");
Player.Message(p, "%T/server backup table [name] %H- Backups that database table");
Player.Message(p, "%T/server import [name] %H- Imports a backed up database table");
}
}
}

View File

@ -137,7 +137,7 @@ namespace MCGalaxy {
ExtractItem(item, ref errors);
if (item.Uri.ToString().ToLower().Contains("sql.sql")) {
// If it's in there, they backed it up, meaning they want it restored
Backup.fillDatabase(item.GetStream());
Backup.ReplaceDatabase(item.GetStream());
}
}
}

View File

@ -174,18 +174,20 @@ namespace MCGalaxy {
return tableNames;
}
internal static void fillDatabase(Stream stream) {
//Backup
internal static void ReplaceDatabase(Stream sql) {
using (FileStream backup = File.Create("backup.sql"))
BackupDatabase(new StreamWriter(backup), false);
//Delete old
BackupDatabase(new StreamWriter(backup), false); // backup
List<string> tables = GetTables();
foreach (string table in tables)
Database.Backend.DeleteTable(table);
Database.Backend.DeleteTable(table); // drop all tables
ImportSql(sql);
}
internal static void ImportSql(Stream sql) {
// Import data (we only have CREATE TABLE and INSERT INTO statements)
using (StreamReader reader = new StreamReader(stream))
using (StreamReader reader = new StreamReader(sql))
using (BulkTransaction helper = BulkTransaction.Create())
{
List<string> buffer = new List<string>();