Commit 22ea3e7ebaf982e9cf9ab1e61344906f92e51928
1 parent
2f5d8765
v1.4.0: database shrink function is implemented
Showing
7 changed files
with
145 additions
and
17 deletions
Show diff stats
Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs
... | ... | @@ -281,6 +281,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS |
281 | 281 | public static class RestoreDataBase { public const string KEY = "RES"; public const string CMD_RESTOREFIRST = "-RESTOREFIRST"; |
282 | 282 | } |
283 | 283 | public static class RelocatePhysicalFiles { public const string KEY = "COP"; } |
284 | + public static class ShrinkDB { public const string KEY = "SHR"; } | |
284 | 285 | } |
285 | 286 | } |
286 | 287 | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools.cs
... | ... | @@ -210,27 +210,46 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS |
210 | 210 | Process.Start(startinfo); |
211 | 211 | } |
212 | 212 | public enum ShutDownMode { Sleep, Hibernation,Lock,Logoff,Restart,ShutDown,} |
213 | - public static string HKLM_GetString(string path, string key) | |
213 | + private static string HKLM_GetString(string path, string key) | |
214 | 214 | { |
215 | 215 | try |
216 | 216 | { |
217 | - RegistryKey rk = Registry.LocalMachine.OpenSubKey(path); | |
218 | - if (rk == null) { return ""; } | |
217 | + Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path); | |
218 | + if (rk == null) return ""; | |
219 | 219 | return (string)rk.GetValue(key); |
220 | 220 | } |
221 | - catch { return ""; } | |
221 | + catch { return null; } | |
222 | 222 | } |
223 | 223 | |
224 | - public static string OSFriendlyName() | |
224 | + public static string GetOSFriendlyNameA() | |
225 | 225 | { |
226 | 226 | string ProductName = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName"); |
227 | 227 | string CSDVersion = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CSDVersion"); |
228 | - if (ProductName != "") | |
228 | + if (!string.IsNullOrWhiteSpace(ProductName)) | |
229 | 229 | { |
230 | - return (ProductName.StartsWith("Microsoft") ? "" : "Microsoft ") + ProductName + | |
231 | - (CSDVersion != "" ? " " + CSDVersion : ""); | |
230 | + return (ProductName.StartsWith("Microsoft") ? "" : "Microsoft ") + ProductName + (CSDVersion != "" ? " " + CSDVersion : ""); | |
232 | 231 | } |
233 | - return ""; | |
232 | + return null; | |
233 | + } | |
234 | + | |
235 | + public static string GetOSFriendlyNameB() | |
236 | + { | |
237 | + string result = string.Empty; | |
238 | + ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem"); | |
239 | + foreach (ManagementObject os in searcher.Get()) { result = os["Caption"].ToString(); break; } | |
240 | + return string.IsNullOrWhiteSpace(result)? null : result; | |
241 | + } | |
242 | + public static string GetOSFriendlyNameC() | |
243 | + { | |
244 | + return new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName; | |
245 | + } | |
246 | + public static string GetOSType() | |
247 | + { | |
248 | + string os = null; | |
249 | + IEnumerable<string> list64 = System.IO.Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\SysWOW64")); | |
250 | + IEnumerable<string> list32 = System.IO.Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\System32")); | |
251 | + if (list32.Count() > 0) { if (list64.Count() > 0) { os = "64bit"; } else { os = "32bit"; } } | |
252 | + return os; | |
234 | 253 | } |
235 | 254 | } |
236 | 255 | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/Manager - InstallManager.cs
... | ... | @@ -111,12 +111,12 @@ namespace Vrh.Log4Pro.MaintenanceConsole.InstallManagerNS |
111 | 111 | } |
112 | 112 | public static InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values? GetOsCode() |
113 | 113 | { |
114 | - string osfrn = Tools.OSFriendlyName(); | |
114 | + string osfrn = Tools.GetOSFriendlyNameB(); | |
115 | 115 | var defaultvalue = |
116 | 116 | osfrn.StartsWith("Microsoft Windows 10") ? InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values.WS2016.ToString() |
117 | 117 | : osfrn.StartsWith("Microsoft Windows Server 2016") ? InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values.WS2016.ToString() |
118 | 118 | : osfrn.StartsWith("Microsoft Windows Server 2012") ? InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values.WS2016.ToString() |
119 | - : null; | |
119 | + : ""; | |
120 | 120 | var oslist = Enum.GetNames(typeof(InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values)).ToList(); |
121 | 121 | string answer = ColorConsole.ReadLine($"Enter os code (friendly name is: '{osfrn}')!", ConsoleColor.Green, validitylist: oslist, defaultvalue: defaultvalue.ToString(), required: true); |
122 | 122 | if (answer.ToLower() == "ex") { return null; } | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs
... | ... | @@ -48,6 +48,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
48 | 48 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.CreateDataScripts.KEY, "Create data scripts", CreateDataScripts, ep)) |
49 | 49 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.RestoreDataBase.KEY, "Restore database backup", RestoreDataBase, ep)) |
50 | 50 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.RelocatePhysicalFiles.KEY, "Copy database and or relocate its physical files", RelocatePhysicalFiles, ep)) |
51 | + .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.ShrinkDB.KEY, "Shrink database", ShrinkDB, ep)) | |
51 | 52 | .SetSelectionMode(Menu.SelectionMode.Single) |
52 | 53 | .SetMenuHeaderDisplayer(DataBaseListDisplayer); |
53 | 54 | menufunctions.ExecuteMenu(functionkey); |
... | ... | @@ -214,6 +215,49 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
214 | 215 | } |
215 | 216 | return o; |
216 | 217 | } |
218 | + private static object ShrinkDB(object parameter, object o) | |
219 | + { | |
220 | + var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); | |
221 | + var args = (parameter as Menu.ExecutorParameter).Args; | |
222 | + | |
223 | + var selectedtaskindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.ScheduledTaskManager.Function.CMD_TASKS); | |
224 | + | |
225 | + var menufolders = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(ShrinkDB)}'!", silent: true); | |
226 | + | |
227 | + Menu.Selection sr = menufolders.Select(selectedtaskindexes); | |
228 | + if (sr.Result == Menu.SelectionResult.Exit) { return o; } | |
229 | + else if (sr.Result == Menu.SelectionResult.None) { return o; } | |
230 | + else if (sr.Result == Menu.SelectionResult.Error) { return o; } | |
231 | + else if (sr.Result == Menu.SelectionResult.Ok) { } | |
232 | + else { } | |
233 | + foreach (var p in sr.SelectedParameterList) | |
234 | + { | |
235 | + SQLDataBase sqld = p.Parameters as SQLDataBase; | |
236 | + try | |
237 | + { | |
238 | + ColorConsole.WriteLine(prefix: $"Enter the shrink options. Format:", bracket:"()", text:"OPTION,FreeSpacePercent:", f:ConsoleColor.Yellow); | |
239 | + ColorConsole.WriteLine(prefix:" ", text:"Default", bracket: "[]", suffix: $": (default) compacts the data and removes free space from the DB. Use togethere with FreeSpacePercent.", f:ConsoleColor.Yellow); | |
240 | + ColorConsole.WriteLine(prefix: " ", text: "NoTruncate",bracket:"[]", suffix: $": compacts the data, but does not remove free space from the DB. FreeSpacePercent is not applicable.", f: ConsoleColor.Yellow); | |
241 | + ColorConsole.WriteLine(prefix: " ", text: "TruncateOnly", bracket: "[]", suffix: $": does not compact the data, but removes free space from the DB. Use togethere with FreeSpacePercent.", f: ConsoleColor.Yellow); | |
242 | + ColorConsole.WriteLine(prefix: " ", text: "FreeSpacePercent", bracket: "[]", suffix: $": an integer number between 0-100,default=10; indicates the percentage of free space in the shrinked DB.", f: ConsoleColor.Yellow); | |
243 | + var shrinkoptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); | |
244 | + if (shrinkoptions == "EX") { continue; } | |
245 | + else if (string.IsNullOrWhiteSpace(shrinkoptions)) { shrinkoptions = "Default,10"; } | |
246 | + string option, fsp = null; | |
247 | + int fspint = 10; | |
248 | + option = shrinkoptions.Split(new char[] { ',' })[0]; | |
249 | + if (shrinkoptions.IndexOf(',') >= 0) { fsp = shrinkoptions.Split(new char[] { ',' })[1]; } | |
250 | + if (string.IsNullOrWhiteSpace(option) || !"Default,NoTruncate,TruncateOnly".Contains(option)) { option = "Default"; } | |
251 | + var shrinkmethod = (ShrinkMethod)Enum.Parse(typeof(ShrinkMethod), option); | |
252 | + if (string.IsNullOrWhiteSpace(fsp) || !int.TryParse(fsp, out fspint)) { fspint = 10; } | |
253 | + | |
254 | + SQLDataBaseManagerCore.ShrinkDB(sqld.SQLCS, shrinkmethod, fspint); | |
255 | + ColorConsole.WriteLine($"Database shrinked. Name:{sqld.DBName}", ConsoleColor.Green); | |
256 | + } | |
257 | + catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } | |
258 | + } | |
259 | + return o; | |
260 | + } | |
217 | 261 | private static object RelocatePhysicalFiles(object parameter, object o) |
218 | 262 | { |
219 | 263 | var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); |
... | ... | @@ -312,6 +356,13 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
312 | 356 | ColorConsole.WriteLine(" "); |
313 | 357 | return " "; |
314 | 358 | } |
359 | + else if (lineix == 4) | |
360 | + { | |
361 | + var dbsize = SQLDataBaseManagerCore.GetSize(st.SQLCS); | |
362 | + ColorConsole.Write(dbsize, ConsoleColor.Yellow, prefix: "DB size:", suffix: " "); | |
363 | + ColorConsole.WriteLine(" "); | |
364 | + return " "; | |
365 | + } | |
315 | 366 | return null; |
316 | 367 | } |
317 | 368 | #endregion private method: DisplayDataBaseInfo |
... | ... | @@ -329,6 +380,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
329 | 380 | sqld.PhysicalFilesDirectoryPath = GetPhysicalFilesLocation(sqld.SQLCS); |
330 | 381 | sqld.DBName = GetDBName(sqld.SQLCS); |
331 | 382 | sqld.DataSource = GetDataSource(sqld.SQLCS); |
383 | + sqld.SizeString = GetSize(sqld.SQLCS); | |
332 | 384 | return sqld; |
333 | 385 | } |
334 | 386 | #endregion public CollectDataBaseInfo |
... | ... | @@ -539,6 +591,47 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
539 | 591 | } |
540 | 592 | catch (Exception ex) { throw ex; } |
541 | 593 | } |
594 | + | |
595 | + public static void ShrinkDB(string sqlconnectionstring, ShrinkMethod shrinkmethod,int freespacepercent) | |
596 | + { | |
597 | + var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring); | |
598 | + var sc = new ServerConnection(sqlc); | |
599 | + var srvr = new Server(sc); | |
600 | + var db = new Database(srvr, GetDBName(sqlconnectionstring)); | |
601 | + db.Shrink(freespacepercent, shrinkmethod); | |
602 | + } | |
603 | + | |
604 | + #region GetSize | |
605 | + /// <summary> | |
606 | + /// Visszadja a megadott adatbázis méretét | |
607 | + /// </summary> | |
608 | + /// <param name="sqlconnectionstring"></param> | |
609 | + /// <returns></returns> | |
610 | + public static string GetSize(string sqlconnectionstring) | |
611 | + { | |
612 | + var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring); | |
613 | + var dbname = GetDBName(sqlconnectionstring); | |
614 | + | |
615 | + var getsizecmd = new Microsoft.Data.SqlClient.SqlCommand ("sp_spaceused", sqlc); | |
616 | + getsizecmd.CommandType = System.Data.CommandType.StoredProcedure; | |
617 | + sqlc.Open(); | |
618 | + var reader = getsizecmd.ExecuteReader(); | |
619 | + | |
620 | + if (reader.HasRows) | |
621 | + { | |
622 | + while (reader.Read()) | |
623 | + { | |
624 | + var dbn = reader["database_name"]; | |
625 | + var dbs = reader["database_size"]; | |
626 | + if (Convert.ToString(dbn) == dbname) { return Convert.ToString(dbs); }; | |
627 | + } | |
628 | + } | |
629 | + sqlc.Close(); | |
630 | + sqlc.Dispose(); | |
631 | + return "N/A"; | |
632 | + } | |
633 | + #endregion GetSize | |
634 | + | |
542 | 635 | #region GetPhysicalFilesLocation |
543 | 636 | /// <summary> |
544 | 637 | /// Returns the physical path to the directory that holds the files of the database |
... | ... | @@ -564,7 +657,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
564 | 657 | #region DBSubstitution |
565 | 658 | public enum DBSubstitutionName |
566 | 659 | { |
567 | - DATABASE,DATASOURCE,DBOTYPE,DBONAME,DBDATAGROUP,BACKUPTS, | |
660 | + DATABASE,DATASOURCE,DBOTYPE,DBONAME,DBDATAGROUP,BACKUPTS,SHRINKOPTION,SHRINKFREESPACEPERCENT, | |
568 | 661 | } |
569 | 662 | #endregion DBSubstitution |
570 | 663 | #region ExecuteSQLScript |
... | ... | @@ -615,6 +708,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
615 | 708 | SQLBatchTxt += scriptline + "\r\n"; |
616 | 709 | } |
617 | 710 | } |
711 | + | |
618 | 712 | sqlc.Close(); |
619 | 713 | if (DataSet != null && DataSet.Tables != null) |
620 | 714 | { |
... | ... | @@ -628,6 +722,12 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
628 | 722 | } |
629 | 723 | else { return new ReturnInfoJSON() { ReturnValue = 0, ReturnMessage = null, }; } |
630 | 724 | } |
725 | + | |
726 | + | |
727 | + | |
728 | + | |
729 | + | |
730 | + | |
631 | 731 | private static System.Data.DataSet ExecuteSQLScriptBatch(Microsoft.Data.SqlClient.SqlConnection sqlc, string sqlbatchtxt, int commandtimeout) |
632 | 732 | { |
633 | 733 | var sqlcommand = sqlc.CreateCommand(); |
... | ... | @@ -1005,6 +1105,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
1005 | 1105 | public string DataSource; |
1006 | 1106 | public SQLDataBaseManagerCore.SQLDBStatus Status; |
1007 | 1107 | public string PhysicalFilesDirectoryPath; |
1108 | + public string SizeString; | |
1008 | 1109 | public string SQLCS |
1009 | 1110 | { |
1010 | 1111 | get { return XmlProcessing.ConnectionStringStore.GetSQL(this.Xml_SQLConnectionString); } | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/Program.cs
... | ... | @@ -35,7 +35,6 @@ namespace Vrh.Log4Pro.MaintenanceConsole |
35 | 35 | { |
36 | 36 | static void Main(string[] args) |
37 | 37 | { |
38 | - | |
39 | 38 | var forcedmodulekey = CommandLine.GetCommandLineArgument(args, CLP.CMD_MODULE); |
40 | 39 | var commandmode = !string.IsNullOrEmpty(forcedmodulekey); |
41 | 40 | var silentmode = commandmode && !string.IsNullOrEmpty(CommandLine.GetCommandLineArgument(args, CLP.CMD_SILENT, switchtype: true)); |
... | ... | @@ -113,7 +112,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole |
113 | 112 | |
114 | 113 | // processor architecture |
115 | 114 | ColorConsole.Write("Processors: "); |
116 | - ColorConsole.Write(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"), ConsoleColor.Yellow, bracket: "[]", prefix: "Architecture: ",suffix:", "); | |
115 | + ColorConsole.Write(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"), ConsoleColor.Yellow, bracket: "[]", prefix: "Architecture: "); | |
116 | + ColorConsole.Write(Tools.GetOSType() ?? "N/A", ConsoleColor.Yellow, bracket: "[]", suffix: ", "); | |
117 | 117 | foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()) |
118 | 118 | { |
119 | 119 | ColorConsole.Write(item["NumberOfProcessors"].ToString(), ConsoleColor.Yellow, bracket: "[]", prefix: "Physical:",suffix:", "); |
... | ... | @@ -134,7 +134,13 @@ namespace Vrh.Log4Pro.MaintenanceConsole |
134 | 134 | ColorConsole.Write(System.Environment.OSVersion.ServicePack, ConsoleColor.Yellow, bracket: "[]", prefix: "Service pack:", suffix: ","); |
135 | 135 | ColorConsole.Write(System.Environment.OSVersion.VersionString, ConsoleColor.Yellow, bracket: "[]", prefix: "Version string:"); |
136 | 136 | ColorConsole.WriteLine(""); |
137 | - ColorConsole.Write(Tools.OSFriendlyName(), ConsoleColor.Yellow, bracket: "[]", prefix: " Friendly name:"); | |
137 | + | |
138 | + ColorConsole.Write("", ConsoleColor.Yellow, prefix: "OS Friendly name versions:"); | |
139 | + string fnA = Tools.GetOSFriendlyNameA(); if (!string.IsNullOrWhiteSpace(fnA)) ColorConsole.Write(fnA, ConsoleColor.Yellow, bracket: "[]"); | |
140 | + string fnB = Tools.GetOSFriendlyNameB(); if (!string.IsNullOrWhiteSpace(fnB) && fnB!=fnA) ColorConsole.Write(fnB, ConsoleColor.Yellow, bracket: "[]"); | |
141 | + string fnC = Tools.GetOSFriendlyNameC(); if (!string.IsNullOrWhiteSpace(fnC) && fnC!=fnA && fnC!=fnB) ColorConsole.Write(fnC, ConsoleColor.Yellow, bracket: "[]"); | |
142 | + if (string.IsNullOrWhiteSpace(fnA) && string.IsNullOrWhiteSpace(fnB) && string.IsNullOrWhiteSpace(fnC)) ColorConsole.Write("N/A", ConsoleColor.Yellow, bracket: "[]"); | |
143 | + ColorConsole.WriteLine(""); | |
138 | 144 | ColorConsole.WriteLine(""); |
139 | 145 | ColorConsole.WriteLine(""); |
140 | 146 | } | ... | ... |
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.3.0.0")] | |
36 | -[assembly: AssemblyFileVersion("1.3.0.0")] | |
35 | +[assembly: AssemblyVersion("1.4.0.0")] | |
36 | +[assembly: AssemblyFileVersion("1.4.0.0")] | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj
... | ... | @@ -157,6 +157,7 @@ |
157 | 157 | <Reference Include="Microsoft.SqlServer.WmiEnum, Version=16.200.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL"> |
158 | 158 | <HintPath>..\packages\Microsoft.SqlServer.SqlManagementObjects.161.46041.41\lib\net462\Microsoft.SqlServer.WmiEnum.dll</HintPath> |
159 | 159 | </Reference> |
160 | + <Reference Include="Microsoft.VisualBasic" /> | |
160 | 161 | <Reference Include="Microsoft.Web.Administration, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> |
161 | 162 | <HintPath>..\packages\Microsoft.Web.Administration.11.1.0\lib\netstandard1.5\Microsoft.Web.Administration.dll</HintPath> |
162 | 163 | </Reference> | ... | ... |