Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MSSQLUtility.cs « Snapshots « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d92dd590b0422e3f4295d238c348f15c5da71d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Duplicati.Library.IO;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Duplicati.Library.Snapshots
{
    public class MSSQLDB : IEquatable<MSSQLDB>
    {
        public string Name { get; }
        public string ID { get; }
        public List<string> DataPaths { get; }

        public MSSQLDB(string Name, string ID, List<string> DataPaths)
        {
            this.Name = Name;
            this.ID = ID;
            this.DataPaths = DataPaths;
        }

        bool IEquatable<MSSQLDB>.Equals(MSSQLDB other)
        {
            return ID.Equals(other.ID);
        }

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            MSSQLDB db = obj as MSSQLDB;
            if (db != null)
            {
                return Equals(db);
            }
            else
            {
                return false;
            }
        }

        public static bool operator ==(MSSQLDB db1, MSSQLDB db2)
        {
            if (object.ReferenceEquals(db1, db2)) return true;
            if (object.ReferenceEquals(db1, null)) return false;
            if (object.ReferenceEquals(db2, null)) return false;

            return db1.Equals(db2);
        }

        public static bool operator !=(MSSQLDB db1, MSSQLDB db2)
        {
            if (object.ReferenceEquals(db1, db2)) return false;
            if (object.ReferenceEquals(db1, null)) return true;
            if (object.ReferenceEquals(db2, null)) return true;

            return !db1.Equals(db2);
        }
    }

    public class MSSQLUtility
    {
        /// <summary>
        /// The tag used for logging
        /// </summary>
        private static readonly string LOGTAG = Logging.Log.LogTagFromType<MSSQLUtility>();
        /// <summary>
        /// The MS SQL VSS Writer Guid
        /// </summary>
        public static readonly Guid MSSQLWriterGuid = new Guid("a65faa63-5ea8-4ebc-9dbd-a0c4db26912a");
        /// <summary>
        /// MS SQL is supported only on Windows platform
        /// </summary>
        public bool IsMSSQLInstalled { get; }
        /// <summary>
        /// Enumerated MS SQL DBs
        /// </summary>
        public List<MSSQLDB> DBs { get { return m_DBs; } }
        private readonly List<MSSQLDB> m_DBs;

        public MSSQLUtility()
        {
            m_DBs = new List<MSSQLDB>();

            if (!Utility.Utility.IsClientWindows)
            {
                IsMSSQLInstalled = false;
                return;
            }

            string[] arrInstalledInstances = null;

            var installed = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server", "InstalledInstances", "");
            if (installed is string)
            {
                if (!string.IsNullOrWhiteSpace(installed as string))
                    arrInstalledInstances = new string[] { installed as string };
            }
            else if (installed is string[])
                arrInstalledInstances = (string[])installed;
            else if (installed != null)
                try { arrInstalledInstances = (string[])installed; }
                catch { }

            if(Environment.Is64BitOperatingSystem && arrInstalledInstances == null)
            {
                var installed32on64 = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server", "InstalledInstances", "");
                if (installed32on64 is string)
                {
                    if (!string.IsNullOrWhiteSpace(installed32on64 as string))
                        arrInstalledInstances = new string[] { installed32on64 as string };
                }         
                else if (installed32on64 is string[])
                    arrInstalledInstances = (string[])installed32on64;
                else if (installed32on64 != null)
                    try { arrInstalledInstances = (string[])installed32on64; }
                    catch { }
             }
            
            IsMSSQLInstalled = arrInstalledInstances != null && arrInstalledInstances.Length > 0;

            if (!IsMSSQLInstalled)
                Logging.Log.WriteInformationMessage(LOGTAG, "NoMSSQLInstance", "Cannot find any MS SQL Server instance. MS SQL Server is probably not installed.");
        }

        /// <summary>
        /// For all MS SQL databases it enumerate all associated paths using VSS data
        /// </summary>
        /// <returns>A collection of DBs and paths</returns>
        public void QueryDBsInfo()
        {
            if (!IsMSSQLInstalled)
                return;

            m_DBs.Clear();

            using (var vssBackupComponents = new VssBackupComponents())
            {
                var writerGUIDS = new [] { MSSQLWriterGuid };
                try
                {
                    vssBackupComponents.SetupWriters(writerGUIDS, null);
                }
                catch (Exception)
                {
                    throw new Interface.UserInformationException("Microsoft SQL Server VSS Writer not found - cannot backup SQL databases.", "NoMsSqlVssWriter");
                }

                foreach (var o in  vssBackupComponents.ParseWriterMetaData(writerGUIDS))
                {
                    m_DBs.Add(new MSSQLDB(o.Name, o.LogicalPath + "\\" + o.Name, o.Paths.ConvertAll(m => m[0].ToString().ToUpperInvariant() + m.Substring(1))
                                           .Distinct(Utility.Utility.ClientFilenameStringComparer)
                                          .OrderBy(a => a).ToList()));
                }
            }
        }
    }
}