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

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


No comments:

Post a Comment