Exemple #1
0
 private static void CheckRequiredVMs(IEnumerable<string> vmNames, VMNameType nameType, IDictionary<string, string> vmNamesMap)
 {
     if (vmNames != null)
         foreach (var vmName in vmNames)
             if (nameType == VMNameType.SystemName && !vmNamesMap.Keys.Contains(vmName, StringComparer.OrdinalIgnoreCase) ||
                nameType == VMNameType.ElementName && !vmNamesMap.Values.Contains(vmName, StringComparer.OrdinalIgnoreCase))
             {
                 System.Console.WriteLine(string.Format("WARNING: \"{0}\" not found", vmName));
             }
 }
Exemple #2
0
 private static void CheckRequiredVMs(IEnumerable <string> vmNames, VMNameType nameType, IDictionary <string, string> vmNamesMap)
 {
     if (vmNames != null)
     {
         foreach (var vmName in vmNames)
         {
             if (nameType == VMNameType.SystemName && !vmNamesMap.Keys.Contains(vmName, StringComparer.OrdinalIgnoreCase) ||
                 nameType == VMNameType.ElementName && !vmNamesMap.Values.Contains(vmName, StringComparer.OrdinalIgnoreCase))
             {
                 System.Console.WriteLine(string.Format("WARNING: \"{0}\" not found", vmName));
             }
         }
     }
 }
Exemple #3
0
        IDictionary <string, string> GetVMNames(IEnumerable <string> vmNames, VMNameType nameType)
        {
            IDictionary <string, string> d = new Dictionary <string, string>();

            string query;
            string vmIdField;

            if (UseWMIV2NameSpace)
            {
                query     = "SELECT VirtualSystemIdentifier, ElementName FROM Msvm_VirtualSystemSettingData WHERE VirtualSystemType = 'Microsoft:Hyper-V:System:Realized'";
                vmIdField = "VirtualSystemIdentifier";
            }
            else
            {
                query     = "SELECT SystemName, ElementName FROM Msvm_VirtualSystemSettingData WHERE SettingType = 3";
                vmIdField = "SystemName";
            }

            string inField = nameType == VMNameType.ElementName ? "ElementName" : vmIdField;

            ManagementScope scope = new ManagementScope(GetWMIScope());

            if (vmNames != null && vmNames.Count() > 0)
            {
                query += string.Format(" AND ({0})", GetORStr(inField, vmNames));
            }

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query)))
            {
                using (var moc = searcher.Get())
                    foreach (var mo in moc)
                    {
                        using (mo)
                            d.Add((string)mo[vmIdField], (string)mo["ElementName"]);
                    }
            }

            return(d);
        }
Exemple #4
0
        public IDictionary <string, string> VSSBackup(IEnumerable <string> vmNames, VMNameType nameType, Options options)
        {
            cancel = false;
            var vmNamesMap = GetVMNames(vmNames, nameType);

            if (vmNamesMap.Count > 0)
            {
                if (options.SingleSnapshot)
                {
                    BackupSubset(vmNamesMap, options);
                }
                else
                {
                    foreach (var kv in vmNamesMap)
                    {
                        var vmNamesMapSubset = new Dictionary <string, string>();
                        vmNamesMapSubset.Add(kv.Key, kv.Value);
                        BackupSubset(vmNamesMapSubset, options);
                    }
                }
            }

            return(vmNamesMap);
        }
Exemple #5
0
        IDictionary<string, string> GetVMNames(IEnumerable<string> vmNames, VMNameType nameType)
        {
            IDictionary<string, string> d = new Dictionary<string, string>();

            string query;
            string vmIdField;

            if (UseWMIV2NameSpace)
            {
                query = "SELECT VirtualSystemIdentifier, ElementName FROM Msvm_VirtualSystemSettingData WHERE VirtualSystemType = 'Microsoft:Hyper-V:System:Realized'";
                vmIdField = "VirtualSystemIdentifier";
            }
            else
            {
                query = "SELECT SystemName, ElementName FROM Msvm_VirtualSystemSettingData WHERE SettingType = 3";
                vmIdField = "SystemName";
            }

            string inField = nameType == VMNameType.ElementName ? "ElementName" : vmIdField;

            ManagementScope scope = new ManagementScope(GetWMIScope());

            if (vmNames != null && vmNames.Count() > 0)
                query += string.Format(" AND ({0})", GetORStr(inField, vmNames));

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query)))
            {
                using (var moc = searcher.Get())
                    foreach (var mo in moc)
                        using (mo)
                            d.Add((string)mo[vmIdField], (string)mo["ElementName"]);
            }

            return d;
        }
