logo

Premium Support
24/7/365

Premium support customers may submit help-desk tickets using the form below.
Alternatively, the help-desk system can be accessed directly at support.maxiomtech.com. support@maxiomtech.com
info@maxiomtech.com
+1 (703) 942-9420
 

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

#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.

#Variables
$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.

Jonathan Sheely

Sr Software Engineer at InspectorIT. Jonathan is an out of the box thinker who has over 10 years experience building and supporting web application software and infrastructure. Jon specializes in ASP.NET C#, Javascript and CSS but is willing to learn anything that gets the job done.

2 Comments