Showing posts with label move site collection. Show all posts
Showing posts with label move site collection. Show all posts

Saturday, October 27, 2012

Programmatically how to create backup and restore a sitecolletion in SharePoint 2010

In previous post, have seen how to take backup and restore a site collection using power shell commands.

Here will take back up and restore a site collection using SharePoint API.

There are two methods to create back up and restore a site collection.

1. Backup and
2. Restore.

Here i want to maintain one log file for tracing backup and restore creation details, so for that created the following method.

here we need to create two folder in D drive for saving backup file and log file.
D:\SiteBackupLog and D:\SiteBackUp


        public static void siteBackupLog(string message)
        {
            string PathName = "D:\\SiteBackupLog\\";
     PathName = PathName + "Backuplog-" + DateTime.Now.ToString("dd-MM-yyyy") + ".log";
            StreamWriter sw = new StreamWriter(PathName, true);
            sw.WriteLine(message + " - " + DateTime.Now);
            sw.Flush();
            sw.Close();
        }

Now , for taking backup of a site collection , used the following method.

        private void btnBackup_Click(object sender, EventArgs e)
        {
            string sourceSiteCollURL = "http://ukreddy:6969";
            string time = DateTime.Now.ToString("dd-MM-yyyy");
            string backupfilename = "D:\\SiteBackup\\SharePointSiteBackup_" + time + ".bak";
            siteBackup(sourceSiteCollURL, backupfilename);
        }
        public static void siteBackup(string siteURL, string fileName)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWebApplication webApplication = null;
                    webApplication = SPWebApplication.Lookup(new Uri(siteURL));
                    SPSiteCollection sitecols = webApplication.Sites;
                    string backupName = fileName;
                    sitecols.Backup(siteURL, backupName, true);//true is used for if already bak file created with the same name , it will overwrite
                });
                siteBackupLog("Site Collection backup created succuessfully");
            }
            catch (Exception ex)
            {
                siteBackupLog(ex.Message);
            }
        }  

in the above code we are try to create backup file and saving it in the specified location and also writing log also.

And the following code is used to restore site collection in new web application i.e http://ukreddy:2727 and restore it as root site collection.


        private void btnRestore_Click(object sender, EventArgs e)
        {
            string sitecollectionURL = "http://ukreddy:2727";
            string backupfilename = "D:\\SiteBackup\\SharePointSiteBackup_27-10-2012.bak";
            SiteRestore(sitecollectionURL, backupfilename);
        }

        private void SiteRestore(string siteURL, string fileName)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWebApplication webApplication = null;
                    webApplication = SPWebApplication.Lookup(new Uri(siteURL));
                    SPSiteCollection sitecols = webApplication.Sites;
                sitecols.Restore("/", fileName, true);// overwrite sitecollection if already exists
                });
                siteBackupLog("Site collection restored successfully");
            }
            catch (Exception ex)
            {
               siteBackupLog(ex.Message);
            }
        }

here the screen shot of actual site collection i.e http://ukreddy:6969 and restored site collection i.e http://ukreddy:2727 respectively.






Thats it...


pls let me know if u have questions/suggestions.




Friday, October 19, 2012

How to move site collection from one web application to another web application using powershell


There are some situations where you want to move your site collection from one web application to another web application or where you want to change site collection port number or site collection path.

1. Take a backup of site collection from old webapplication
2. Restore backup in new web application.
3. Remove site collection from old web application
4. Delete backup file.

Here is my requirement, i have two web applications

1. http://ukreddy:2727 in this i have one root site collection
2. http://ukreddy:3636 This is my new web application 

now i want to move site collection from http://ukreddy:2727 web application to http://ukreddy:3636 web application or i want to change site collection port number.

In my site collection i have added some documents to document library, added events to calendar and add tasks to task list for testing.





1. Take backup of site collection 

Take backup of site from old web application(i.e http://ukreddy:2727).

Backup-SPSite -Identity http://ukreddy:2727 -Path "C:\\MoveSiteCollection.bak" 

or

Backup-SPSite -Identity http://ukreddy:2727 -Path "C:\\MoveSiteCollection.bak" -force

Note: -force is swicht parameter and it is used to create/overwrite if .bak file already exist.

This cmdlet will create .bak file in  "C:\\" path with the given name in cmdlet.



2. Restore backup file to new web application(i.e http://ukreddy:3636):

Here assuming that http://ukreddy:3636 web application created and root site collection not yet created.

Restore-SPSite -Identity http://ukreddy:3636 -Path "C:\\MoveSiteCollection.bak" 




or 

Restore-SPSite -Identity http://ukreddy:3636 -Path "C:\\MoveSiteCollection.bak"  -Confirm:$false



if you use -Confirm:$false parameter then it wont ask your confirmation for restore of site collection. Simply restore site collection.

This cmdlet will create root site collection in http://ukreddy:3636 as is it which in http://ukreddy:2727





3. Remove site collection 

Remove site collection from old web application.

Remove-SPSite -Identity http://ukreddy:2727 -Confirm:$false




4. Remove backup file

Remove the backup file from the path where we given path for backup.

Remove-Item "C:\\MoveSiteCollection.bak"




Here the code snippet copy the snippet and save it as .ps1 and run the .ps1 file in powershell....

###############################################################################

#Get the from Site Collection URL
$fromURL = "http://ukreddy:2727"
#Get the to Site Collection URL 
$toURL = "http://ukreddy:3636"

#Location to the backup file directory to store Site Collection backup files
$backupPath = "C:\\test.bak"

#Backing up Site Collection prior to moving
Write-Host "Backing Up Site Collection…."
Backup-SPSite $fromURL -Path C:\test.bak -force

#Removing Site Collection from old web application path
Write-Host "Removing Site Collection from old managed path location…"
Remove-SPSite -Identity $fromURL -Confirm:$false

#Restoring Site Collection to new Managed Path
Write-Host "Restoring Site Collection to new managed path location…"
Restore-SPSite -Identity $toURL -Path c:\test.bak -Confirm:$false

#Remove backup files from the backup dirctory
Remove-Item c:\test.bak

###############################################################################