Commit 1369dd36970987b2a4ee79f56091a0ffbb9cdb53
1 parent
8288c475
v1.29.2.0
- MoveDbToRemoteServer kiegészítése
Showing
2 changed files
with
181 additions
and
73 deletions
Show diff stats
Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs
| ... | ... | @@ -135,7 +135,6 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
| 135 | 135 | SQLDataBase sqld = p.Parameters as SQLDataBase; |
| 136 | 136 | try |
| 137 | 137 | { |
| 138 | - | |
| 139 | 138 | SQLDataBaseManagerCore.BackupSqlScripts(sqld,TS); |
| 140 | 139 | ColorConsole.WriteLine($"SQLDB code scripts created. Name:{sqld.Xml_Description}", ConsoleColor.Green); |
| 141 | 140 | } |
| ... | ... | @@ -633,16 +632,57 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
| 633 | 632 | |
| 634 | 633 | var selectedsqldbindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.SQLDataBaseManager.Function.CMD_DATABASES); |
| 635 | 634 | |
| 636 | - selectionloop: | |
| 635 | + (bool Exit, bool Yes) yesornoanswer; | |
| 636 | + (bool Exit, bool Yes, SQLDataBase Dbfrom, SQLDataBase Dbto, string Selecteddbname, bool Emulation) movedbanswer; | |
| 637 | + while (true) | |
| 638 | + { | |
| 639 | + var DateTimeNow = DateTime.Now; | |
| 640 | + | |
| 641 | + movedbanswer = YesOrNo_MoveDbToRemoteServer(config, selectedsqldbindexes); | |
| 642 | + if (movedbanswer.Exit) break; | |
| 643 | + if (movedbanswer.Yes) | |
| 644 | + { | |
| 645 | + var movesuccess = SQLDataBaseManagerCore.MoveDbToRemoteServer(movedbanswer.Dbfrom, movedbanswer.Dbto, movedbanswer.Selecteddbname, movedbanswer.Emulation); | |
| 646 | + if (!movesuccess) continue; | |
| 647 | + } | |
| 648 | + | |
| 649 | + var usernamelist = new string[] { "corplear\\lschwirg", "corplear\\gen_vrhalmadmin", $"corplear\\{Program.ThisComputer.ComputerName}$" }; | |
| 650 | + | |
| 651 | + yesornoanswer = YesOrNo_ServerLogin(movedbanswer.Dbfrom.SQLCS, usernamelist); | |
| 652 | + if (yesornoanswer.Exit) return o; | |
| 653 | + if (yesornoanswer.Yes) SQLDataBaseManagerCore.CreateServerLogins(movedbanswer.Emulation, movedbanswer.Dbfrom.SQLCS, usernamelist, loginrolelist,fullaccessrolelist); | |
| 654 | + | |
| 655 | + yesornoanswer = YesOrNo_ServerLogin(movedbanswer.Dbto.SQLCS, usernamelist); | |
| 656 | + if (yesornoanswer.Exit) return o; | |
| 657 | + if (yesornoanswer.Yes) SQLDataBaseManagerCore.CreateServerLogins(movedbanswer.Emulation, movedbanswer.Dbto.SQLCS, usernamelist, loginrolelist, fullaccessrolelist); | |
| 658 | + | |
| 659 | + yesornoanswer = YesOrNo_SetDbOffline(movedbanswer.Dbfrom.SQLCS, null); | |
| 660 | + if (yesornoanswer.Exit) return o; | |
| 661 | + if (yesornoanswer.Yes) SQLDataBaseManagerCore.SetDatabaseOffline(movedbanswer.Dbfrom.SQLCS); | |
| 662 | + | |
| 663 | + ColorConsole.WriteLine($"Moving DB to remote server completed. Required time: {(int)(DateTime.Now.Subtract(DateTimeNow).TotalSeconds)} seconds.", ConsoleColor.Green); | |
| 664 | + } | |
| 665 | + return o; | |
| 666 | + } | |
| 667 | + private static (bool,bool,SQLDataBase, SQLDataBase, string, bool) YesOrNo_MoveDbToRemoteServer(SQLDataBaseManagerXmlProcessor config,string selectedsqldbindexes) | |
| 668 | + { | |
| 669 | + (bool, bool, SQLDataBase, SQLDataBase, string, bool) returnvalueexit = (true, false, null, null, null, false); | |
| 670 | + | |
| 671 | + selectionloop: | |
| 672 | + | |
| 673 | + ColorConsole.WriteLine($"Do You want to move ANY database from local server to remote server?", ConsoleColor.Yellow); | |
| 674 | + var yesorno = ColorConsole.ReadLine("Move?", ConsoleColor.Yellow, validitylist: new List<string> { "yes", "no" }, defaultvalue: "yes"); | |
| 675 | + if (yesorno.ToUpper() == "EX") return returnvalueexit; | |
| 676 | + | |
| 637 | 677 | var menufolders = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function. '{nameof(MoveDbToRemoteServer)}'! Format:FROMLOCALDB,TOREMOTEDB", silent: true, selectionmode: Menu.SelectionMode.Multi); |
| 638 | 678 | |
| 639 | 679 | Menu.Selection sr = menufolders.Select(selectedsqldbindexes); |
| 640 | - if (sr.Result == Menu.SelectionResult.Exit) { return o; } | |
| 680 | + if (sr.Result == Menu.SelectionResult.Exit) { return returnvalueexit; } | |
| 641 | 681 | else if (sr.Result == Menu.SelectionResult.None) { goto selectionloop; } |
| 642 | 682 | else if (sr.Result == Menu.SelectionResult.Error) { goto selectionloop; } |
| 643 | 683 | else if (sr.Result == Menu.SelectionResult.Ok && sr.SelectedParameterList.FirstOrDefault() == null) { goto selectionloop; } |
| 644 | - else if (sr.Result == Menu.SelectionResult.Ok && sr.SelectedParameterList.Count() != 2) | |
| 645 | - { | |
| 684 | + else if (sr.Result == Menu.SelectionResult.Ok && sr.SelectedParameterList.Count() != 2) | |
| 685 | + { | |
| 646 | 686 | ColorConsole.WriteLine($"Select exactly 2 DB. Format:FROMLOCALDB,TOREMOTEDB", ConsoleColor.Red); |
| 647 | 687 | goto selectionloop; |
| 648 | 688 | } |
| ... | ... | @@ -652,7 +692,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
| 652 | 692 | var spto = sr.SelectedParameterList.ElementAt(1); |
| 653 | 693 | SQLDataBase dbfrom = spfrom.Parameters as SQLDataBase; |
| 654 | 694 | SQLDataBase dbto = spto.Parameters as SQLDataBase; |
| 655 | - if (!dbto.Xml_IsRemoteDB) | |
| 695 | + if (!dbto.Xml_IsRemoteDB) | |
| 656 | 696 | { |
| 657 | 697 | ColorConsole.WriteLine($"TOREMOTEDB '{dbto.SQLCS}' has to be remote!", ConsoleColor.Red); |
| 658 | 698 | goto selectionloop; |
| ... | ... | @@ -662,64 +702,39 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
| 662 | 702 | ColorConsole.WriteLine($"FROMLOCALDB '{dbfrom.SQLCS}' has to be local!", ConsoleColor.Red); |
| 663 | 703 | goto selectionloop; |
| 664 | 704 | } |
| 705 | + if (yesorno.ToUpper() != "YES") return (false, false,dbfrom,dbto, null,false); | |
| 706 | + | |
| 665 | 707 | bool emulation = false; |
| 666 | - | |
| 667 | 708 | var selecteddbname = ColorConsole.ReadLine($"Enter the name of the restored DB. EMPTY={dbto.DBName}, EX=exit."); |
| 668 | - if (selecteddbname.ToUpper() == "EX") return o; | |
| 669 | - else if (string.IsNullOrWhiteSpace(selecteddbname)) selecteddbname=dbto.DBName; | |
| 709 | + if (selecteddbname.ToUpper() == "EX") return returnvalueexit; | |
| 710 | + else if (string.IsNullOrWhiteSpace(selecteddbname)) selecteddbname = dbto.DBName; | |
| 670 | 711 | |
| 671 | 712 | confirmloop:; |
| 672 | 713 | var selection = ColorConsole.ReadLine("Enter CONFIRM to start, EMU to emulate, EX to exit."); |
| 673 | - if (selection.ToUpper() == "EX") return o; | |
| 714 | + if (selection.ToUpper() == "EX") return returnvalueexit; | |
| 674 | 715 | else if (selection.ToUpper() == "EMU") emulation = true; |
| 675 | 716 | else if (selection.ToUpper() == "CONFIRM") { } |
| 676 | 717 | else goto confirmloop; |
| 677 | - | |
| 678 | - var DateTimeNow = DateTime.Now; | |
| 679 | - var usernameList = new string[] { "corplear\\lschwirg", "corplear\\gen_vrhalmadmin", $"corplear\\{Program.ThisComputer.ComputerName}$" }; | |
| 680 | - foreach (var username in usernameList) | |
| 681 | - { | |
| 682 | - ColorConsole.WriteLine(username, ConsoleColor.Yellow, prefix: nameof(SQLDataBaseManagerCore.CreateUser) + ":"); | |
| 683 | - try { if (!emulation) SQLDataBaseManagerCore.CreateLogin(dbfrom.SQLCS, username, null, "master", LoginType.WindowsUser, loginrolelist); } | |
| 684 | - catch (Exception ex) { ColorConsole.WriteLine(nameof(SQLDataBaseManagerCore.CreateLogin)+">>>: "+ex.MessageNested(), ConsoleColor.Red); } | |
| 685 | - try { if (!emulation) SQLDataBaseManagerCore.CreateUser(dbfrom.SQLCS, username, string.Join(",", fullaccessrolelist), "w"); } | |
| 686 | - catch (Exception ex) { ColorConsole.WriteLine(nameof(SQLDataBaseManagerCore.CreateUser) + "\n" + ex.MessageNested(), ConsoleColor.Red); } | |
| 687 | - } | |
| 688 | - | |
| 689 | - //backup dbfrom | |
| 690 | - var backupfileFullname = SQLDataBaseManagerCore.CreateBackup(dbfrom, false, DateTimeNow,emulation); | |
| 691 | - //move dbfrom to tranit area | |
| 692 | - | |
| 693 | - if (!emulation && !File.Exists(backupfileFullname)) { goto selectionloop; } | |
| 694 | - | |
| 695 | - string backupfileFilename = null; | |
| 696 | - string tranzitfileFullname = null; | |
| 697 | - try | |
| 698 | - { | |
| 699 | - backupfileFilename = Path.GetFileName(backupfileFullname); | |
| 700 | - tranzitfileFullname = Path.Combine(dbto.Xml_TranzitDirectoryPath, backupfileFilename); | |
| 701 | - ColorConsole.WriteLine($"Moving backup file {backupfileFullname} to tranzit location: {dbto.Xml_TranzitDirectoryPath}", ConsoleColor.Yellow); | |
| 702 | - if (!emulation) | |
| 703 | - { | |
| 704 | - if (File.Exists(tranzitfileFullname)) { File.Delete(tranzitfileFullname); } | |
| 705 | - File.Move(backupfileFullname, tranzitfileFullname); | |
| 706 | - ColorConsole.WriteLine($"...completed...", ConsoleColor.Yellow); | |
| 707 | - } | |
| 708 | - } | |
| 709 | - catch (Exception ex) | |
| 710 | - { | |
| 711 | - ColorConsole.WriteLine($"Moving file FAILED!", ConsoleColor.Red); | |
| 712 | - ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); | |
| 713 | - goto selectionloop; | |
| 714 | - } | |
| 715 | - if (!emulation && !File.Exists(tranzitfileFullname)) { goto selectionloop; } | |
| 716 | - | |
| 717 | - //restore backup to dbto | |
| 718 | - tranzitfileFullname = Path.Combine(dbto.Xml_TranzitDirectoryPath, backupfileFilename); | |
| 719 | - SQLDataBaseManagerCore.RestoreBackup(dbto, tranzitfileFullname, dbto.Xml_PhysicalFilesDirectoryPath, false, selecteddbname, emulation); | |
| 720 | - | |
| 721 | - ColorConsole.WriteLine($"Moving DB to remote server completed. Required time: {(int)(DateTime.Now.Subtract(DateTimeNow).TotalSeconds)} seconds.",ConsoleColor.Green); | |
| 722 | - return o; | |
| 718 | + return (false,true,dbfrom,dbto, selecteddbname,emulation); | |
| 719 | + } | |
| 720 | + private static (bool,bool) YesOrNo_SetDbOffline(string sqlcs, string dbname) | |
| 721 | + { | |
| 722 | + dbname = dbname ?? SQLDataBaseManagerCore.GetDBName(sqlcs); | |
| 723 | + var servername = SQLDataBaseManagerCore.GetDataSource(sqlcs); | |
| 724 | + ColorConsole.WriteLine($"Do You want to set database '{dbname}' OFFLINE on server '{servername}'?", ConsoleColor.Yellow); | |
| 725 | + ColorConsole.WriteLine($" SQL connection string: {sqlcs}", ConsoleColor.Yellow); | |
| 726 | + ColorConsole.WriteLine($" Server/database: {servername}/{dbname}", ConsoleColor.Yellow); | |
| 727 | + var yesorno = ColorConsole.ReadLine("Set offline?", ConsoleColor.Yellow, validitylist: new List<string> { "yes", "no" }, defaultvalue: "yes"); | |
| 728 | + return (yesorno.ToUpper() == "EX", yesorno.ToUpper() == "YES"); | |
| 729 | + } | |
| 730 | + private static (bool,bool) YesOrNo_ServerLogin(string sqlcs,string[] usernamelist) | |
| 731 | + { | |
| 732 | + ColorConsole.WriteLine($"Do You want to create the following mandatory server logins?", ConsoleColor.Yellow); | |
| 733 | + ColorConsole.WriteLine($" SQL connection string: {sqlcs}", ConsoleColor.Yellow); | |
| 734 | + ColorConsole.WriteLine($" Server: {SQLDataBaseManagerCore.GetDataSource(sqlcs)}", ConsoleColor.Yellow); | |
| 735 | + ColorConsole.WriteLine($" Username list: " + string.Join(",", usernamelist), ConsoleColor.Yellow); | |
| 736 | + var yesorno = ColorConsole.ReadLine("Create login?", ConsoleColor.Yellow, validitylist: new List<string> { "yes", "no" }, defaultvalue: "yes"); | |
| 737 | + return (yesorno.ToUpper() == "EX", yesorno.ToUpper() == "YES"); | |
| 723 | 738 | } |
| 724 | 739 | #endregion MoveDbToRemoteServer |
| 725 | 740 | |
| ... | ... | @@ -1153,7 +1168,8 @@ GO |
| 1153 | 1168 | if (restoredbname.ToUpper() == "EX") { continue; } |
| 1154 | 1169 | else if (string.IsNullOrWhiteSpace(restoredbname)) { restoredbname = sqld.DBName; } |
| 1155 | 1170 | ColorConsole.WriteLine("Enter the path for the DB physical files.", ConsoleColor.Yellow); |
| 1156 | - ColorConsole.WriteLine(sqld.PhysicalFilesDirectoryPath, ConsoleColor.Yellow, prefix: $" Empty=current location of source DB: ", bracket: "[]"); | |
| 1171 | + var PhysicalFilesDirectoryPathcolor = sqld.PhysicalFilesDirectoryPath.StartsWith("ERR") ? ConsoleColor.Red : ConsoleColor.Yellow; | |
| 1172 | + ColorConsole.WriteLine(sqld.PhysicalFilesDirectoryPath, PhysicalFilesDirectoryPathcolor, prefix: $" Empty=current location of source DB: ", bracket: "[]"); | |
| 1157 | 1173 | ColorConsole.WriteLine(SQLDataBaseManagerCore.GetServerDefaultPhysicalDATFileLocation(sqld.SQLCS)??"???", ConsoleColor.Yellow,prefix: $" DEFAULT= sql server default location.",bracket:"[]"); |
| 1158 | 1174 | var targetdirectory = ColorConsole.ReadLine($"Enter the target path.EX=exit.", ConsoleColor.Yellow, suffix: " --> "); |
| 1159 | 1175 | if (targetdirectory.ToUpper() == "EX") { continue; } |
| ... | ... | @@ -1224,13 +1240,15 @@ GO |
| 1224 | 1240 | } |
| 1225 | 1241 | else if (lineix == 3) |
| 1226 | 1242 | { |
| 1227 | - ColorConsole.Write($"{st.PhysicalFilesDirectoryPath}", ConsoleColor.Yellow, prefix: "DB files physical location:", suffix: " "); | |
| 1243 | + var PhysicalFilesDirectoryPathcolor = st.PhysicalFilesDirectoryPath.StartsWith("ERR") ? ConsoleColor.Red : ConsoleColor.Yellow; | |
| 1244 | + ColorConsole.Write($"{st.PhysicalFilesDirectoryPath}", PhysicalFilesDirectoryPathcolor, prefix: "DB files physical location:", suffix: " "); | |
| 1228 | 1245 | ColorConsole.WriteLine(" "); |
| 1229 | 1246 | return " "; |
| 1230 | 1247 | } |
| 1231 | 1248 | else if (lineix == 4) |
| 1232 | 1249 | { |
| 1233 | - ColorConsole.Write(st.SizeString, ConsoleColor.Yellow, prefix: "DB size:", suffix: " "); | |
| 1250 | + var SizeStringcolor = st.SizeString.StartsWith("ERR") ? ConsoleColor.Red : ConsoleColor.Yellow; | |
| 1251 | + ColorConsole.Write(st.SizeString, SizeStringcolor, prefix: "DB size:", suffix: " "); | |
| 1234 | 1252 | ColorConsole.WriteLine(" "); |
| 1235 | 1253 | return " "; |
| 1236 | 1254 | } |
| ... | ... | @@ -1253,6 +1271,12 @@ GO |
| 1253 | 1271 | #region class SQLDataBaseManager |
| 1254 | 1272 | public static class SQLDataBaseManagerCore |
| 1255 | 1273 | { |
| 1274 | + /// <summary> | |
| 1275 | + /// Egy adatbázis fizikai file-jainak mozgatása egy másik lokációba | |
| 1276 | + /// </summary> | |
| 1277 | + /// <param name="sqld"></param> | |
| 1278 | + /// <param name="targetdirectory"></param> | |
| 1279 | + /// <param name="restoredbname"></param> | |
| 1256 | 1280 | public static void RelocatePhysicalFiles(SQLDataBase sqld, string targetdirectory, string restoredbname) |
| 1257 | 1281 | { |
| 1258 | 1282 | var dbbackupfilepath = CreateBackup(sqld, false,null); |
| ... | ... | @@ -1260,6 +1284,52 @@ GO |
| 1260 | 1284 | RestoreBackup(sqld, dbbackupfilepath, targetdirectory, false, restoredbname); |
| 1261 | 1285 | if (File.Exists(dbbackupfilepath)) { File.Delete(dbbackupfilepath); } |
| 1262 | 1286 | } |
| 1287 | + | |
| 1288 | + /// <summary> | |
| 1289 | + /// Egy adatbázis átmozgatása egy távoli szerverre | |
| 1290 | + /// </summary> | |
| 1291 | + /// <param name="dbfrom"></param> | |
| 1292 | + /// <param name="dbto"></param> | |
| 1293 | + /// <param name="selecteddbname"></param> | |
| 1294 | + /// <param name="emulation"></param> | |
| 1295 | + /// <returns></returns> | |
| 1296 | + public static bool MoveDbToRemoteServer(SQLDataBase dbfrom, SQLDataBase dbto, string selecteddbname, bool emulation) | |
| 1297 | + { | |
| 1298 | + //backup dbfrom | |
| 1299 | + var DateTimeNow = DateTime.Now; | |
| 1300 | + var backupfileFullname = SQLDataBaseManagerCore.CreateBackup(dbfrom, false, DateTimeNow, emulation); | |
| 1301 | + //move dbfrom to tranit area | |
| 1302 | + | |
| 1303 | + if (!emulation && !File.Exists(backupfileFullname)) { return false; } | |
| 1304 | + | |
| 1305 | + string backupfileFilename = null; | |
| 1306 | + string tranzitfileFullname = null; | |
| 1307 | + try | |
| 1308 | + { | |
| 1309 | + backupfileFilename = Path.GetFileName(backupfileFullname); | |
| 1310 | + tranzitfileFullname = Path.Combine(dbto.Xml_TranzitDirectoryPath, backupfileFilename); | |
| 1311 | + ColorConsole.WriteLine($"Moving backup file {backupfileFullname} to tranzit location: {dbto.Xml_TranzitDirectoryPath}", ConsoleColor.Yellow); | |
| 1312 | + if (!emulation) | |
| 1313 | + { | |
| 1314 | + if (File.Exists(tranzitfileFullname)) { File.Delete(tranzitfileFullname); } | |
| 1315 | + File.Move(backupfileFullname, tranzitfileFullname); | |
| 1316 | + ColorConsole.WriteLine($"...completed...", ConsoleColor.Yellow); | |
| 1317 | + } | |
| 1318 | + } | |
| 1319 | + catch (Exception ex) | |
| 1320 | + { | |
| 1321 | + ColorConsole.WriteLine($"Moving file FAILED!", ConsoleColor.Red); | |
| 1322 | + ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); | |
| 1323 | + return false; | |
| 1324 | + } | |
| 1325 | + if (!emulation && !File.Exists(tranzitfileFullname)) { return false; } | |
| 1326 | + | |
| 1327 | + //restore backup to dbto | |
| 1328 | + tranzitfileFullname = Path.Combine(dbto.Xml_TranzitDirectoryPath, backupfileFilename); | |
| 1329 | + SQLDataBaseManagerCore.RestoreBackup(dbto, tranzitfileFullname, dbto.Xml_PhysicalFilesDirectoryPath, false, selecteddbname, emulation); | |
| 1330 | + return true; | |
| 1331 | + } | |
| 1332 | + | |
| 1263 | 1333 | /// <summary> |
| 1264 | 1334 | /// Restores database backup (zipped or normal) |
| 1265 | 1335 | /// </summary> |
| ... | ... | @@ -1316,18 +1386,23 @@ GO |
| 1316 | 1386 | smoRestore.PercentCompleteNotification = 1; |
| 1317 | 1387 | var backupdevice = new BackupDeviceItem(normalbackupfilepath, DeviceType.File); |
| 1318 | 1388 | smoRestore.Devices.Add(backupdevice); |
| 1319 | - smoRestore.Database = string.IsNullOrWhiteSpace(restoretodbname) | |
| 1320 | - ? smoRestore.ReadBackupHeader(sqlserver).Rows[0]["DatabaseName"].ToString() | |
| 1321 | - : restoretodbname; | |
| 1322 | - | |
| 1389 | + smoRestore.Database = restoretodbname; | |
| 1390 | + if (string.IsNullOrWhiteSpace(smoRestore.Database)) | |
| 1391 | + { | |
| 1392 | + smoRestore.Database = smoRestore.ReadBackupHeader(sqlserver).Rows[0]["DatabaseName"].ToString(); | |
| 1393 | + ColorConsole.WriteLine($"ReadBackupHeader completed. Database name in backup header:{smoRestore.Database}", ConsoleColor.Yellow); | |
| 1394 | + } | |
| 1323 | 1395 | |
| 1324 | 1396 | var dbfilelist = smoRestore.ReadFileList(sqlserver); |
| 1397 | + ColorConsole.WriteLine($"ReadFileList completed.", ConsoleColor.Yellow); | |
| 1325 | 1398 | var smorestoreDATfile = new RelocateFile(); |
| 1326 | 1399 | string targetdbphysicalfilesdirectorypathDAT = targetdbphysicalfilesdirectorypath; |
| 1327 | 1400 | if (string.IsNullOrEmpty(targetdbphysicalfilesdirectorypathDAT)) { targetdbphysicalfilesdirectorypathDAT = sqlserver.DefaultFile; } |
| 1328 | 1401 | if (string.IsNullOrEmpty(targetdbphysicalfilesdirectorypathDAT)) { targetdbphysicalfilesdirectorypathDAT = sqlserver.MasterDBPath; } |
| 1329 | 1402 | smorestoreDATfile.PhysicalFileName = Path.Combine(targetdbphysicalfilesdirectorypathDAT, smoRestore.Database + "_Data.mdf"); |
| 1330 | 1403 | smorestoreDATfile.LogicalFileName = dbfilelist.Select("Type='D'")[0]["LogicalName"].ToString(); |
| 1404 | + ColorConsole.WriteLine($"DAT PhysicalFileName to relocate: {smorestoreDATfile.PhysicalFileName}", ConsoleColor.Yellow); | |
| 1405 | + ColorConsole.WriteLine($"DAT LogicalFileName to relocate: {smorestoreDATfile.LogicalFileName}", ConsoleColor.Yellow); | |
| 1331 | 1406 | smoRestore.RelocateFiles.Add(smorestoreDATfile); |
| 1332 | 1407 | |
| 1333 | 1408 | var smorestoreLOGfile = new RelocateFile(); |
| ... | ... | @@ -1336,6 +1411,8 @@ GO |
| 1336 | 1411 | if (string.IsNullOrEmpty(targetdbphysicalfilesdirectorypathLOG)) { targetdbphysicalfilesdirectorypathLOG = sqlserver.MasterDBLogPath; } |
| 1337 | 1412 | smorestoreLOGfile.PhysicalFileName = Path.Combine(targetdbphysicalfilesdirectorypathLOG, smoRestore.Database + "_Log.ldf"); |
| 1338 | 1413 | smorestoreLOGfile.LogicalFileName = dbfilelist.Select("Type='L'")[0]["LogicalName"].ToString(); |
| 1414 | + ColorConsole.WriteLine($"LOG PhysicalFileName to relocate: {smorestoreLOGfile.PhysicalFileName}", ConsoleColor.Yellow); | |
| 1415 | + ColorConsole.WriteLine($"LOG LogicalFileName to relocate: {smorestoreLOGfile.LogicalFileName}", ConsoleColor.Yellow); | |
| 1339 | 1416 | smoRestore.RelocateFiles.Add(smorestoreLOGfile); |
| 1340 | 1417 | |
| 1341 | 1418 | GetExclusiveUse(smoRestore.Database, sqlserver, sc); |
| ... | ... | @@ -1469,7 +1546,6 @@ GO |
| 1469 | 1546 | ColorConsole.Write($"Completed: {e.Percent}%. {e.Message}"); |
| 1470 | 1547 | } |
| 1471 | 1548 | |
| 1472 | - #region private methods | |
| 1473 | 1549 | public static List<FileInfo> GetBackupFilePathList(SQLDataBase sqld) |
| 1474 | 1550 | { |
| 1475 | 1551 | var filenamemask = Path.GetFileNameWithoutExtension(sqld.Xml_BackupFileNameMask); |
| ... | ... | @@ -1619,6 +1695,17 @@ GO |
| 1619 | 1695 | } |
| 1620 | 1696 | finally { sqlserver?.ConnectionContext.SqlConnectionObject.Dispose(); } |
| 1621 | 1697 | } |
| 1698 | + public static void CreateServerLogins(bool emulation, string sqlcs, string[] usernamelist,string[] loginrolelist, string[] fullaccessrolelist) | |
| 1699 | + { | |
| 1700 | + foreach (var username in usernamelist) | |
| 1701 | + { | |
| 1702 | + ColorConsole.WriteLine(username, ConsoleColor.Yellow, prefix: nameof(SQLDataBaseManagerCore.CreateUser) + ":"); | |
| 1703 | + try { if (!emulation) SQLDataBaseManagerCore.CreateLogin(sqlcs, username, null, "master", LoginType.WindowsUser, loginrolelist); } | |
| 1704 | + catch (Exception ex) { ColorConsole.WriteLine(nameof(SQLDataBaseManagerCore.CreateLogin) + ">>>: " + ex.MessageNested(), ConsoleColor.Red); } | |
| 1705 | + try { if (!emulation) SQLDataBaseManagerCore.CreateUser(sqlcs, username, string.Join(",", fullaccessrolelist), "w"); } | |
| 1706 | + catch (Exception ex) { ColorConsole.WriteLine(nameof(SQLDataBaseManagerCore.CreateUser) + "\n" + ex.MessageNested(), ConsoleColor.Red); } | |
| 1707 | + } | |
| 1708 | + } | |
| 1622 | 1709 | |
| 1623 | 1710 | public static void ConfigureWindowsUser(string cs, string sapassword, string databasename, string windowsfullusername,string windowsuserpassword,List<string> rolenamelist) |
| 1624 | 1711 | { |
| ... | ... | @@ -1699,15 +1786,33 @@ GO |
| 1699 | 1786 | } |
| 1700 | 1787 | public static void GetExclusiveUse(string databasename,Server sqlserver,SqlConnection sc) |
| 1701 | 1788 | { |
| 1702 | - try { sqlserver.KillAllProcesses(databasename); } | |
| 1789 | + try | |
| 1790 | + { | |
| 1791 | + sqlserver.KillAllProcesses(databasename); | |
| 1792 | + ColorConsole.WriteLine($"{nameof(GetExclusiveUse)}/KillAllProcesses completed.", ConsoleColor.Yellow); | |
| 1793 | + } | |
| 1703 | 1794 | catch (Exception ex) { ColorConsole.WriteLine($"{nameof(GetExclusiveUse)}/{nameof(Server.KillAllProcesses)}>>>:" + ex.MessageNested(), ConsoleColor.Red); } |
| 1704 | 1795 | |
| 1705 | 1796 | SqlCommand sqlcommand = null; |
| 1706 | - try { sqlcommand = new SqlCommand($"ALTER DATABASE {databasename} SET Single_User WITH Rollback IMMEDIATE", sc); sqlcommand.ExecuteNonQuery(); } | |
| 1797 | + try | |
| 1798 | + { | |
| 1799 | + var cmd = $"ALTER DATABASE {databasename} SET Single_User WITH Rollback IMMEDIATE"; | |
| 1800 | + sqlcommand = new SqlCommand(cmd, sc); sqlcommand.ExecuteNonQuery(); | |
| 1801 | + ColorConsole.WriteLine($"{nameof(GetExclusiveUse)}/{nameof(SqlCommand.ExecuteNonQuery)}/\"{cmd}\" completed.", ConsoleColor.Yellow); | |
| 1802 | + } | |
| 1707 | 1803 | catch (Exception ex) { ColorConsole.WriteLine($"{nameof(GetExclusiveUse)}/{nameof(SqlCommand.ExecuteNonQuery)}>>>: " + ex.MessageNested(), ConsoleColor.Red); } |
| 1708 | 1804 | finally { sqlcommand?.Dispose(); } |
| 1709 | 1805 | } |
| 1710 | 1806 | |
| 1807 | + public static void SetDatabaseOffline(string sqlcs, string dbname=null) | |
| 1808 | + { | |
| 1809 | + dbname = dbname ?? SQLDataBaseManagerCore.GetDBName(sqlcs); | |
| 1810 | + var sc = ServerConnectionPool.GetSqlConnection(sqlcs, "master"); | |
| 1811 | + var cmd = $"ALTER DATABASE {dbname} SET OFFLINE WITH ROLLBACK IMMEDIATE"; | |
| 1812 | + //var cmd = $"ALTER DATABASE {dbname ?? SQLDataBaseManagerCore.GetDBName(sqlcs)} SET ONLINE"; | |
| 1813 | + var sqlcommand = new SqlCommand(cmd, sc); sqlcommand.ExecuteNonQuery(); | |
| 1814 | + } | |
| 1815 | + | |
| 1711 | 1816 | public static string GetServerDefaultPhysicalDATFileLocation(string cs) |
| 1712 | 1817 | { |
| 1713 | 1818 | Server sqlserver = null; |
| ... | ... | @@ -2186,7 +2291,6 @@ GO |
| 2186 | 2291 | } |
| 2187 | 2292 | } |
| 2188 | 2293 | #endregion BackupSqlData |
| 2189 | - #endregion private methods | |
| 2190 | 2294 | } |
| 2191 | 2295 | #endregion class SQLDataBaseManager |
| 2192 | 2296 | |
| ... | ... | @@ -2263,7 +2367,7 @@ GO |
| 2263 | 2367 | try |
| 2264 | 2368 | { |
| 2265 | 2369 | GetPhysicalFilesLocationAndSize(this.SQLCS, out this.SizeString, out this.PhysicalFilesDirectoryPath); |
| 2266 | - this.Status = this.PhysicalFilesDirectoryPath=="ERR" ? SQLDBStatus.NoAccess : SQLDBStatus.OK; | |
| 2370 | + this.Status = this.PhysicalFilesDirectoryPath.StartsWith("ERR") ? SQLDBStatus.NoAccess : SQLDBStatus.OK; | |
| 2267 | 2371 | this.DBName = SQLDataBaseManagerCore.GetDBName(this.SQLCS); |
| 2268 | 2372 | this.DataSource = SQLDataBaseManagerCore.GetDataSource(this.SQLCS); |
| 2269 | 2373 | } |
| ... | ... | @@ -2305,7 +2409,7 @@ GO |
| 2305 | 2409 | } |
| 2306 | 2410 | dbsizetext = dbsizetext ?? "N/A"; |
| 2307 | 2411 | } |
| 2308 | - catch { } | |
| 2412 | + catch (Exception ex) {dbsizetext = $"ERROR! {nameof(GetPhysicalFilesLocationAndSize)} getting size." + ex.MessageNested() + $" sqlconnectionstring:{sqlconnectionstring}";} | |
| 2309 | 2413 | |
| 2310 | 2414 | try |
| 2311 | 2415 | { |
| ... | ... | @@ -2322,10 +2426,14 @@ GO |
| 2322 | 2426 | var result = SQLDataBaseManagerCore.ExecuteSQLScript(sqlc, scripttext, commandtimeout, vars); |
| 2323 | 2427 | if (result.ReturnValue != 0) { dbpathtext = Path.GetDirectoryName(result.ReturnMessage); } |
| 2324 | 2428 | } |
| 2325 | - catch { } | |
| 2429 | + catch (Exception ex) {dbpathtext =$"ERROR! {nameof(GetPhysicalFilesLocationAndSize)} getting location." + ex.MessageNested() + $" sqlconnectionstring:{sqlconnectionstring}";} | |
| 2326 | 2430 | sqlc.Close(); |
| 2327 | 2431 | } |
| 2328 | - catch { } | |
| 2432 | + catch (Exception ex) | |
| 2433 | + { | |
| 2434 | + dbpathtext = $"ERROR! {nameof(GetPhysicalFilesLocationAndSize)}." + ex.MessageNested() + $" sqlconnectionstring:{sqlconnectionstring}"; | |
| 2435 | + dbsizetext = null; | |
| 2436 | + } | |
| 2329 | 2437 | dbsizetext = dbsizetext ?? "ERR"; |
| 2330 | 2438 | dbpathtext = dbpathtext ?? "ERR"; |
| 2331 | 2439 | } | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs
| ... | ... | @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; |
| 32 | 32 | // You can specify all the values or you can default the Build and Revision Numbers |
| 33 | 33 | // by using the '*' as shown below: |
| 34 | 34 | // [assembly: AssemblyVersion("1.0.*")] |
| 35 | -[assembly: AssemblyVersion("1.29.1.0")] | |
| 36 | -[assembly: AssemblyFileVersion("1.29.1.0")] | |
| 35 | +[assembly: AssemblyVersion("1.29.2.0")] | |
| 36 | +[assembly: AssemblyFileVersion("1.29.2.0")] | ... | ... |