回答済み Export site and then automatically import to backup site

  • 2012年8月3日 8:40
     
     

    Hi

    I want to export a site from Production  and then import this site to a backup server every 2 hours.

    How can this be achieved, is there a tool to make life simpler ?

    When the export is taking place on the Production server can users still use the site to make entries ?

    Thanks

すべての返信

  • 2012年8月3日 8:59
     
     

    If you are trying to copy content, Why don't you use content deployment?



    • 編集済み ova c 2012年8月3日 9:09
    •  
  • 2012年8月3日 9:26
     
     
    hi whats content deployment ?
  • 2012年8月3日 9:28
     
     

    Content Deployment allows you to copy content across farms, you can schedule a job and it can do it for you. It is good to keep a synced  test environment.

    Overview: http://technet.microsoft.com/en-us/library/ee721058.aspx

    Planning Content Deployment: http://technet.microsoft.com/en-us/library/cc263428.aspx


    ceren

  • 2012年8月3日 11:03
     
     

    Hi

    I've found a wizared for Content Deployment.  http://spdeploymentwizard.codeplex.com/

    Does it relate or are they two different things ?

  • 2012年8月3日 11:14
     
     

    Yes, this tool use the content deployment api to export sites.

    Be careful
    the content deployment api are quite fragile and not really performing.

    Have you tried to use the Backup / Restore api?

    http://technet.microsoft.com/library/cc261956(office.12).aspx

    This api works at database level. Content deployment api, instead, works at item leve.


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

  • 2012年8月3日 11:15
     
     回答済み

    I think they are different

    The Content Deployment option is OOB. You can find the related settings in Central Administration:

    check these:

    http://technet.microsoft.com/en-US/library/cc263090.aspx 

    http://technet.microsoft.com/en-US/library/cc262075.aspx

    and this may help:

    http://www.sharepointboost.com/blog/using-content-deployment-to-copy-one-site-collection-to-another-in-sharepoint-2010/


    ceren

  • 2012年8月3日 11:24
     
     回答済み コードあり

    Alternatively, you can use this code, which is provided as is, with my litlle hands, to use the backup / restore strategy.


    Obviously to be tested properly.

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint.Administration.Backup;
    
    namespace zzzzzzzz
    {
        /// <summary>
        /// provide functionalty to backup and restore SharePoint data
        /// </summary>
        public class BackupRestoreHelper : IDisposable
        {
    
            #region Backup
    
            /// <summary>
            /// Do a backup of a specific site collection and upload it on a specific document library
            /// </summary>
            /// <param name="sourceSiteUrl">The url of the site of wich you want to take a backup</param>
            /// <param name="destinationUri">The url of the file where you wanto to save the backuped data</param>
            public void BackupSite(Uri sourceSiteUrl, string destinationUri)
            {
                string tmpFolder = string.Empty;
    
                try
                {
                    //Get temp folder
                    tmpFolder = Environment.GetEnvironmentVariable("temp");
                    tmpFolder = Path.Combine(tmpFolder, Guid.NewGuid().ToString());
    
                    //Get filename from destination url
                    Uri destUri = new Uri(destinationUri);
                    string tmpFileName = Path.Combine(tmpFolder, destUri.Segments[destUri.Segments.Length - 1]);
    
                    //Create temp folder if not exists
                    DirectoryInfo di = new DirectoryInfo(tmpFolder);
                    if (!di.Exists)
                        di.Create();
    
                    //Do backup
                    DoBackup(sourceSiteUrl, tmpFileName);
    
                    //Do upload
                    CommonSPHelper.UploadFile(tmpFileName, destinationUri);
    
                    OnRaiseBackupCompletedEvent(new EventArgs());
                }
                catch (Exception)
                {
                    throw;
                }
    
                finally
                {
                    if (!string.IsNullOrEmpty(tmpFolder))
                    {
                        try
                        {
                            DirectoryInfo di = new DirectoryInfo(tmpFolder);
                            if (di.Exists)
                                di.Delete(true);
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
                    }
                }
    
            }
    
            /// <summary>
            /// Do a backup of a specific site collection and save it on specified file system path
            /// </summary>
            /// <param name="sourceSiteUrl">The url of the site of wich you want to take a backup</param>
            /// <param name="destinationFileName">The full name of the file where you want save the file</param>
            private void DoBackup(Uri sourceSiteUrl, string destinationFileName)
            {
                if (sourceSiteUrl == null)
                    throw new ArgumentNullException("sourceSiteUrl");
    
                if (string.IsNullOrEmpty(destinationFileName))
                    throw new ArgumentNullException("destinatioFileName");
    
    
                SPWebApplication sPWebApplication = SPWebApplication.Lookup(sourceSiteUrl);
                if (sPWebApplication == null)
                    throw new NotSupportedException(string.Format("Web application {0} does not exist.", sourceSiteUrl.AbsoluteUri));
    
                //using snapshot
                SPDatabaseSnapshot sPDatabaseSnapshot = null;
                try
                {
                    using (SPSite sPSite = new SPSite(sourceSiteUrl.ToString()))
                    {
                        SPContentDatabase contentDatabase = sPSite.ContentDatabase;
                        sPDatabaseSnapshot = contentDatabase.Snapshots.CreateSnapshot();
                        SPContentDatabase sPContentDatabase = SPContentDatabase.CreateUnattachedContentDatabase(sPDatabaseSnapshot.ConnectionString);
                        string strSiteUrl = sPSite.HostHeaderIsSiteName ? sPSite.Url.ToString() : sPSite.ServerRelativeUrl;
                        sPContentDatabase.Sites.Backup(strSiteUrl, destinationFileName, true);
    
                    }
                }
                finally
                {
                    if (sPDatabaseSnapshot != null)
                    {
                        sPDatabaseSnapshot.Delete();
                    }
                }
            }
    
            #region Events
    
            // Declare the event using EventHandler<T>
            public event EventHandler<EventArgs> BackupCompletedEvent;
    
            protected virtual void OnRaiseBackupCompletedEvent(EventArgs e)
            {
                // Make a temporary copy of the event to avoid possibility of
                // a race condition if the last subscriber unsubscribes
                // immediately after the null check and before the event is raised.
                EventHandler<EventArgs> handler = BackupCompletedEvent;
    
                // Event will be null if there are no subscribers
                if (handler != null)
                {
                    // Use the () operator to raise the event.
                    handler(this, e);
                }
            }
    
            #endregion
    
            #endregion
    
            #region Restore
    
            /// <summary>
            /// Restore a site from a backup 
            /// </summary>
            /// <param name="siteUrl">The destination url</param>
            /// <param name="templateUrl">The template url</param>
            /// <param name="webApplicationId">The id of the web application</param>
            /// <param name="database"></param>
            /// <param name="ownerName">The site collection owner name</param>
            /// <param name="ownerLogin">The site collection owner login</param>
            /// <param name="ownerEmail">The site collection owner e-mail</param>
            /// <param name="nLCID">The language code (1033, 1040...)</param>
            public void RestoreSiteFromTemplate(Uri siteUrl,
                                                       Uri templateUrl,
                                                       Guid webApplicationId,
                                                       AdminSPHelper.ContentDatabase database,
                                                       string ownerName,
                                                       string ownerLogin,
                                                       string ownerEmail,
                                                       uint nLCID)
            {
                //save template in temp folder
                string tmpFolder = string.Empty;
    
                try
                {
                    //Create empty site collection
                    var webApp = SPWebService.ContentService.WebApplications.Where(application => application.Id == webApplicationId).FirstOrDefault();
    
                    if (webApp == null)
                        throw new NotSupportedException(string.Format("Web application {0} does not exist.", webApplicationId));
    
                    try
                    {
                        if (!((database != null) && (database.Id != null)))
                        {
                            database = AdminSPHelper.GetPreferredContentDbWithCustomRoundRobin(webApplicationId);
                        }
    
                        webApp.ContentDatabases[database.Id].Sites.Add(siteUrl.AbsoluteUri,
                                                                           siteUrl.Segments[siteUrl.Segments.Length - 1],
                                                                           string.Empty,
                                                                           nLCID,
                                                                           null,
                                                                           ownerLogin,
                                                                           ownerName,
                                                                           ownerEmail);
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(string.Format("Error creating site collection {0}. {1}", siteUrl, ex.Message), ex);
                    }
    
                    //Get temp folder
                    tmpFolder = Environment.GetEnvironmentVariable("temp");
    
                    tmpFolder = Path.Combine(tmpFolder, Guid.NewGuid().ToString());
    
                    //Get filename from destination url
                    string tmpFileName = Path.Combine(tmpFolder, templateUrl.Segments[templateUrl.Segments.Length - 1]);
    
                    //Save template on temporary folder
                    CommonSPHelper.DownloadFile(templateUrl.AbsoluteUri, tmpFileName);
    
                    FileInfo fi = new FileInfo(tmpFileName);
    
                    if (fi.Exists)
                    {
                        DoRestore(siteUrl, tmpFileName);
                    }
                    else
                        throw new ApplicationException(string.Format("Temporary file {0} does not exist.", tmpFileName));
    
    
                    OnRaiseRestoreCompletedEvent(new EventArgs());
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (!string.IsNullOrEmpty(tmpFolder))
                    {
                        try
                        {
                            DirectoryInfo di = new DirectoryInfo(tmpFolder);
                            if (di.Exists)
                                di.Delete(true);
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
                    }
                }
            }
    
            /// <summary>
            /// Execute Restore method of a specific site collection
            /// </summary>
            /// <param name="sourceSiteUrl">The url of the backup package</param>
            /// <param name="destinationFileName">The url of the destination site collection</param>
            private void DoRestore(Uri sourceSiteUrl, string destinationFileName)
            {
                if (string.IsNullOrEmpty(sourceSiteUrl.AbsoluteUri))
                    throw new ArgumentNullException("sourceSiteUrl");
    
                if (string.IsNullOrEmpty(destinationFileName))
                    throw new ArgumentNullException("destinatioFileName");
    
                SPWebApplication sPWebApplication = SPWebApplication.Lookup(sourceSiteUrl);
                if (sPWebApplication == null)
                    throw new NotSupportedException(string.Format("Web application {0} does not exist.", sourceSiteUrl.AbsoluteUri));
    
                sPWebApplication.Sites.Restore(sourceSiteUrl.OriginalString, destinationFileName, true);
    
            }
    
            #region Events
    
            // Declare the event using EventHandler<T>
            public event EventHandler<EventArgs> RestoreCompletedEvent;
    
            protected virtual void OnRaiseRestoreCompletedEvent(EventArgs e)
            {
                // Make a temporary copy of the event to avoid possibility of
                // a race condition if the last subscriber unsubscribes
                // immediately after the null check and before the event is raised.
                EventHandler<EventArgs> handler = RestoreCompletedEvent;
    
                // Event will be null if there are no subscribers
                if (handler != null)
                {
                    // Use the () operator to raise the event.
                    handler(this, e);
                }
            }
    
            #endregion
    
    
    
            #endregion
    
            public void Dispose()
            {
                //TO DO 
            }
        }
    }



    Regards,
    Bubu
    http://zsvipullo.blogspot.it