public Dictionary<string, List<string>> List(string server, string instance)
        {
            DbSnapshotMgr mgr = new DbSnapshotMgr();
            DbConnectionInfo connectionInfo = new DbConnectionInfo(server, instance);

            return mgr.ListSnapshots(connectionInfo);
        }
        public string Restore(string server, string instance, string database, string snapshot, string username = "", string password="")
        {
            DbSnapshotMgr mgr = new DbSnapshotMgr();
            DbConnectionInfo connectionInfo;
            if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
            {
                connectionInfo = new DbConnectionInfo(server, instance, username, password);
            }
            else
            {
                connectionInfo = new DbConnectionInfo(server, instance);
            }

            try
            {
                  mgr.RestoreSnapshot(connectionInfo,database,snapshot);
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }

            return "Ok";
        }
Ejemplo n.º 3
0
        public Dictionary<string, List<string>> ListSnapshots(DbConnectionInfo connectionInfo)
        {
            Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();

            if (connectionInfo == null)
                throw new ArgumentNullException("connectionInfo must be initialized to create a snapshot.");

            // 1. Initiate db connection
            Server server = connectionInfo.GetServer();

            foreach (Database database in server.Databases)
            {
                if(database.IsDatabaseSnapshot)
                {
                    if(!result.ContainsKey(database.DatabaseSnapshotBaseName))
                    {
                        result.Add(database.DatabaseSnapshotBaseName,new List<string>());
                    }

                    result[database.DatabaseSnapshotBaseName].Add(database.Name);
                }
            }

            return result;
        }
            public void CreateSnapshotValid()
            {
                var target = new DbSnapshotMgr();
                var connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string databaseName = "Certit";
                string snapshotDatabaseName = string.Empty;

                target.CreateSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
            }
            public void CreateSnapshotMissingDb()
            {
                var target = new DbSnapshotMgr();
                var connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string databaseName = "UnknowDatabase";
                string snapshotDatabaseName = string.Empty;

                try
                {
                    target.CreateSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    Assert.IsTrue(ex.Message.Contains(databaseName));
                }
            }
        public string Delete(string server, string instance,  string snapshot)
        {
            DbSnapshotMgr mgr = new DbSnapshotMgr();
            DbConnectionInfo connectionInfo = new DbConnectionInfo(server,instance);
            try
            {
                  mgr.DeleteSnapshot(connectionInfo,snapshot);
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }

            return "Ok";
        }
            public void CreateSnapshotNoValidDbNameParameters()
            {
                var target = new DbSnapshotMgr();
                var connectionInfo = new DbConnectionInfo();
                string databaseName = string.Empty;
                string snapshotDatabaseName = string.Empty;

                try
                {
                    target.CreateSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
                    Assert.Fail();
                }
                catch (ArgumentNullException ex)
                {
                    Assert.IsTrue(ex.Message.Contains("databaseName"));
                }
            }
Ejemplo n.º 8
0
        public void CreateSnapshot(DbConnectionInfo connectionInfo, string databaseName, string snapshotDatabaseName = null)
        {
            if (connectionInfo == null)
                throw new ArgumentNullException("connectionInfo must be initialized to create a snapshot.");
            if (string.IsNullOrWhiteSpace(databaseName))
                throw new ArgumentNullException("databaseName is required to create a snapshot.");

            // 1. Initiate db connection
            Server server = connectionInfo.GetServer();

            // 2. Check if DB Exists
            Database db = server.Databases[databaseName];
            if (db == null)
                throw new ArgumentOutOfRangeException(string.Format("The database {0} does not exists.", databaseName));

            var snapshot = db.IsDatabaseSnapshot;
            var hasSnapshots = db.IsDatabaseSnapshotBase;

            string dbSnapshotName = string.IsNullOrWhiteSpace(snapshotDatabaseName) ? databaseName + "_" + DateTime.UtcNow.ToString("yyMMdd") : snapshotDatabaseName;

            Database snapshotDatabase = new Database(server, dbSnapshotName);
            snapshotDatabase.DatabaseSnapshotBaseName = db.Name;
            foreach (FileGroup fileGroup in db.FileGroups)
            {
                FileGroup fg = new FileGroup(snapshotDatabase, fileGroup.Name);
                snapshotDatabase.FileGroups.Add(fg);
            }

            foreach (FileGroup fileGroup in db.FileGroups)
            {
                foreach (DataFile dataFile in fileGroup.Files)
                {
                    var df = new DataFile(snapshotDatabase.FileGroups[fileGroup.Name],
                                            dataFile.Name,
                                            Path.Combine(db.PrimaryFilePath, string.Format("{0}_{1}.ss", dataFile.Name, snapshotDatabaseName)));
                    snapshotDatabase.FileGroups[fileGroup.Name].Files.Add(df);
                }
            }
            // 3.

            snapshotDatabase.Create();
        }
