/// <summary>
        /// Backups the site.
        /// </summary>
        /// <param name="site">The site.</param>
        private void BackupSiteCore(SPSite site)
        {
            string path = Path.Combine(_path, EncodePath(site.Url.ToString())) + ".bak";

            Logger.Write("Backing up site '{0}' to '{1}'", site.Url, path);

            if (!_useSnapshot)
            {
                bool writeLock = site.WriteLocked;
                if (!_noSiteLock)
                {
                    site.WriteLocked = true;
                }
                try
                {
                    site.WebApplication.Sites.Backup(site.Url, path, _overwrite);
                }
                finally
                {
                    if (!_noSiteLock)
                    {
                        site.WriteLocked = writeLock;
                    }
                }
            }
            else
            {
                SPDatabaseSnapshot snapshot = null;
                try
                {
                    snapshot = site.ContentDatabase.Snapshots.CreateSnapshot();
                    SPContentDatabase database   = SPContentDatabase.CreateUnattachedContentDatabase(snapshot.ConnectionString);
                    string            strSiteUrl = site.HostHeaderIsSiteName ? site.Url.ToString() : site.ServerRelativeUrl;
                    database.Sites.Backup(strSiteUrl, path, _overwrite);
                }
                finally
                {
                    if (snapshot != null)
                    {
                        snapshot.Delete();
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Performs the export.
        /// </summary>
        /// <param name="export">The export.</param>
        /// <param name="exportObject">The export object.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="logFile">if set to <c>true</c> [log file].</param>
        /// <param name="quiet">if set to <c>true</c> [quiet].</param>
        /// <param name="url">The URL.</param>
        internal static void PerformExport(SPExport export, SPExportObject exportObject, SPExportSettings settings, bool logFile, bool quiet, string url, bool useSqlSnapshot)
        {
            SPDatabaseSnapshot snapshot = null;

            using (SPSite site = new SPSite(url))
            {
                ValidateUser(site);

                if (!useSqlSnapshot)
                {
                    settings.SiteUrl = site.Url;
                }
                else
                {
                    snapshot = site.ContentDatabase.Snapshots.CreateSnapshot();
                    SPContentDatabase database2 = SPContentDatabase.CreateUnattachedContentDatabase(snapshot.ConnectionString);
                    settings.UnattachedContentDatabase = database2;
                    settings.SiteUrl = database2.Sites[site.ServerRelativeUrl].Url;
                }

                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = Utilities.GetListFromViewUrl(web, url);

                    if (list == null)
                    {
                        throw new Exception("List not found.");
                    }

                    settings.SiteUrl = web.Url;
                    exportObject.Id  = list.ID;
                }

                settings.ExportObjects.Add(exportObject);


                try
                {
                    export.Run();
                    if (!quiet)
                    {
                        ArrayList dataFiles = settings.DataFiles;
                        if (dataFiles != null)
                        {
                            Console.WriteLine();
                            Console.WriteLine("File(s) generated: ");
                            for (int i = 0; i < dataFiles.Count; i++)
                            {
                                Console.WriteLine("\t{0}", Path.Combine(settings.FileLocation, (string)dataFiles[i]));
                                Console.WriteLine();
                            }
                            Console.WriteLine();
                        }
                    }
                }
                finally
                {
                    if (useSqlSnapshot && (snapshot != null))
                    {
                        snapshot.Delete();
                    }

                    if (logFile)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Log file generated: ");
                        Console.WriteLine("\t{0}", settings.LogFilePath);
                    }
                }
            }
        }
        protected override void InternalProcessRecord()
        {
            bool createLogFile = !base.NoLogFile.IsPresent;

            if (!base.NoFileCompression.IsPresent)
            {
                this.ValidateDirectory(base.Path);
            }
            if ((!base.Force.IsPresent) && File.Exists(base.Path))
            {
                throw new SPException(string.Format("PSCannotOverwriteExport,{0}", base.Path));
            }
            SPExportSettings settings = new SPExportSettings();
            SPExport         export   = new SPExport(settings);

            base.SetDeploymentSettings(settings);
            settings.ExportMethod        = SPExportMethodType.ExportAll;
            settings.ExcludeDependencies = !IncludeDependencies.IsPresent;
            SPDatabaseSnapshot snapshot = null;

            if (!this.UseSqlSnapshot.IsPresent)
            {
                settings.SiteUrl = this.SiteUrl;
            }
            else
            {
                snapshot = this.Site.ContentDatabase.Snapshots.CreateSnapshot();
                SPContentDatabase database2 = SPContentDatabase.CreateUnattachedContentDatabase(snapshot.ConnectionString);
                settings.UnattachedContentDatabase = database2;
                settings.SiteUrl = database2.Sites[this.Site.ServerRelativeUrl].Url;
            }
            SPExportObject exportObject = this.ExportObject;

            if (((exportObject.Type != SPDeploymentObjectType.Web) ||
                 base.ShouldProcess(string.Format("ShouldProcessExportWeb,{0},{1}", this.SiteUrl, base.Path))) &&
                ((exportObject.Type != SPDeploymentObjectType.List) ||
                 base.ShouldProcess(string.Format("ShouldProcessExportList,{0},{1}", this.SiteUrl + "/" + this.ItemUrl, base.Path))))
            {
                if (exportObject != null)
                {
                    exportObject.ExcludeChildren    = ExcludeChildren.IsPresent;
                    exportObject.IncludeDescendants = IncludeDescendants;
                    settings.ExportObjects.Add(exportObject);
                }
                settings.IncludeVersions = this.IncludeVersions;
                if (base.IncludeUserSecurity.IsPresent)
                {
                    settings.IncludeSecurity = SPIncludeSecurity.All;
                }
                settings.OverwriteExistingDataFile = (bool)base.Force;
                settings.FileMaxSize = this.CompressionSize;
                try
                {
                    export.Run();
                }
                finally
                {
                    if (base.Verbose && createLogFile)
                    {
                        Console.WriteLine();
                        Console.WriteLine(SPResource.GetString("ExportOperationLogFile", new object[0]));
                        Console.WriteLine("\t{0}", settings.LogFilePath);
                    }
                    if ((this.UseSqlSnapshot.IsPresent) && (snapshot != null))
                    {
                        snapshot.Delete();
                    }
                }
                if (base.Verbose)
                {
                    string    fileLocation = settings.FileLocation;
                    ArrayList dataFiles    = settings.DataFiles;
                    if (dataFiles != null)
                    {
                        if (((fileLocation != null) && (fileLocation.Length > 0)) && (fileLocation[fileLocation.Length - 1] != System.IO.Path.DirectorySeparatorChar))
                        {
                            fileLocation = fileLocation + System.IO.Path.DirectorySeparatorChar;
                        }
                        Console.WriteLine();
                        Console.WriteLine(SPResource.GetString("ExportOperationFilesGenerated", new object[0]));
                        for (int i = 0; i < dataFiles.Count; i++)
                        {
                            Console.WriteLine("\t{0}{1}", fileLocation, dataFiles[i]);
                            Console.WriteLine();
                        }
                        if (base.NoFileCompression.IsPresent)
                        {
                            DirectoryInfo info = new DirectoryInfo(base.Path);
                            Console.WriteLine("\t{0}", info.FullName);
                        }
                        Console.WriteLine();
                    }
                }
            }
        }