Backup site collections from a .csv file using Powershell

Assuming that you have a .csv file which contains a list of site collections in this format

URL,path
http://site1,path1
http://urlpath/site2,path2

the line you need to backup them is this one:

ImportCsv yourfile.csv | ForEach-Object { Backup-SPSite $_.URL -Path $_.path -force}

 

But what if do you have only site’s URL? and the csv file looks like:

URL,path
http://site1
http://urlpath/site2

In this case you need to use URL to get the .bak file name

ImportCsv yourfile.csv | ForEach-Object { Backup-SPSite $_.URL -Path (($_.Url).replace(“/”,”).replace(“:”,”)+”.bak”)  -force}

where ($_.Url).replace(“/”,”).replace(“:”,”)+”.bak” will be the name of the backup file like this one
httpurlpathsite1.bak

To avoid overwriting, you could add datetimestamp in file’s name like this

Import-Csv yourfile.csv | ForEach-Object { Backup-SPSite $_.URL -Path (($_.Url).replace(“/”,”).replace(“:”,”)+”_”+(Get-Date -Format “yyyyMMddhhmm”)+”.bak”) -force
 }

and the back-up file will looks like:
httpurlpathsite1_201705010324.bak

This entry was posted in backup, Powershell, Sharepoint. Bookmark the permalink.