Ejemplo n.º 9
0
        public void DeleteSnapshot(DbConnectionInfo connectionInfo, string snapshotDatabaseName)
        {
            if (connectionInfo == null)
                throw new ArgumentNullException("connectionInfo must be initialized to create a snapshot.");
            if (string.IsNullOrWhiteSpace(snapshotDatabaseName))
                throw new ArgumentNullException("databaseName is required to create a snapshot.");

            // 1. Initiate db connection
            Server server = connectionInfo.GetServer();

            // 2. Check if DB Exists and if it's not a snapshot
            Database db = server.Databases[snapshotDatabaseName];
            if (db == null)
                throw new ArgumentOutOfRangeException(string.Format("The database {0} does not exists.", snapshotDatabaseName));

            if (!db.IsDatabaseSnapshot)
                throw new ArgumentException(string.Format("The database {0} is not a snapshot database.", snapshotDatabaseName));

            db.Drop();
        }
            public void ValidRestore()
            {
                DbSnapshotMgr target = new DbSnapshotMgr();
                DbConnectionInfo connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string databaseName = "Certit";
                string snapshotDatabaseName = "Certit_120725";

                target.RestoreSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
            }
            public void SnapshotDbIsNotASnapshot()
            {
                DbSnapshotMgr target = new DbSnapshotMgr();
                DbConnectionInfo connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string databaseName = "Certit";
                string snapshotDatabaseName = "Certit";

                try
                {
                    target.RestoreSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
                    Assert.Fail();
                }
                catch (ArgumentException ex)
                {
                    Assert.IsTrue(ex.Message.Contains("is not a snapshot"));
                }
            }
            public void InvalidSnapshot()
            {
                DbSnapshotMgr target = new DbSnapshotMgr();
                DbConnectionInfo connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string databaseName = "Certit";
                string snapshotDatabaseName = "UnknowSnapshotDb";

                try
                {
                    target.RestoreSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
                    Assert.Fail();
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    Assert.IsTrue(ex.Message.Contains("does not exists"));
                }
            }
            public void InvalidDatabaseName()
            {
                DbSnapshotMgr target = new DbSnapshotMgr();
                DbConnectionInfo connectionInfo = new DbConnectionInfo();
                string databaseName = string.Empty;
                string snapshotDatabaseName = string.Empty;

                try
                {
                    target.RestoreSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
                    Assert.Fail();
                }
                catch (ArgumentNullException ex)
                {
                    Assert.IsTrue(ex.Message.Contains("databaseName"));
                }
            }
            public void DeleteSnapshotValid()
            {
                var target = new DbSnapshotMgr();
                var connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string snapshotDatabaseName = "Certit_pretest";

                target.DeleteSnapshot(connectionInfo, snapshotDatabaseName);
            }
            public void DeleteSnapshotDbIsNotASnapshot()
            {
                var target = new DbSnapshotMgr();
                var connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string snapshotDatabaseName = "Certit";

                try
                {
                    target.DeleteSnapshot(connectionInfo, snapshotDatabaseName);
                }
                catch (ArgumentException ex)
                {
                    Assert.IsTrue(ex.Message.Contains("is not a snapshot database"));
                }
            }
            public void ListSnapshot()
            {
                DbSnapshotMgr target = new DbSnapshotMgr();
                DbConnectionInfo connectionInfo = new DbConnectionInfo(@".\", "SQL2012");

                var result = target.ListSnapshots(connectionInfo);
                Assert.IsTrue(result.Count > 0);
            }
            public void CreateSnapshotValidthCustomSnapshotName()
            {
                var target = new DbSnapshotMgr();
                var connectionInfo = new DbConnectionInfo(@".\", "SQL2012");
                string databaseName = "Certit";
                string snapshotDatabaseName = "Certit_pretest";

                try
                {
                    target.CreateSnapshot(connectionInfo, databaseName, snapshotDatabaseName);
                }
                catch (ArgumentNullException ex)
                {
                    Assert.IsTrue(ex.Message.Contains("databaseName"));
                }
            }
Ejemplo n.º 18
0
        public void RestoreSnapshot(DbConnectionInfo connectionInfo, string databaseName, string snapshotDatabaseName)
        {
            if (connectionInfo == null)
                throw new ArgumentNullException("connectionInfo must be initialized to create a snapshot.");
            if (string.IsNullOrWhiteSpace(databaseName))
                throw new ArgumentNullException("databaseName is required to create a snapshot.");

            // 1. Initiate db connection
            Server server = connectionInfo.GetServer();

            // 2. Check if DB Exists and if it's not a snapshot
            Database db = server.Databases[databaseName];
            if (db == null)
                throw new ArgumentOutOfRangeException(string.Format("The database {0} does not exists.", databaseName));

            Database snapshotDb = server.Databases[snapshotDatabaseName];
            if (snapshotDb == null)
                throw new ArgumentOutOfRangeException(string.Format("The database {0} does not exists.", snapshotDatabaseName));

            if (!snapshotDb.IsDatabaseSnapshot)
                throw new ArgumentException(string.Format("The database {0} is not a snapshot database.", snapshotDatabaseName));

            string restoreSQL = "RESTORE DATABASE {0} FROM DATABASE_SNAPSHOT = '{1}'";

            server.ConnectionContext.ExecuteNonQuery(string.Format(restoreSQL, db.Name, snapshotDb.Name));
        }