Skip to content

Instantly share code, notes, and snippets.

@ryanvgates
Last active September 10, 2016 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanvgates/af08106279e83add8cc6f7e7c65ba064 to your computer and use it in GitHub Desktop.
Save ryanvgates/af08106279e83add8cc6f7e7c65ba064 to your computer and use it in GitHub Desktop.
Powershell Backup And Copy
#
# BackupAndCopy.psm1
#
function BackupAndCopy
{
[cmdletbinding(SupportsShouldProcess=$True)]
param([string]$source, [string]$destination, [string]$backup)
$backupPath = $("$backup\") + $(Split-Path $destination -Leaf) + "_"+ $(get-date -Format MM-dd-yyy_HH-mm-ss) + "\"
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment