Automating deployments can save you a great deal of headaches and even trauma in some cases. However I found it disappointing when I actually tried to put the best practice of automating deployments into practice.

I needed to make a backup copy of the production folder and then overwrite the live copy with the tested copy from the QA environment. Much to my dismay, I couldn’t find any publicly available and publicly licensed PowerShell scripts to do this job.

I created the below module to do just that.

#
# BackupAndCopy.psm1
#
function BackupAndCopy
{
[cmdletbinding(SupportsShouldProcess=$True)]
param([string]$source, [string]$destination, [string]$backup)
$backupPath = $("$backup\") + $(Split-Path $destination Leaf) + "_"+ $(get-date Format MMddyyy_HHmmss) + "\"
New-Item ItemType Directory Path $destination$Suffix Force
New-Item ItemType Directory Path $backupPath Force
New-PSDrive Name src PSProvider FileSystem Root $source Credential $credentials
New-PSDrive Name dest PSProvider FileSystem Root $destination$Suffix
New-PSDrive Name backup PSProvider FileSystem Root $backupPath
echo "Backing up $destination to excluding the Web.config"
Copy-Item Path dest:\* Destination backup:\ Recurse Force PassThru Container
echo "Copying files from $source to $destination excluding the Web.config"
Copy-Item Path src:\* Destination dest:\ Exclude @('Web.config') Recurse Force PassThru Container
echo "Delete folders/files older "
$limit = (Get-Date).AddDays(-7)
Get-ChildItem Path $backup Force Directory | Where-Object { $_.CreationTime -lt $limit } | Remove-Item Force Recurse Verbose
}

view raw
BackupAndCopy.psm1
hosted with ❤ by GitHub

Noteworthy Items

  1. Backup folder is datetime stamped in a consistent manner
    1. Makes troubleshooting  simpler
    2. More straight forward rollbacks
  2. Different domains necessitated the use of New-PSDrive. It does still work on the same domain though.
  3. Excluding the web.config to preserve environmental configuration/differences.
  4. Delete backups older than 7 days so your infrastructure team doesn’t have to ask you why the drive is full.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s