Пример #1
0
        /// <summary>
        ///   AgDatabaseMove the database to all instances of the availability group.
        ///   To join the AG, Finalize must be set.
        /// </summary>
        /// <param name="lastLsn">The last restored LSN used to continue while in no recovery mode.</param>
        /// <returns>The last LSN restored.</returns>
        public decimal Move(decimal?lastLsn = null)
        {
            if (!_options.Overwrite && _options.Destination.Exists() && !_options.Destination.Restoring)
            {
                throw new ArgumentException("Database exists and overwrite option is not set");
            }

            if (lastLsn == null && _options.Destination.Restoring)
            {
                throw new
                      ArgumentException("lastLsn parameter can only be used if the Destination database is in a restoring state");
            }

            if (_options.Overwrite)
            {
                _options.Destination.Delete();
            }

            _options.Source.LogBackup();

            var backupChain = new BackupChain(_options.Source);
            var backupList  = backupChain.OrderedBackups.ToList();

            if (_options.Destination.Restoring && lastLsn != null)
            {
                backupList.RemoveAll(b => b.LastLsn <= lastLsn.Value);
            }

            if (!backupList.Any())
            {
                throw new BackupChainException("No backups found to restore");
            }

            _options.Destination.Restore(backupList, _options.FileRelocator);

            if (_options.CopyLogins)
            {
                _options.Destination.CopyLogins(_options.Source.AssociatedLogins().Select(UpdateDefaultDb).ToList());
            }

            if (_options.Finalize)
            {
                _options.Destination.JoinAg();
            }

            if (_options.DeleteSource)
            {
                _options.Source.Delete();
            }

            return(backupList.Max(bl => bl.LastLsn));
        }
Пример #2
0
        /// <summary>
        ///   Restore the database to all instances of the availability group.
        ///   To join the AG, Finalize must be set.
        /// </summary>
        /// <param name="lastLsn">The last restored LSN used to continue while in no recovery mode.</param>
        /// <returns>The last LSN restored.</returns>
        public decimal AgDbRestore(decimal?lastLsn = null)
        {
            if (!Overwrite && _destination.Exists() && !_destination.Restoring)
            {
                throw new ArgumentException("Database exists and overwrite option is not set.");
            }

            if (lastLsn != null && !_destination.Restoring)
            {
                throw new
                      ArgumentException("Database is not in a restoring state which is required to use the lastLsn parameter.");
            }

            var backupChain = new BackupChain(_source);
            var backupList  = backupChain.RestoreOrder.ToList();

            if (_destination.Restoring && lastLsn != null)
            {
                backupList.RemoveAll(b => b.LastLsn <= lastLsn.Value);

                if (!backupList.Any())
                {
                    throw new BackupChainException("No backups found to restore.");
                }
            }

            _destination.Restore(backupList, FileRelocator);

            if (Finalize)
            {
                _destination.JoinAg();
            }

            if (CopyLogins)
            {
                _destination.CopyLogins(_source.AssociatedLogins().Select(UpdateDefaultDb).ToList());
            }

            return(backupList.Max(bl => bl.LastLsn));
        }