Using Powershell and 7zip to backup your website

backup website

Using Powershell and 7zip to backup your website

It’s a basic principle for your website; you need to have backups. There are many software packages out there that can help you backup websites but if you you don’t need anything complicated or just want the flexibility to make it work your way then a custom powershell script may be for you.

Getting Started

The first thing you need to know about powershell is that by default scripts will not run due to access restrictions. Running the following command will enable scripts in powershell

[sourcecode language=”powershell”]
#Allows scripts to be run.
Set-ExecutionPolicy Unrestricted

Backing up your website and database

The following script will grab all the databases in your MS SQL database instance and create .bak files that will be compressed. Just as important as your database is the website files themselves which will also be backed up and will be organized by folder if you have multiple websites.

[sourcecode language=”powershell”]
$currentDateStamp = (Get-Date -UFormat %Y).ToString() + (Get-Date -UFormat %m).ToString() + (Get-Date -UFormat %d).ToString() #Creates a date stamp in a specific format. ex. 2013-01-31
$outputMainDir = "F:Backups"
$sevenZip = "C:Program Files7-Zip7z.exe"
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition #Grabs the path that the script was run from
$scriptPath += ""

#Web Path Variables
$inputWebsDir = "F:Sites"
$outputWebsDir = $outputMainDir + "Webs"
#DB Path Variables
$inputDBDir = "F:BackupsDatabases"
$outputDBDir = $outputMainDir + "Databases"

#Backup and Zip website files
$filesToZip = Get-ChildItem $inputWebsDir -Attributes D #Grabs all the folders for websites that need backed up.
#Loops through each folder to be zipped
foreach($f in $filesToZip) {
$zipArgs = "a", "-tzip",($outputWebsDir + $f.Name + "" + $f.Name + "_" + $currentDateStamp + ".zip"),($inputWebsDir + $f.Name + "*") #Sets arguments for 7zip. Includes creating a sub directory for each website.
& $sevenZip $zipArgs #Zips folder #You would think that an ampersand (&) would join something but in powershell it executes a command.

#Backup SQL Databases
Invoke-Sqlcmd -ServerInstance "." -inputfile ($scriptPath + "BackupSQL_Script.sql") #Runs SQL script to create .bak files for all databases.
$filesToZip = Get-ChildItem ($outputDBDir + "*.bak") -Attributes !D #Gets a list of all the .bak files that need zipped.
#Loop through each file in order to zip it up
foreach($f in $filesToZip){
$zipArgs = "a", "-tzip",($outputDBDir + $f.Name + ".zip"),($outputDBDir + $f.Name) #Arguments to pass to 7zip
& $sevenZip $zipArgs #Zips .bak file
Remove-Item $f.PSPath #Removes .bak file

Interested in the BackupSQL_Script.sql file? You can checkout the gist here.

Antonio Chagoury

Hi, I'm Antonio, Founder and CEO of Maxiom Technology (formerly Inspector IT).I'm a technology executive and entrepreneur who has achieved consistent success in driving growth, generating revenue, and enhancing value in domestic and international markets through technology product innovations.