Beispiel #1
0
        public void Delete(ISnapshot sn)
        {
            Logger.Append(Severity.DEBUG, "Deleting snapshot " + sn.Path + " (id " + sn.Id + ")");
            if (!OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
            {
                // Deleting is unnecessary on XP since snaps are non-persistent
                // and automatically released on Disposing VSS objects.
                return;
            }


            try{
                IVssImplementation vssi = VssUtils.LoadImplementation();
                using (IVssBackupComponents oVSS = vssi.CreateVssBackupComponents()){
                    oVSS.InitializeForBackup(null);
                    oVSS.SetContext(VssSnapshotContext.All);
                    oVSS.DeleteSnapshot(sn.Id, true);
                }

                Logger.Append(Severity.INFO, "Deleted snapshot " + sn.Path + " (id " + sn.Id + ")");
            }
            catch (Exception vsse) {
                Logger.Append(Severity.WARNING, "Unable to delete snapshot " + sn.Path + " (id " + sn.Id + "): " + vsse.Message);
                //backup.Dispose();
                throw vsse;
            }
        }
Beispiel #2
0
        public static void CopyFile(string from, string to)
        {
            FileInfo MyFileInfo = new FileInfo(from);
            String   _Volume    = MyFileInfo.Directory.Root.Name;

            // VSS step 1: Initialize
            IVssImplementation   _vssImplementation = VssUtils.LoadImplementation();
            IVssBackupComponents _backup            = _vssImplementation.CreateVssBackupComponents();

            _backup.InitializeForBackup(null);

            // VSS step 2: Getting Metadata from all the VSS writers
            _backup.GatherWriterMetadata();

            // VSS step 3: VSS Configuration
            _backup.SetContext(VssVolumeSnapshotAttributes.Persistent | VssVolumeSnapshotAttributes.NoAutoRelease);
            _backup.SetBackupState(false, true, Alphaleonis.Win32.Vss.VssBackupType.Full, false);

            // VSS step 4: Declaring the Volumes that we need to use in this beckup.
            // The Snapshot is a volume element (Here come the name "Volume Shadow-Copy")
            // For each file that we nee to copy we have to make sure that the propere volume is in the "Snapshot Set"
            Guid MyGuid01 = _backup.StartSnapshotSet();
            Guid MyGuid02 = _backup.AddToSnapshotSet(_Volume, Guid.Empty);

            // VSS step 5: Preparation (Writers & Provaiders need to start preparation)
            _backup.PrepareForBackup();
            // VSS step 6: Create a Snapshot For each volume in the "Snapshot Set"
            _backup.DoSnapshotSet();

            _backup.ExposeSnapshot(MyGuid02, null, VssVolumeSnapshotAttributes.ExposedLocally, "L:");

            // VSS step 8: Copy Files!

            /***********************************
             * /* Now we start to copy the files/folders/disk!
             * /* Execution time can depend on what we are copying
             * /* We can copy one element or several element.
             * /* As long as we are working under the same snapshot,
             * /* the element should be in consist state from the same point-in-time
             * /***********************************/
            String sVSSFile1 = from.Replace(_Volume, @"L:\");

            if (File.Exists(sVSSFile1))
            {
                System.IO.File.Copy(sVSSFile1, to + @"\" + System.IO.Path.GetFileName(from), true);
            }


            // VSS step 9: Delete the snapshot (using the Exposed Snapshot name)
            foreach (VssSnapshotProperties prop in _backup.QuerySnapshots())
            {
                if (prop.ExposedName == @"L:\")
                {
                    Console.WriteLine("prop.ExposedNam Found!");
                    _backup.DeleteSnapshot(prop.SnapshotId, true);
                }
            }

            _backup = null;
        }
Beispiel #3
0
        /// <summary>
        /// This stage initializes both the requester (this program) and
        /// any writers on the system in preparation for a backup and sets
        /// up a communcation channel between the two.
        /// </summary>
        void InitializeBackup()
        {
            // Here we are retrieving an OS-dependent object that encapsulates
            // all of the VSS functionality.  The OS indepdence that this single
            // factory method provides is one of AlphaVSS's major strengths!
            IVssImplementation vss = VssUtils.LoadImplementation();

            // Now we create a BackupComponents object to manage the backup.
            // This object will have a one-to-one relationship with its backup
            // and must be cleaned up when the backup finishes (ie. it cannot
            // be reused).
            //
            // Note that this object is a member of our class, as it needs to
            // stick around for the full backup.
            _backup = vss.CreateVssBackupComponents();

            // Now we must initialize the components.  We can either start a
            // fresh backup by passing null here, or we could resume a previous
            // backup operation through an earlier use of the SaveXML method.
            _backup.InitializeForBackup(null);

            // At this point, we're supposed to establish communication with
            // the writers on the system.  It is possible before this step to
            // enable or disable specific writers via the BackupComponents'
            // Enable* and Disable* methods.
            _backup.GatherWriterMetadata();
        }
Beispiel #4
0
            private void Initialize(string Volume, bool IncludeBootableSystemState)
            {
                //string filename = @"C:\Windows\system32\config\sam";
                //FileInfo fiSource = new FileInfo(filename);
                //String Volume = fiSource.Directory.Root.Name;

                // VSS step 1: Initialize
                IVssImplementation   vss    = VssUtils.LoadImplementation();
                IVssBackupComponents backup = vss.CreateVssBackupComponents();

                backup.InitializeForBackup(null);

                // VSS step 2: Getting Metadata from all the VSS writers
                backup.GatherWriterMetadata();

                // VSS step 3: VSS Configuration
                backup.SetContext((VssVolumeSnapshotAttributes)0);
                backup.SetBackupState(false, IncludeBootableSystemState, Alphaleonis.Win32.Vss.VssBackupType.Full, false);

                // VSS step 4: Declaring the Volumes that we need to use in this beckup.
                // The Snapshot is a volume element (hence the name "Volume Shadow-Copy").
                // For each file that we nee to copy we have to make sure that the proper volume is included in the "Snapshot Set".
                Guid SetGuid    = backup.StartSnapshotSet();
                Guid VolumeGuid = backup.AddToSnapshotSet(Volume, Guid.Empty);

                // VSS step 5: Preparation (Writers & Provaiders need to start preparation)
                backup.PrepareForBackup();
                // VSS step 6: Create a Snapshot For each volume in the "Snapshot Set"
                backup.DoSnapshotSet();

                /***********************************
                 * /* At this point we have a snapshot!
                 * /* This action should not take more then 60 second, regardless of file or disk size.
                 * /* The snapshot is not a backup or any copy!
                 * /* please more information at http://technet.microsoft.com/en-us/library/ee923636.aspx
                 * /***********************************/

                // VSS step 7: Expose Snapshot

                /***********************************
                 * /* Snapshot path look like:
                 * \\?\Volume{011682bf-23d7-11e2-93e7-806e6f6e6963}\
                 * The build in method System.IO.File.Copy do not work with path like this,
                 * Therefore, we are going to Expose the Snapshot to our application,
                 * by mapping the Snapshot to new virtual volume
                 * - Make sure that you are using a volume that is not already exist
                 * - This is only for learning purposes. usually we will use the snapshot directly as i show in the next example in the blog
                 * /***********************************/

                VssSnapshotProperties SnapshotProperties = backup.GetSnapshotProperties(VolumeGuid);
                DirectoryInfo         diShadowRoot       = new DirectoryInfo(SnapshotProperties.SnapshotDeviceObject);

                DirectoryInfo[] Folders = diShadowRoot.GetDirectories();
            }
Beispiel #5
0
        public VSSService()
        {
            backup = VssUtils.LoadImplementation().CreateVssBackupComponents();

            backup.InitializeForBackup(null);

            snapShotSet = backup.StartSnapshotSet();

            // TODO : per component backup
            //backup.GatherWriterMetadata();
        }
Beispiel #6
0
        void InitializeBackup()
        {
            IVssImplementation vss = VssUtils.LoadImplementation();

            _backup = vss.CreateVssBackupComponents();
            _backup.InitializeForBackup(null);

            using (IVssAsync async = _backup.GatherWriterMetadata())
            {
                async.Wait();
            }
        }
Beispiel #7
0
        public bool IsVolumeSnapshottable(string volumeName)
        {
            IVssImplementation vssImplementation = VssUtils.LoadImplementation();

            using (IVssBackupComponents backup = vssImplementation.CreateVssBackupComponents()){
                backup.InitializeForBackup(null);
                using (IVssAsync async = backup.GatherWriterMetadata()){
                    async.Wait();
                    async.Dispose();
                }
                return(backup.IsVolumeSupported(volumeName));
            }
        }
        public VolumeShadowCopy(string VolumeName)
        {
            IVssImplementation vss = VssUtils.LoadImplementation();

            BackupComponents = vss.CreateVssBackupComponents();
            BackupComponents.InitializeForBackup(null);
            BackupComponents.GatherWriterMetadata();
            BackupComponents.FreeWriterMetadata();
            SetId = BackupComponents.StartSnapshotSet();
            AddVolume(Path.GetPathRoot(VolumeName));
            BackupComponents.SetBackupState(false, true, VssBackupType.Full, false);
            BackupComponents.PrepareForBackup();
        }
        // This stage initializes both the requester and any writers on the system in
        // preparation for a backup, and sets up a communication channel between the two.
        private void InitializeBackup()
        {
            // Retrieving an OS-dependent object that encapsulates all of the VSS functionality.
            IVssImplementation vss = VssUtils.LoadImplementation();

            // Now we create a BackupComponents object to manage the backup.
            _backup = vss.CreateVssBackupComponents();

            // Now we must initialize the components. We can either start a fresh backup by passing null here.
            _backup.InitializeForBackup(null);

            // Establish communication with the writers on the system. It is possible before this step to
            // enable or disable specific writers via the BackupComponents' Enable* and Disable* methods.
            _backup.GatherWriterMetadata();
        }
Beispiel #10
0
        public VSS(BackupLevel level)
        {
            this.RestorePosition    = RestoreOrder.AfterStorage;
            this.ExplodedComponents = new List <string>();
            Metadata  = new SPOMetadata();
            BasePaths = new List <BasePath>();
            IVssImplementation vss = VssUtils.LoadImplementation();

            backup = vss.CreateVssBackupComponents();
            //Logger.Append(Severity.DEBUG, "0/6 Initializing Snapshot ("+ ((BasePaths == null)? "NON-component mode" : "component mode")+")");
            backup.InitializeForBackup(null);
            VssBackupType vssLevel = VssBackupType.Full;

            if (level == BackupLevel.Full)
            {
                vssLevel = VssBackupType.Full;
            }
            else if (level == BackupLevel.Refresh)
            {
                vssLevel = VssBackupType.Incremental;
            }

            /*	else if (level == BackupLevel.Differential)
             *                      vssLevel = VssBackupType.Differential;*/
            else if (level == BackupLevel.TransactionLog)
            {
                vssLevel = VssBackupType.Log;
            }
            //if(spoPaths == null) // component-less snapshot set
            //		backup.SetBackupState(false, true, VssBackupType.Full, false);
            //else
            backup.SetBackupState(true, true, vssLevel, false);
            if (OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
            {
                // The only context supported on Windows XP is VssSnapshotContext.Backup
                backup.SetContext(VssSnapshotContext.AppRollback);
            }
            //Logger.Append(Severity.DEBUG, "1/6 Gathering writers metadata and status");
            using (IVssAsync async = backup.GatherWriterMetadata()){
                async.Wait();
                async.Dispose();
            }
            // gather writers status before adding backup set components
            using (IVssAsync async = backup.GatherWriterStatus()){
                async.Wait();
                async.Dispose();
            }
        }
Beispiel #11
0
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.RegisterType <MigratorEngine>();
            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
            .Where(t => t.Name.EndsWith("MigrationStep"))
            .InstancePerDependency();

            builder.RegisterType <FileSystemHelper>().AsImplementedInterfaces();
            builder.RegisterType <FileCopyHelper>().AsImplementedInterfaces().InstancePerDependency();
            builder.RegisterType <OperatingSystemHelper>().AsImplementedInterfaces();
            builder.RegisterType <FileSystem>().AsImplementedInterfaces();
            IVssImplementation vss = VssUtils.LoadImplementation();

            builder.RegisterInstance(vss).AsImplementedInterfaces();
        }
Beispiel #12
0
        void InitializeBackup()
        {
            IVssImplementation vss = VssUtils.LoadImplementation();

            Debug.WriteLine("vss interface implementation called - vssutils.loadimplementation() called");
            thisBackup = vss.CreateVssBackupComponents();
            Debug.WriteLine("backup component created");
            //try
            //{
            thisBackup.InitializeForBackup(null);
            thisBackup.GatherWriterMetadata();
            //}
            //catch (Alphaleonis.Win32.Vss.VssUnexpectedErrorException ue)
            //{
            //    Debug.WriteLine("AlphaVss Unexpected Error: " + ue);
            // }
        }
Beispiel #13
0
        static void BackupFile(string srcPath, string destPath)
        {
            srcPath = Path.GetFullPath(srcPath);

            IVssImplementation vssImpl = VssUtils.LoadImplementation();

            using (IVssBackupComponents vss = vssImpl.CreateVssBackupComponents())
            {
                vss.InitializeForBackup(null);

                vss.SetBackupState(true, true, VssBackupType.Full, false);
                vss.SetContext(VssSnapshotContext.FileShareBackup);

                using (IVssAsync async = vss.GatherWriterMetadata())
                    async.Wait();

                Guid vssSet = vss.StartSnapshotSet();

                var rootPath   = Path.GetPathRoot(srcPath);
                var snapshotId = vss.AddToSnapshotSet(rootPath, Guid.Empty);

                using (IVssAsync async = vss.DoSnapshotSet())
                    async.Wait();

                try
                {
                    var snapshotPath = vss.GetSnapshotProperties(snapshotId).SnapshotDeviceObject;

                    var pathNoRoot = srcPath.Substring(rootPath.Length);
                    var path       = Path.Combine(snapshotPath, pathNoRoot);

                    if (File.Exists(destPath))
                    {
                        File.Delete(destPath);
                    }
                    Alphaleonis.Win32.Filesystem.File.Copy(path, destPath);
                }
                finally
                {
                    vss.DeleteSnapshotSet(vssSet, true);
                }
            }
        }
Beispiel #14
0
        public bool CreateSnapshot()
        {
            try
            {
                var impl = VssUtils.LoadImplementation();
                if (impl == null)
                {
                    return(false);
                }

                _backup = impl.CreateVssBackupComponents();
                _backup.InitializeForBackup(null);

                if (!_backup.IsVolumeSupported(_volumeName))
                {
                    return(false);
                }

                _backup.GatherWriterMetadata();

                _backup.SetContext(VssVolumeSnapshotAttributes.Persistent | VssVolumeSnapshotAttributes.NoAutoRelease);
                _backup.SetBackupState(false, true, VssBackupType.Full, false);

                _snapshotSetId = _backup.StartSnapshotSet();
                _shadowCopyId  = _backup.AddToSnapshotSet(_volumeName, Guid.Empty);

                _backup.PrepareForBackup();
                _backup.DoSnapshotSet();

                _snapshotVolumeName = _backup.QuerySnapshots().First(x => x.SnapshotSetId == _snapshotSetId && x.SnapshotId == _shadowCopyId).OriginalVolumeName;

                return(true);
            }
            catch
            {
                Helpers.Dispose(ref _backup);
                return(false);
            }
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            IVssImplementation vssImplementation = VssUtils.LoadImplementation();

            using (IVssBackupComponents backup = vssImplementation.CreateVssBackupComponents())
            {
                backup.InitializeForBackup(null);

                if (OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
                {
                    // This does not work on Windows XP, since the only context supported
                    // on Windows XP is VssSnapshotContext.Backup which is the default.
                    backup.SetContext(VssSnapshotContext.All);
                }

                foreach (VssSnapshotProperties prop in backup.QuerySnapshots())
                {
                    Console.WriteLine("Snapshot ID: {0:B}", prop.SnapshotId);
                    Console.WriteLine("Snapshot Set ID: {0:B}", prop.SnapshotSetId);
                    Console.WriteLine("Original Volume Name: {0}", prop.OriginalVolumeName);
                    Console.WriteLine();
                }
            }
        }
        public void Backup(Tuple <string, string> paths)
        {
            String               _Source1           = paths.Item1;
            String               _Destination       = paths.Item2;
            FileInfo             MyFileInfo         = new FileInfo(_Source1);
            String               _Volume            = MyFileInfo.Directory.Root.Name;
            IVssImplementation   _vssImplementation = VssUtils.LoadImplementation();
            IVssBackupComponents _backup            = _vssImplementation.CreateVssBackupComponents();

            _backup.InitializeForBackup(null);
            _backup.GatherWriterMetadata();
            _backup.SetContext(VssVolumeSnapshotAttributes.Persistent | VssVolumeSnapshotAttributes.NoAutoRelease);
            _backup.SetBackupState(false, true, Alphaleonis.Win32.Vss.VssBackupType.Full, false);
            Guid MyGuid01 = _backup.StartSnapshotSet();
            Guid MyGuid02 = _backup.AddToSnapshotSet(_Volume, Guid.Empty);

            // VSS step 5: Preparation (Writers & Provaiders need to start preparation)
            _backup.PrepareForBackup();
            // VSS step 6: Create a Snapshot For each volume in the "Snapshot Set"
            _backup.DoSnapshotSet();
            // GET drive labels in use
            var d_name = " ";
            var d_nam  = " ";

            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach (DriveInfo d in allDrives)
            {
                if (d.Name != @"A:\")
                {
                    d_name = @"A:\";
                    d_nam  = @"A:";
                }
                else if (d.Name != @"B:\")
                {
                    d_name = @"B:\";
                    d_nam  = @"A:";
                }
                else if (d.Name != @"Z:\")
                {
                    d_name = @"Z:\";
                    d_nam  = @"Z:";
                }
            }
            _backup.ExposeSnapshot(MyGuid02, null, VssVolumeSnapshotAttributes.ExposedLocally, d_nam);
            String sVSSFile1 = _Source1.Replace(_Volume, d_name);

            if (File.Exists(sVSSFile1))
            {
                System.IO.File.Copy(sVSSFile1, _Destination + @"\" + System.IO.Path.GetFileName(_Source1), true);
            }
            foreach (VssSnapshotProperties prop in _backup.QuerySnapshots())
            {
                if (prop.ExposedName == d_name)
                {
                    Console.WriteLine("prop.ExposedNam Found!");
                    _backup.DeleteSnapshot(prop.SnapshotId, true);
                }
            }
            _backup = null;
            return;
        }
Beispiel #17
0
        private static void Zip()
        {
            Log2 vssLog = new Log2(nameof(AddonsBackup) + " - VSS Service");
            // get free drive letter
            string driveLetter = new string[] { "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:" }.FirstOrDefault(l => !DriveInfo.GetDrives().Select(m => m.RootDirectory.Name).Contains(l));

            if (driveLetter == default(string))
            {
                throw new IOException("Can't find free drive letter!");
            }
            vssLog.Info($"Free drive letter: {driveLetter}");
            // making VSS snapshot
            IVssImplementation   vssImplementation = VssUtils.LoadImplementation();
            IVssBackupComponents backupComponents  = vssImplementation.CreateVssBackupComponents();

            backupComponents.InitializeForBackup(null);
            vssLog.Info("VssBackupComponents is initialized");
            Guid backupGuid1 = Guid.Empty;

            try
            {
                backupComponents.GatherWriterMetadata();
                backupComponents.SetContext(VssVolumeSnapshotAttributes.Persistent | VssVolumeSnapshotAttributes.NoAutoRelease);
                backupComponents.SetBackupState(false, true, VssBackupType.Full, false);
                vssLog.Info("VssBackupComponents is set up");
                backupComponents.StartSnapshotSet();
                backupGuid1 = backupComponents.AddToSnapshotSet(new DirectoryInfo(_settings.WoWDirectory).Root.Name, Guid.Empty);
                backupComponents.PrepareForBackup();
                backupComponents.DoSnapshotSet();
                vssLog.Info("Snapshot is taken");
                backupComponents.ExposeSnapshot(backupGuid1, null, VssVolumeSnapshotAttributes.ExposedLocally, driveLetter);
                // zipping
                string zipPath = $"{_settings.WoWAddonsBackupPath}\\AddonsBackup_{DateTime.UtcNow:yyyyMMdd_HHmmss}.zip";
                log.Info("Zipping to file: " + zipPath);
                using (ZipFile zip = new ZipFile(zipPath, Encoding.UTF8))
                {
                    zip.CompressionLevel = (CompressionLevel)_settings.WoWAddonsBackupCompressionLevel;
                    foreach (string dirName in FoldersToArchive)
                    {
                        zip.AddDirectory(_settings.WoWDirectory.Replace(new DirectoryInfo(_settings.WoWDirectory).Root.Name, $"{driveLetter}\\") + "\\" + dirName, "\\" + dirName);
                    }
                    zip.SaveProgress += AddonsBackup_SaveProgress;
                    var processPriority = Process.GetCurrentProcess().PriorityClass;
                    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal;
                    zip.Save();
                    Process.GetCurrentProcess().PriorityClass = processPriority;
                    zip.SaveProgress -= AddonsBackup_SaveProgress;
                }
            }
            finally
            {
                VssSnapshotProperties existingSnapshot = backupComponents.QuerySnapshots().FirstOrDefault(l => l.SnapshotId == backupGuid1);
                if (existingSnapshot == default(VssSnapshotProperties))
                {
                    vssLog.Error($"Can't delete snapshot {backupGuid1}");
                }
                else
                {
                    backupComponents.DeleteSnapshot(existingSnapshot.SnapshotId, true);
                    backupComponents.Dispose();
                    vssLog.Info($"Snapshot is deleted ({existingSnapshot.SnapshotId})");
                }
                GC.Collect();
            }
        }
Beispiel #18
0
        /// <summary>
        /// This is only first test of using AlphaVSS project.
        /// Please make sure you read thoroughly and understand the code's comments and adapt it to your system and needs.
        /// We are using constant links for "source files to copy" and for the "destination folder".
        /// In any dynamic application you should get those values from the user.
        ///
        /// building your project:
        /// -----------------------
        /// 1. Creating New Project
        ///    * Open Visual Studio As Administrator!
        ///    * Chose create new project type "Console application"
        ///    * use Dot.Net 4 framework
        ///    * named the project Ariely_AlphaVSS_Sample01
        /// 2. Add NuGet Package named alphavss (use the search on-line option, and install the package)
        /// 3. Go over the Code's comment and understand it!!! After you are ready you can use it.
        /// 4. add "using Alphaleonis.Win32.Vss;" & "using System.IO;"
        /// </summary>
        /// <param name="args"></param>
        public static void copy(string destination)
        {
            // Getting information on the files to copy (This is not part of the VSS action)
            string   userName     = Environment.UserName;
            string   baseAddress  = @"C:\Users\{0}\AppData\Local\Microsoft\Windows\WebCache\WebCacheV01.dat";
            string   directory    = string.Format(baseAddress, userName);
            String   _Source1     = directory;
            String   _Destination = destination;
            FileInfo MyFileInfo   = new FileInfo(_Source1);
            String   _Volume      = MyFileInfo.Directory.Root.Name;

            // VSS step 1: Initialize
            IVssImplementation   _vssImplementation = VssUtils.LoadImplementation();
            IVssBackupComponents _backup            = _vssImplementation.CreateVssBackupComponents();

            _backup.InitializeForBackup(null);

            // VSS step 2: Getting Metadata from all the VSS writers
            _backup.GatherWriterMetadata();

            // VSS step 3: VSS Configuration
            _backup.SetContext(VssVolumeSnapshotAttributes.Persistent | VssVolumeSnapshotAttributes.NoAutoRelease);
            _backup.SetBackupState(false, true, Alphaleonis.Win32.Vss.VssBackupType.Full, false);

            // VSS step 4: Declaring the Volumes that we need to use in this beckup.
            // The Snapshot is a volume element (Here come the name "Volume Shadow-Copy")
            // For each file that we nee to copy we have to make sure that the propere volume is in the "Snapshot Set"
            Guid MyGuid01 = _backup.StartSnapshotSet();
            Guid MyGuid02 = _backup.AddToSnapshotSet(_Volume, Guid.Empty);

            // VSS step 5: Preparation (Writers & Provaiders need to start preparation)
            _backup.PrepareForBackup();
            // VSS step 6: Create a Snapshot For each volume in the "Snapshot Set"
            _backup.DoSnapshotSet();

            /***********************************
             * /* At this point we have a snapshot!
             * /* This action should not take more then 60 second, regardless of file or disk size.
             * /* THe snapshot is not a backup or any copy!
             * /* please more information at http://technet.microsoft.com/en-us/library/ee923636.aspx
             * /***********************************/

            // VSS step 7: Expose Snapshot

            /***********************************
             * /* Snapshot path look like:
             * \\?\Volume{011682bf-23d7-11e2-93e7-806e6f6e6963}\
             * The build in method System.IO.File.Copy do not work with path like this,
             * Therefor, we are going to Expose the Snapshot to our application,
             * by mapping the Snapshot to new virtual volume
             * - Make sure that you are using a volume that is not already exist
             * - This is only for learning purposes. usually we will use the snapshot directly as i show in the next example in the blog
             * /***********************************/
            _backup.ExposeSnapshot(MyGuid02, null, VssVolumeSnapshotAttributes.ExposedLocally, "S:");

            // VSS step 8: Copy Files!

            /***********************************
             * /* Now we start to copy the files/folders/disk!
             * /* Execution time can depend on what we are copying
             * /* We can copy one element or several element.
             * /* As long as we are working under the same snapshot,
             * /* the element should be in consist state from the same point-in-time
             * /***********************************/
            String sVSSFile1 = _Source1.Replace(_Volume, @"S:\");

            if (File.Exists(sVSSFile1))
            {
                System.IO.File.Copy(sVSSFile1, _Destination + @"\" + System.IO.Path.GetFileName(_Source1), true);
            }


            // VSS step 9: Delete the snapshot (using the Exposed Snapshot name)
            foreach (VssSnapshotProperties prop in _backup.QuerySnapshots())
            {
                if (prop.ExposedName == @"S:\")
                {
                    Console.WriteLine("prop.ExposedNam Found!");
                    _backup.DeleteSnapshot(prop.SnapshotId, true);
                }
            }

            _backup = null;
        }
Beispiel #19
0
        private VssSnapshotProperties GetVssSnapshotFromTime(DateTime selectedCreationTime)
        {
            Debug.WriteLine(string.Format("Selected creation time: {0}", selectedCreationTime.ToLongTimeString()));

            // Get selected restore point
            SystemRestoreItem selectedSystemRestoreItem = null;

            foreach (SystemRestoreItem sri in systemRestorePoints)
            {
                if (sri.CreationTime.ToLocalTime().CompareTo(selectedCreationTime) == 0)
                {
                    selectedSystemRestoreItem = sri;
                    break;
                }
            }

            // Enumerate snapshots using VSS API
            IVssImplementation vssImplmentation = VssUtils.LoadImplementation();
            ////VssBackupComponents vss = new VssBackupComponents();
            IVssBackupComponents vssBackup = vssImplmentation.CreateVssBackupComponents();

            vssBackup.InitializeForBackup(null);
            vssBackup.SetContext(VssSnapshotContext.All);

            List <VssSnapshotProperties> snapshotHourMatches   = new List <VssSnapshotProperties>();
            List <VssSnapshotProperties> snapshotMinuteMatches = new List <VssSnapshotProperties>();

            foreach (VssSnapshotProperties snapshot in vssBackup.QuerySnapshots())
            {
                DateTime truncatedSelectedRestoreItemTime = TruncateToHour(selectedSystemRestoreItem.CreationTime.ToLocalTime());
                DateTime truncatedSnapshotTime            = TruncateToHour(snapshot.CreationTimestamp);

                if (truncatedSelectedRestoreItemTime.CompareTo(truncatedSnapshotTime) == 0)
                {
                    snapshotHourMatches.Add(snapshot);
                }
            }

            if (snapshotHourMatches.Count == 1)
            {
                Debug.WriteLine(string.Format("Snapshot creation time: {0}", snapshotHourMatches[0].CreationTimestamp.ToLongTimeString()));
                return(snapshotHourMatches[0]);
            }
            else if (snapshotHourMatches.Count > 1)
            {
                foreach (VssSnapshotProperties snapshot in snapshotHourMatches)
                {
                    DateTime truncatedSelectedRestoreItemTime = TruncateToMinute(selectedSystemRestoreItem.CreationTime.ToLocalTime());
                    DateTime truncatedSnapshotTime            = TruncateToMinute(snapshot.CreationTimestamp);

                    if (truncatedSelectedRestoreItemTime.CompareTo(truncatedSnapshotTime) == 0)
                    {
                        snapshotMinuteMatches.Add(snapshot);
                    }
                }

                if (snapshotMinuteMatches.Count == 1)
                {
                    Debug.WriteLine(string.Format("Snapshot creation time: {0}", snapshotMinuteMatches[0].CreationTimestamp.ToLongTimeString()));
                    return(snapshotMinuteMatches[0]);
                }
                else if (snapshotMinuteMatches.Count > 1)
                {
                    Debug.WriteLine(string.Format("Found {0} potential matches", snapshotMinuteMatches.Count.ToString()));

                    // Find closest match
                    foreach (VssSnapshotProperties snapshot in snapshotMinuteMatches)
                    {
                        if (selectedSystemRestoreItem.CreationTime.ToLocalTime().CompareTo(snapshot.CreationTimestamp) < 0)
                        {
                            Debug.WriteLine(string.Format("Snapshot creation time: {0}", snapshot.CreationTimestamp.ToLongTimeString()));
                            return(snapshot);
                        }
                    }
                }
            }

            return(null);
        }
        /// <summary>
        /// Makes a recursive snapshot of the directory as it is.
        /// </summary>
        /// <param name="baseDirectory">The directory to start in.</param>
        /// <returns>A file system snapshot</returns>
        public static SnapshotFilesystem MakeFsSnapshot(System.IO.DirectoryInfo baseDirectory)
        {
            DirectoryInfo baseDir = new DirectoryInfo(baseDirectory.FullName);

            if (!baseDir.Exists)
            {
                throw new ArgumentException("Base directory doesn't exist");
            }

            SnapshotFilesystem res = new SnapshotFilesystem();

            if (!baseDir.FullName.EndsWith(Path.DirectorySeparatorChar) && !baseDir.FullName.EndsWith(Path.AltDirectorySeparatorChar))
            {
                // Add ending slash to get a uniform output
                baseDir = new DirectoryInfo(baseDir.FullName + Path.DirectorySeparatorChar);
            }

            res.BasePath = baseDir.FullName;
            res.Items    = new LinkedList <SnapshotFilesystemItem>();

            // Make VSS
            // Sequence of calls: http://us.generation-nt.com/answer/volume-shadow-copy-backupcomplete-vss-e-bad-state-help-29094302.html
            IVssImplementation   vssImplementation = VssUtils.LoadImplementation();
            IVssBackupComponents backupComponents  = vssImplementation.CreateVssBackupComponents();

            backupComponents.InitializeForBackup(null);
            backupComponents.SetContext(VssSnapshotContext.Backup);

            backupComponents.SetBackupState(false, false, VssBackupType.Copy, false);
            backupComponents.GatherWriterMetadata();

            try
            {
                Guid snapshotSetGuid  = backupComponents.StartSnapshotSet();
                Guid backupVolumeGuid = backupComponents.AddToSnapshotSet(baseDir.Root.FullName);

                backupComponents.PrepareForBackup();
                backupComponents.DoSnapshotSet();

                VssSnapshotProperties properties = backupComponents.GetSnapshotProperties(backupVolumeGuid);

                DirectoryInfo shadowCopyBase = new DirectoryInfo(Path.Combine(properties.SnapshotDeviceObject, Path.GetDirectoryNameWithoutRoot(baseDir.FullName)));

                if (!shadowCopyBase.FullName.EndsWith(Path.DirectorySeparatorChar) && !shadowCopyBase.FullName.EndsWith(Path.AltDirectorySeparatorChar))
                {
                    // Add ending slash to get a uniform output
                    shadowCopyBase = new DirectoryInfo(shadowCopyBase.FullName + Path.DirectorySeparatorChar);
                }

                // Do stuff
                DoFsSnapshot(shadowCopyBase, shadowCopyBase, res.Items);

                // Delete snapshot
                backupComponents.BackupComplete();
                backupComponents.DeleteSnapshotSet(snapshotSetGuid, false);
            }
            catch (Exception)
            {
                backupComponents.AbortBackup();
            }

            //DoFsSnapshot(baseDirectory, baseDirectory, res.Items);

            return(res);
        }
Beispiel #21
0
        //edit 05.04.2017
        public void StartDiffBackup()
        {
            UpdateSosttask(2);
            IVssBackupComponents backup = null;
            string  namedriver          = "";
            Boolean successtask         = true;

            try
            {
                if (DTask.Shadow == 1)
                {
                    string[]    massdrives = { "A:\\", "B:\\", "D:\\", "E:\\", "I:\\", "K:\\", "L:\\", "M:\\", "N:\\", "O:\\", "P:\\" };
                    string      havedriver = "";
                    DriveInfo[] drives     = DriveInfo.GetDrives();
                    foreach (DriveInfo d in drives)
                    {
                        havedriver = havedriver + d.Name;
                    }
                    foreach (string strdriver in massdrives)
                    {
                        if (!havedriver.Contains(strdriver))
                        {
                            namedriver = strdriver;
                        }
                    }

                    FileInfo MyFileInfo = new FileInfo(DTask.Source);
                    String   _Volume    = MyFileInfo.Directory.Root.Name;

                    IVssImplementation _vssImplementation = VssUtils.LoadImplementation();
                    backup = _vssImplementation.CreateVssBackupComponents();
                    backup.InitializeForBackup(null);

                    backup.GatherWriterMetadata();

                    backup.SetContext(VssVolumeSnapshotAttributes.Persistent | VssVolumeSnapshotAttributes.NoAutoRelease);
                    backup.SetBackupState(false, true, Alphaleonis.Win32.Vss.VssBackupType.Full, false);

                    Guid MyGuid01 = backup.StartSnapshotSet();
                    Guid MyGuid02 = backup.AddToSnapshotSet(_Volume, Guid.Empty);

                    backup.PrepareForBackup();
                    backup.DoSnapshotSet();

                    backup.ExposeSnapshot(MyGuid02, null, VssVolumeSnapshotAttributes.ExposedLocally, namedriver.Remove(2, 1));

                    DTask.Source = DTask.Source.Replace(_Volume, namedriver);
                }

                StringWriter SW            = new StringWriter();
                StringWriter Skip_file_log = new StringWriter();
                string       time_today    = DateTime.Now.Date.ToShortDateString();
                ZipFile      zf            = new ZipFile(DTask.Dest + "\\" + DTask.Taskname + "_" + time_today.Replace(".", "-") + "." + DTask.Extension);
                zf.ProvisionalAlternateEncoding = Encoding.GetEncoding("cp866");

                if (DTask.Password != "no")
                {
                    zf.Password = DTask.Password;
                }
                zf.UseZip64WhenSaving      = Zip64Option.AsNecessary;
                zf.ZipErrorAction          = ZipErrorAction.Skip;
                zf.StatusMessageTextWriter = Skip_file_log;

                string[] massexeption;
                string[] separator = { Environment.NewLine };
                massexeption = DTask.Exeption.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                Stack <string> dirs = new Stack <string>(20);
                string         root = DTask.Exeption;

                if (!Directory.Exists(root))
                {
                    throw new ArgumentException();
                }
                dirs.Push(root);


                while (dirs.Count > 0)
                {
                    string   currentDir = dirs.Pop();
                    string[] subDirs;
                    try
                    {
                        subDirs = Directory.GetDirectories(currentDir);
                    }

                    catch (UnauthorizedAccessException e)
                    {
                        SW.WriteLine("Нет доступа к файлу или каталогу: " + currentDir);
                        successtask = false;
                        continue;
                    }
                    catch (DirectoryNotFoundException e)
                    {
                        SW.WriteLine("Не найден файл или каталог: " + currentDir);
                        successtask = false;
                        continue;
                    }
                    catch (PathTooLongException e)
                    {
                        SW.WriteLine("Слишком длинный путь или имя файла: " + currentDir);
                        successtask = false;
                        continue;
                    }


                    string[] files = null;
                    try
                    {
                        files = Directory.GetFiles(currentDir);
                    }

                    catch (UnauthorizedAccessException e)
                    {
                        SW.WriteLine(e.Message);
                        successtask = false;
                        continue;
                    }

                    catch (DirectoryNotFoundException e)
                    {
                        SW.WriteLine(e.Message);
                        successtask = false;
                        continue;
                    }

                    if (DTask.Exeption == "no")
                    {
                        foreach (string file in files)
                        {
                            try
                            {
                                FileInfo fi = new FileInfo(file);
                                if (fi.LastWriteTime > DTask.DateBeginBackup)
                                {
                                    zf.AddFile(file, Path.GetDirectoryName(file).Replace(DTask.Source, string.Empty));
                                }
                            }
                            catch (FileNotFoundException e)
                            {
                                SW.WriteLine("Не найден файл: " + file);
                                successtask = false;
                                continue;
                            }

                            catch (PathTooLongException e)
                            {
                                SW.WriteLine("Слишком длинный путь или имя файла: " + file);
                                successtask = false;
                                continue;
                            }
                        }
                    }
                    else
                    {
                        int excludeflag = 0;
                        foreach (string file in files)
                        {
                            try
                            {
                                excludeflag = 0;
                                foreach (var exep in massexeption)
                                {
                                    if (Path.GetFullPath(file).Contains(exep))
                                    {
                                        excludeflag = 1;
                                        break;
                                    }
                                }
                                if (excludeflag == 0)
                                {
                                    FileInfo fi = new FileInfo(file);
                                    if (fi.LastWriteTime > DTask.DateBeginBackup)
                                    {
                                        zf.AddFile(file, Path.GetDirectoryName(file).Replace(DTask.Source, string.Empty));
                                    }
                                }
                            }
                            catch (FileNotFoundException e)
                            {
                                SW.WriteLine("Не найден файл: " + file);
                                successtask = false;
                                continue;
                            }

                            catch (PathTooLongException e)
                            {
                                SW.WriteLine("Слишком длинный путь или имя файла: " + file);
                                successtask = false;
                                continue;
                            }
                        }
                    }


                    foreach (string str in subDirs)
                    {
                        dirs.Push(str);
                    }
                }



                zf.Save();
                zf.Dispose();
                if (successtask)
                {
                    UpdateSosttask(4);
                }
                else
                {
                    UpdateSosttask(5);
                }


                if (DTask.Shadow == 1)
                {
                    foreach (VssSnapshotProperties prop in backup.QuerySnapshots())
                    {
                        if (prop.ExposedName == namedriver)
                        {
                            backup.DeleteSnapshot(prop.SnapshotId, true);
                        }
                    }
                    backup = null;
                }

                string       strLine;
                int          CountSkip    = 0;
                StringReader stringreader = new StringReader(SW.ToString());
                while (true)
                {
                    strLine = stringreader.ReadLine();
                    if (strLine != null)
                    {
                        CountSkip++;
                        BDAL.OpenConnection();
                        BDAL.AddDiffLog(strLine, CurrentDate, DTask.Idtask, 0);
                        BDAL.CloseConnection();
                    }
                    else
                    {
                        break;
                    }
                }


                string       strLine2;
                StringReader stringreader2 = new StringReader(Skip_file_log.ToString());
                while (true)
                {
                    strLine2 = stringreader2.ReadLine();
                    if (strLine2 != null)
                    {
                        if (strLine2.Contains("Skipping"))
                        {
                            CountSkip++;
                            BDAL.OpenConnection();
                            BDAL.AddDiffLog(strLine2, CurrentDate, DTask.Idtask, 0);
                            BDAL.CloseConnection();
                        }
                    }
                    else
                    {
                        break;
                    }
                }


                if (CountSkip > 0)
                {
                    UpdateSosttask(5);
                }

                string msg = "Завершено успешно";

                BDAL.OpenConnection();
                BDAL.AddDiffLog(msg, CurrentDate, DTask.Idtask, 1);
                BDAL.CloseConnection();
                DeleteDimeLive();


                if (DTask.Ftp == 1)
                {
                    try
                    {
                        UpdateSosttask(2);
                        FtpClient    client = new FtpClient();
                        StreamReader sr     = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "ftp_sett.ini");
                        client.PassiveMode = true;   //Включаем пассивный режим.
                        int    TimeoutFTP   = 30000; //Таймаут.
                        string FTP_SERVER   = sr.ReadLine();
                        int    FTP_PORT     = Convert.ToInt32(sr.ReadLine());
                        string FTP_USER     = sr.ReadLine();
                        string FTP_PASSWORD = Decrypt(sr.ReadLine(), "OFRna73");
                        string FTP_FOLDER   = sr.ReadLine();


                        client.Connect(TimeoutFTP, FTP_SERVER, FTP_PORT);
                        client.Login(TimeoutFTP, FTP_USER, FTP_PASSWORD);
                        if (FTP_FOLDER != "not_folder")
                        {
                            client.ChangeDirectory(TimeoutFTP, FTP_FOLDER);
                        }
                        client.PutFile(TimeoutFTP, zf.Name.Substring(zf.Name.IndexOf(DTask.Taskname)), zf.Name);
                        sr.Close();
                        client.Disconnect(TimeoutFTP);

                        msg = "Копирование архива на FTP сервер завершено успешно";

                        BDAL.OpenConnection();
                        BDAL.AddDiffLog(msg, CurrentDate, DTask.Idtask, 1);
                        BDAL.CloseConnection();
                        UpdateSosttask(4);
                    }
                    catch (Exception ex)
                    {
                        BDAL.OpenConnection();
                        BDAL.AddDiffLog(ex.ToString(), CurrentDate, DTask.Idtask, 0);
                        BDAL.CloseConnection();
                        UpdateSosttask(3);
                    }
                }
            }
            catch (Exception ex)
            {
                Add_Diff_Log_Error(ex.Message);
                try
                {
                    if (DTask.Shadow == 1)
                    {
                        foreach (VssSnapshotProperties prop in backup.QuerySnapshots())
                        {
                            if (prop.ExposedName == namedriver)
                            {
                                backup.DeleteSnapshot(prop.SnapshotId, true);
                            }
                        }
                        backup = null;
                    }
                }
                catch
                {
                }
            }
        }
Beispiel #22
0
        public List <ISnapshot> ListSpecialObjects()
        {
            List <ISnapshot>   alreadyExisting   = new List <ISnapshot>();
            IVssImplementation vssImplementation = VssUtils.LoadImplementation();

            using (IVssBackupComponents backup = vssImplementation.CreateVssBackupComponents()){
                backup.InitializeForBackup(null);
                using (IVssAsync async = backup.GatherWriterMetadata()){
                    async.Wait();
                    async.Dispose();
                }

                if (OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
                {
                    // The only context supported on Windows XP is VssSnapshotContext.All
                    //backup.SetContext(VssSnapshotContext.Backup);
                    backup.SetContext(VssSnapshotContext.AppRollback | VssSnapshotContext.All);
                }
                //backup.SetBackupState(false,  true, VssBackupType.Full, false);
                //Guid snapID = backup.StartSnapshotSet();
                //backup.AddToSnapshotSet(@"C:\");
                foreach (IVssExamineWriterMetadata writer in backup.WriterMetadata)
                {
                    VSSSnapshot s = new VSSSnapshot();
                    s.Path       = writer.WriterName;
                    s.Type       = (writer.Source == VssSourceType.TransactedDB || writer.Source == VssSourceType.NonTransactedDB)? "VSSDB" : "VSSWriter";
                    s.Version    = writer.Version.Major + "." + writer.Version.Minor + "." + writer.Version.MajorRevision + "." + writer.Version.MinorRevision + writer.Version.Revision;
                    s.MountPoint = writer.WriterName;
                    foreach (IVssWMComponent cmp in writer.Components)
                    {
                        VSSSnapshot childS = new VSSSnapshot();
                        childS.Path = cmp.ComponentName;
                        childS.Icon = cmp.GetIcon();
                        childS.Type = (cmp.Type == VssComponentType.Database)? "VSSDB" : "VSSFileGroup";
                        if (!cmp.Selectable)
                        {
                            childS.Type = "spoDisabled";
                        }
                        childS.MountPoint = cmp.LogicalPath;
                        childS.Disabled   = true;
                        if (OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
                        {
                            foreach (VssWMDependency dep in cmp.Dependencies)
                            {
                                Logger.Append(Severity.INFO, "TODO: vss component " + cmp.ComponentName + " has dependancy on " + dep.ComponentName);
                            }
                        }
                        if (cmp.Selectable)
                        {
                            childS.Disabled = false;
                        }
                        s.AddChildComponent(childS);
                    }
                    alreadyExisting.Add(s);
                }
                //backup.QueryProviders
                foreach (IVssWriterComponents wc in   backup.WriterComponents)
                {
                    foreach (IVssComponent c in wc.Components)
                    {
                        VSSSnapshot s = new VSSSnapshot();
                        s.Path = c.ComponentName;
                        s.Type = c.ComponentType.ToString();
                        alreadyExisting.Add(s);
                    }
                }
            }
            return(alreadyExisting);
        }
            private void Main()
            {
                string sourceFolder = PathHelper.AppendSeparator(_session.SourceFolder);                                         // C:\,  C:\Folder\, \\Server\Share\, \\Server\Share\Folder\
                string volume       = PathHelper.RemoveTrailingSeparator(Path.GetPathRoot(sourceFolder));                        // C:,   C:,         \\Server\Share,  \\Server\Share
                string pathFromRoot = PathHelper.RemoveTrailingSeparator(sourceFolder.Substring(volume.Length));                 // "",   \Folder,    "",              \Folder

                if (pathFromRoot.Length == 0)
                {
                    pathFromRoot = null;                                                                                             // null, \Folder,    null,            \Folder
                }
                try
                {
                    // helper function throwing an ObjectDisposedException if _disposeEvent is currently signaled
                    Action CheckForDisposeRequest = () => { if (_disposeEvent.WaitOne(0))
                                                            {
                                                                throw new ObjectDisposedException("VscThread");
                                                            }
                    };

                    CheckForDisposeRequest();

                    _backup = VssUtils.LoadImplementation().CreateVssBackupComponents();
                    _backup.InitializeForBackup(null);
                    _backup.SetContext(VssSnapshotContext.Backup);
                    _backup.SetBackupState(false, false, VssBackupType.Copy, false);
                    CheckForDisposeRequest();

                    _backup.GatherWriterMetadata();
                    CheckForDisposeRequest();

                    _snapshotSetID    = _backup.StartSnapshotSet();
                    _volumeSnapshotID = _backup.AddToSnapshotSet(PathHelper.AppendSeparator(volume));
                    CheckForDisposeRequest();

                    _backup.PrepareForBackup();
                    VerifyWriterStatus();
                    CheckForDisposeRequest();

                    // create the snapshot
                    _backup.DoSnapshotSet();
                    VerifyWriterStatus();

                    // mount the source folder as file share
                    // (non-persistent shadow copies cannot be mounted locally on a drive or folder)
                    string shareName = _backup.ExposeSnapshot(_volumeSnapshotID, pathFromRoot,
                                                              VssVolumeSnapshotAttributes.ExposedRemotely, null);
                    _session._mountPoint = @"\\localhost\" + shareName;

                    CheckForDisposeRequest();
                }
                catch (ObjectDisposedException)                 // by CheckForDisposeRequest()
                {
                    Cleanup();
                    return;
                }
                catch (Exception e)
                {
                    Cleanup();

                    string msg = (e is UnauthorizedAccessException
                                                ? "You lack the required privileges to create a volume shadow copy.\nPlease restart RoboMirror as administrator."
                                                : "The volume shadow copy could not be created:\n\n" + e.Message);
                    _session.OnError(msg);

                    return;
                }

                // fire the Ready event
                try { _session.OnReady(); }
                // on unhandled exception by Ready event handler: Cleanup() before rethrowing
                catch { Cleanup(); throw; }

                // wait for Dispose() signal
                _disposeEvent.WaitOne();

                Cleanup();
            }
Beispiel #24
0
        private void BackupSubset(IDictionary <string, string> vmNamesMapSubset, Options options)
        {
            IVssImplementation vssImpl = VssUtils.LoadImplementation();

            using (IVssBackupComponents vss = vssImpl.CreateVssBackupComponents())
            {
                RaiseEvent(EventAction.InitializingVSS, null, null);

                vss.InitializeForBackup(null);
                vss.SetBackupState(true, true, VssBackupType.Full, false);
                vss.SetContext(VssSnapshotContext.Backup);

                // Add Hyper-V writer
                Guid hyperVwriterGuid = new Guid("66841cd4-6ded-4f4b-8f17-fd23f8ddc3de");
                vss.EnableWriterClasses(new Guid[] { hyperVwriterGuid });

                vss.GatherWriterMetadata();

                IList <IVssWMComponent> components = new List <IVssWMComponent>();
                // key: volumePath, value: volumeName. These values are equivalent on a standard volume, but differ in the CSV case
                IDictionary <string, string> volumeMap = new Dictionary <string, string>();

                var wm = vss.WriterMetadata.Where((o) => o.WriterId.Equals(hyperVwriterGuid)).FirstOrDefault();
                foreach (var component in wm.Components)
                {
                    if (vmNamesMapSubset.ContainsKey(component.ComponentName))
                    {
                        components.Add(component);
                        vss.AddComponent(wm.InstanceId, wm.WriterId, component.Type, component.LogicalPath, component.ComponentName);
                        foreach (var file in component.Files)
                        {
                            string volumeName = null;
                            string volumePath = null;

                            volumePath = Path.GetPathRoot(file.Path).ToUpper();
                            volumeName = volumePath;

                            if (!volumeMap.ContainsKey(volumePath))
                            {
                                volumeMap.Add(volumePath, volumeName);
                            }
                        }
                    }
                }

                if (components.Count > 0)
                {
                    Guid vssSet = vss.StartSnapshotSet();

                    // Key: volumeName, value: snapshotGuid
                    IDictionary <string, Guid> snapshots = new Dictionary <string, Guid>();

                    foreach (var volumeName in volumeMap.Values)
                    {
                        snapshots.Add(volumeName, vss.AddToSnapshotSet(volumeName, Guid.Empty));
                    }

                    vss.PrepareForBackup();

                    RaiseEvent(EventAction.StartingSnaphotSet, components, volumeMap);
                    vss.DoSnapshotSet();
                    RaiseEvent(EventAction.SnapshotSetDone, components, volumeMap);

                    // key: volumeName, value: snapshotVolumePath
                    IDictionary <string, string> snapshotVolumeMap = new Dictionary <string, string>();

                    foreach (var kv in snapshots)
                    {
                        snapshotVolumeMap.Add(kv.Key, vss.GetSnapshotProperties(kv.Value).SnapshotDeviceObject);
                    }

                    BackupFiles(components, volumeMap, snapshotVolumeMap, vmNamesMapSubset, options);

                    foreach (var component in components)
                    {
                        vss.SetBackupSucceeded(wm.InstanceId, wm.WriterId, component.Type, component.LogicalPath, component.ComponentName, true);
                    }

                    vss.BackupComplete();

                    RaiseEvent(EventAction.DeletingSnapshotSet, components, volumeMap);
                    vss.DeleteSnapshotSet(vssSet, true);
                }
            }
        }
Beispiel #25
0
        public ISnapshot[] CreateVolumeSnapShot(List <FileSystem> volumes, string[] spoPaths, SnapshotSupportedLevel level)
        {
            using (new Alphaleonis.Win32.Security.PrivilegeEnabler(Privilege.Backup, Privilege.Restore)){
                //PrivilegesManager pm = new PrivilegesManager();
                //pm.Grant();
                VssBackupType vssLevel = VssBackupType.Full;
                if (level == SnapshotSupportedLevel.Full)
                {
                    vssLevel = VssBackupType.Full;
                }
                else if (level == SnapshotSupportedLevel.Incremental)
                {
                    vssLevel = VssBackupType.Incremental;
                }
                else if (level == SnapshotSupportedLevel.Differential)
                {
                    vssLevel = VssBackupType.Differential;
                }
                else if (level == SnapshotSupportedLevel.TransactionLog)
                {
                    vssLevel = VssBackupType.Log;
                }
                ArrayList snapshots = new ArrayList();
                Metadata = new SPOMetadata();
                bool snapshotSuccedeed = false;
                try{
                    IVssImplementation vss = VssUtils.LoadImplementation();
                    backup = vss.CreateVssBackupComponents();
                    Logger.Append(Severity.DEBUG, "0/6 Initializing Snapshot (" + ((spoPaths == null)? "NON-component mode" : "component mode") + ", level " + level + ")");
                    backup.InitializeForBackup(null);
                    if (spoPaths == null)            // component-less snapshot set
                    {
                        backup.SetBackupState(false, true, VssBackupType.Full, false);
                    }
                    else
                    {
                        backup.SetBackupState(true, true, vssLevel, (vssLevel == VssBackupType.Full)?false:true);
                    }
                    if (OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
                    {
                        // The only context supported on Windows XP is VssSnapshotContext.Backup
                        backup.SetContext(VssSnapshotContext.AppRollback /*|VssSnapshotContext.All*/);
                    }
                    Logger.Append(Severity.DEBUG, "1/6 Gathering writers metadata and status");
                    using (IVssAsync async = backup.GatherWriterMetadata()){
                        async.Wait();
                        async.Dispose();
                    }
                    // gather writers status before adding backup set components
                    using (IVssAsync async = backup.GatherWriterStatus()){
                        async.Wait();
                        async.Dispose();
                    }
                    Logger.Append(Severity.DEBUG, "2/6 Adding writers and components");
                    // Now we add the components (vss writers, writer paths/params) of this snapshot set
                    if (spoPaths != null)
                    {
                        foreach (IVssExamineWriterMetadata writer in backup.WriterMetadata)
                        {
                            foreach (string spo in spoPaths)
                            {
                                //Logger.Append (Severity.TRIVIA, "Searching writer and/or component matching "+spo);
                                int index = spo.IndexOf(writer.WriterName);
                                if (index < 0 && spo != "*")
                                {
                                    continue;
                                }
                                bool found = false;
                                Logger.Append(Severity.TRIVIA, "Found matching writer " + writer.WriterName + ", instance name=" + writer.InstanceName);
                                // First we check that the writer's status is OK, else we don't add it to avoid failure of complete snapshot if it's not
                                bool writerOk = false;
                                foreach (VssWriterStatusInfo status in backup.WriterStatus)
                                {
                                    if (status.Name == writer.WriterName)
                                    {
                                        Logger.Append(Severity.TRIVIA, "Checking required writer " + status.Name
                                                      + ", status=" + status.State.ToString() + ", error state=" + status.Failure.ToString());
                                        if (status.State == VssWriterState.Stable && status.Failure == VssError.Success)                                // if we get there it means that we are ready to add the wanted component to VSS set
                                        {
                                            writerOk = true;
                                        }
                                        else
                                        {
                                            Logger.Append(Severity.ERROR, "Cannot add writer " + status.Name + " to snapshot set,"
                                                          + " status=" + status.State.ToString() + ". Backup data  managed by this writer may not be consistent");
                                            if (LogEvent != null)
                                            {
                                                LogEvent(this, new LogEventArgs(820, Severity.WARNING, status.Name + ", Status=" + status.State.ToString() + ", Failure=" + status.Failure.ToString()));
                                            }
                                        }
                                    }
                                }
                                if (!writerOk)
                                {
                                    if (OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
                                    {
                                        backup.DisableWriterClasses(new Guid[] { writer.WriterId });
                                    }
                                    continue;
                                }
                                bool addAllComponents = false;
                                if (spo.Length == index + writer.WriterName.Length || spo == "*" || spo == "" + writer.WriterName + @"\*")
                                {
                                    addAllComponents = true;
                                }
                                foreach (IVssWMComponent component in writer.Components)
                                {
                                    found = false;
                                    //Console.WriteLine("createvolsnapshot : current component is :"+component.LogicalPath+@"\"+component.ComponentName);
                                    if ((!addAllComponents) && spo.IndexOf(component.LogicalPath + @"\" + component.ComponentName) < 0)
                                    {
                                        continue;
                                    }
                                    //Logger.Append (Severity.TRIVIA, "Asked to recursively select all '"+writer.WriterName+"' writer's components");
                                    if (OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
                                    {
                                        foreach (VssWMDependency dep in  component.Dependencies)
                                        {
                                            Logger.Append(Severity.TRIVIA, "Component " + component.ComponentName + " depends on component " + dep.ComponentName);
                                        }
                                    }
                                    if (component.Selectable)
                                    {
                                        backup.AddComponent(writer.InstanceId, writer.WriterId, component.Type, component.LogicalPath, component.ComponentName);
                                    }
                                    Logger.Append(Severity.INFO, "Added writer '" + writer.WriterName + "' component " + component.ComponentName);
                                    found = true;

                                    // Second we need to find every drive containing files necessary for writer's backup
                                    // and add them to drives list, in case they weren't explicitely selected as part of backuppaths
                                    List <VssWMFileDescription> componentFiles = new List <VssWMFileDescription>();
                                    componentFiles.AddRange(component.Files);
                                    componentFiles.AddRange(component.DatabaseFiles);
                                    componentFiles.AddRange(component.DatabaseLogFiles);
                                    foreach (VssWMFileDescription file in componentFiles)
                                    {
                                        if (string.IsNullOrEmpty(file.Path))
                                        {
                                            continue;
                                        }
                                        //Console.WriteLine ("component file path="+file.Path+", alt backuplocation="+file.AlternateLocation
                                        //		+", backuptypemask="+file.BackupTypeMask.ToString()+", spec="+file.FileSpecification+", recursive="+file.IsRecursive);
                                        // TODO : Reuse GetInvolvedDrives (put it into VolumeManager class)
                                        string drive = file.Path.Substring(0, 3).ToUpper();
                                        if (drive.Contains(":") && drive.Contains("\\"))
                                        {
                                            var searchedVol = from FileSystem vol in volumes
                                                              where vol.MountPoint.Contains(drive)
                                                              select vol;
                                            //if(!volumes.Contains(drive)){
                                            if (searchedVol == null)
                                            {
                                                Logger.Append(Severity.INFO, "Select VSS component " + component.LogicalPath
                                                              + @"\" + component.ComponentName + " requires snapshotting of drive " + drive + ", adding it to the list.");
                                                volumes.Add(searchedVol.First());
                                            }
                                            break;
                                        }
                                    }

                                    //Logger.Append(Severity.TRIVIA, "Added writer/component "+spo);
                                    //break;
                                }
                                //metadata.Metadata.Add(writer.SaveAsXml());
                                Metadata.Metadata.Add(writer.WriterName, writer.SaveAsXml());
                                if (found == false)
                                {
                                    Logger.Append(Severity.WARNING, "Could not find VSS component " + spo + " which was part of backup paths");
                                }
                            }
                        }
                    }
                    Logger.Append(Severity.DEBUG, "3/6 Preparing Snapshot ");
                    //backup.SetBackupState(false,  true, VssBackupType.Full, false);
                    Guid snapID = backup.StartSnapshotSet();
                    //Guid volID = new Guid();
                    foreach (FileSystem volume in volumes)
                    {
                        VSSSnapshot snapshot = new VSSSnapshot();
                        snapshot.Type = this.Name;
                        Logger.Append(Severity.DEBUG, "Preparing Snapshot of " + volume.MountPoint);
                        if (volume.MountPoint != null && backup.IsVolumeSupported(volume.MountPoint))
                        {
                            snapshot.Id = backup.AddToSnapshotSet(volume.MountPoint);

                            snapshot.Path = volume.MountPoint;
                        }
                        else                  // return the fake provider to get at least a degraded backup, better than nothing
                        {
                            Logger.Append(Severity.WARNING, "Volume '" + volume.MountPoint + "' is not snapshottable (or null). Backup will be done without snapshot, risks of data inconsistancy.");
                            ISnapshotProvider fakeSnapProvider = SnapshotProvider.GetProvider("NONE");
                            List <FileSystem> fakeList         = new List <FileSystem>();
                            fakeList.Add(volume);
                            snapshot = (VSSSnapshot)fakeSnapProvider.CreateVolumeSnapShot(fakeList, null, SnapshotSupportedLevel.Full)[0];
                        }
                        if (snapshot.Id == System.Guid.Empty)
                        {
                            Logger.Append(Severity.ERROR, "Unable to add drive " + volume.MountPoint + " to snapshot set (null guid)");
                        }
                        else
                        {
                            Logger.Append(Severity.TRIVIA, "Drive " + volume.MountPoint + " will be snapshotted to " + snapshot.Id);
                        }
                        snapshots.Add(snapshot);
                    }
                    Logger.Append(Severity.DEBUG, "4/6 Calling Prepare...");
                    using (IVssAsync async = backup.PrepareForBackup()){
                        async.Wait();
                        async.Dispose();
                    }
                    Logger.Append(Severity.DEBUG, "5/6 Snapshotting volumes");
                    using (IVssAsync async = backup.DoSnapshotSet()){
                        async.Wait();
                        async.Dispose();
                    }
                    //if(OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003))
                    foreach (IVssExamineWriterMetadata w in backup.WriterMetadata)
                    {
                        foreach (IVssWMComponent comp in w.Components)
                        {
                            try{
                                backup.SetBackupSucceeded(w.InstanceId, w.WriterId, comp.Type, comp.LogicalPath, comp.ComponentName, true);
                                Logger.Append(Severity.TRIVIA, "Component " + comp.ComponentName + " has been notified about backup success.");
                            }
                            catch (Exception) {
                                //Logger.Append (Severity.WARNING, "Could not notify component "+comp.ComponentName+" about backup completion : "+se.Message);
                            }
                        }
                    }
                    //Node.Misc.VSSObjectHandle.StoreObject(backup);
                    try{
                        //on XP backupcomplete consider that we have done with the snapshot and releases it.
                        //if(OperatingSystemInfo.IsAtLeast(OSVersionName.WindowsServer2003)){
                        backup.BackupComplete();

                        Metadata.Metadata.Add("_bcd_", backup.SaveAsXml());
                        //}
                    }catch (Exception bce) {
                        Logger.Append(Severity.WARNING, "Error calling VSS BackupComplete() : " + bce.Message);
                    }

                    using (IVssAsync async = backup.GatherWriterStatus()){
                        async.Wait();
                        async.Dispose();
                    }

                    Logger.Append(Severity.DEBUG, "6/6 Successfully shapshotted volume(s) ");

                    foreach (ISnapshot sn in snapshots)
                    {
                        if (sn.Id == Guid.Empty)
                        {
                            continue;
                        }
                        sn.MountPoint = backup.GetSnapshotProperties(sn.Id).SnapshotDeviceObject;
                        sn.TimeStamp  = Utilities.Utils.GetUtcUnixTime(backup.GetSnapshotProperties(sn.Id).CreationTimestamp.ToUniversalTime());

                        /*
                         * DirectoryInfo snapMountPoint = Directory.CreateDirectory( Path.Combine(Utilities.ConfigManager.GetValue("Backups.TempFolder"), "snapshot_"+sn.Id));
                         * Logger.Append(Severity.DEBUG, "Mounting shapshotted volume '"+sn.Name+"' to '"+snapMountPoint.FullName+"'");
                         * backup.ExposeSnapshot(sn.Id, null, VssVolumeSnapshotAttributes.ExposedLocally, snapMountPoint.FullName);
                         * //sn.Path = snapMountPoint.FullName+Path.DirectorySeparatorChar;
                         * //sn.Path = @"\\?\Volume{"+sn.Id+"}"+Path.DirectorySeparatorChar;*/
                    }
                }
                catch (Exception e) {
                    try{
                        //backup.BackupComplete();
                        backup.AbortBackup();
                    }
                    catch (Exception ae) {
                        Logger.Append(Severity.ERROR, "Error trying to cancel VSS snapshot set : " + ae.Message);
                    }

                    // TODO !! report snapshoty failure to hub task
                    Logger.Append(Severity.ERROR, "Error creating snapshot :'" + e.Message + " ---- " + e.StackTrace + "'. Backup will continue without snapshot. Backup of VSS components will fail. !TODO! report that to hub");
                    backup.Dispose();
                    throw new Exception(e.Message);
                }
                finally{
                    // TODO !!! reactivate dispose
                    //backup.Dispose();

                    //pm.Revoke();
                }

                return((ISnapshot[])snapshots.ToArray(typeof(ISnapshot)));
            }
        }
Beispiel #26
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}");
            }
        }