Example #1
0
        private void initialize(IEnumerable <string> paths)
        {
            var vss = VssUtils.LoadImplementation();

            Volumes = new ReadOnlyDictionary <string, VolumeShadowCopyVol>(paths.ToDictionary(k => k, k => new VolumeShadowCopyVol {
                Path = k
            }));
            foreach (var vol in Volumes.Values)
            {
                if (!Volume.IsVolume(vol.Path))
                {
                    throw new ArgumentException(String.Format("{0} is not a valid volume.", vol.Path), nameof(paths));
                }
                vol.UniquePath  = Volume.GetUniqueVolumeNameForPath(vol.Path) ?? vol.Path;
                vol.DisplayPath = GetDisplayNameForVolume(vol.UniquePath);
            }

            var context     = VssSnapshotContext.Backup;
            var contextAttr = (VssVolumeSnapshotAttributes)context;

            bkpComponents = vss.CreateVssBackupComponents();

            bkpComponents.InitializeForBackup(null);
            if (context != VssSnapshotContext.Backup)
            {
                bkpComponents.SetContext(context);
            }
            bkpComponents.SetBackupState(true, true, VssBackupType.Full, false);

            Program.LogDebug("Gathering writer metadata...");
            using (var result = bkpComponents.BeginGatherWriterMetadata(null, null))
                result.AsyncWaitHandle.WaitOne();

            writers = new List <VssWriterDescriptor>(bkpComponents.WriterMetadata.Select(wm => new VssWriterDescriptor(wm)));

            SelectComponentsForBackup(Volumes.Values.Select(v => v.Path), bkpComponents, writers);

            var snapshotSetId = bkpComponents.StartSnapshotSet();

            foreach (var vol in Volumes.Values)
            {
                Program.LogDebug($"Adding volume {vol.Path} [aka {vol.DisplayPath}, unique {vol.UniquePath}]...");
                bkpComponents.AddToSnapshotSet(vol.UniquePath);
            }

            bkpComponents.PrepareForBackup();
            EnsureNoWritersFailed(bkpComponents, writers);
            Program.LogDebug("Creating the shadow copy...");
            bkpComponents.DoSnapshotSet();
            _needsCleanup = true;

            EnsureNoWritersFailed(bkpComponents, writers);
            Program.LogDebug("   done.");

            var snapshots = bkpComponents.QuerySnapshots();

            foreach (var vol in Volumes.Values)
            {
                var snap = snapshots.Where(s => string.Equals(s.OriginalVolumeName, vol.UniquePath, StringComparison.OrdinalIgnoreCase)).MaxElement(s => s.CreationTimestamp);
                vol.SnapshotPath = snap.SnapshotDeviceObject;
                Program.LogDebug($"Volume {vol.Path} [aka {vol.DisplayPath}, unique {vol.UniquePath}] snapshot UNC: {vol.SnapshotPath}");
            }
        }