Exemple #6
0
        public IDictionary<string, string> VSSBackup(IEnumerable<string> vmNames, VMNameType nameType, Options options)
        {
            cancel = false;
            var vmNamesMap = GetVMNames(vmNames, nameType);

            if (vmNamesMap.Count > 0)
            {
                if (options.SingleSnapshot)
                    BackupSubset(vmNamesMap, options);
                else
                    foreach (var kv in vmNamesMap)
                    {
                        var vmNamesMapSubset = new Dictionary<string, string>();
                        vmNamesMapSubset.Add(kv.Key, kv.Value);
                        BackupSubset(vmNamesMapSubset, options);
                    }
            }

            return vmNamesMap;
        }
Exemple #7
0
        static void Main(string[] args)
        {
            try
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                System.Console.WriteLine("HyperVBackup 2.1");
                System.Console.WriteLine("Copyright (C) 2012 Cloudbase Solutions Srl");
                System.Console.WriteLine("Copyright (C) 2015 Coliseo Software Srl");

                var parser  = new Parser(ConfigureSettings);
                var options = new Options();
                if (parser.ParseArgumentsStrict(args, options, () => Environment.Exit(1)))
                {
                    GetConsoleWidth();
                    System.Console.WriteLine();

                    if (options.CleanOutputDays != 0)
                    {
                        CleanOutputByDays(options.Output, options.CleanOutputDays);
                    }

                    if (options.CleanOutputMb != 0)
                    {
                        CleanOutputByMegabytes(options.Output, options.CleanOutputMb);
                    }

                    var vmNames = GetVMNames(options);

                    if (vmNames == null)
                    {
                        System.Console.WriteLine("Backing up all VMs on this server");
                    }

                    if (!Directory.Exists(options.Output))
                    {
                        throw new Exception(string.Format("The folder \"{0}\" is not valid", options.Output));
                    }

                    if (options.CleanOutputDays != 0)
                    {
                        CleanOutputByDays(options.Output, options.CleanOutputDays);
                    }

                    if (options.CleanOutputMb != 0)
                    {
                        CleanOutputByMegabytes(options.Output, options.CleanOutputMb);
                    }

                    VMNameType nameType = options.Name ? VMNameType.ElementName : VMNameType.SystemName;

                    BackupManager mgr = new BackupManager();
                    mgr.BackupProgress += MgrBackupProgress;

                    System.Console.CancelKeyPress += Console_CancelKeyPress;

                    var backupOptions = new HyperVBackUp.Engine.Options
                    {
                        CompressionLevel = options.CompressionLevel,
                        Output           = options.Output,
                        OutputFormat     = options.OutputFormat,
                        SingleSnapshot   = options.SingleSnapshot,
                        VhdInclude       = options.VhdInclude,
                        VhdIgnore        = options.VhdIgnore,
                        Password         = options.Password
                    };

                    var vmNamesMap = mgr.VSSBackup(vmNames, nameType, backupOptions);

                    CheckRequiredVMs(vmNames, nameType, vmNamesMap);

                    ShowElapsedTime(stopwatch);
                }
            }
            catch (BackupCancelledException ex)
            {
                System.Console.Error.WriteLine(string.Format(ex.Message));
                Environment.Exit(3);
            }
            catch (Exception ex)
            {
                System.Console.Error.WriteLine(string.Format("Error: {0}", ex.Message));
                System.Console.Error.WriteLine(ex.StackTrace);
                Environment.Exit(2);
            }

            Environment.Exit(cancel ? 3 : 0);
        }