Skip to content

Instantly share code, notes, and snippets.

@amhendley
Last active August 29, 2015 14:22
Show Gist options
  • Save amhendley/5d64da081bcc5cd3bc76 to your computer and use it in GitHub Desktop.
Save amhendley/5d64da081bcc5cd3bc76 to your computer and use it in GitHub Desktop.
Backs up a source folder path to a given destination folder with some logic to define time stamped folders and preserve file system space by recycling older back-up folders where the -AppendDateTime argument is used.
# SimpleBackup.ps1
# Written by A Hendley ([email protected])
#
# A simple back-up of a source folder path to a given destination folder path.
<#
.SYNOPSIS
Backs up a source folder path to a given destination folder with some logic to
define time stamped folders and preserve file system space by recycling older
back-up folders where the -AppendDateTime argument is used.
.DESCRIPTION
Backs up a source folder path to a given destination folder with some logic to
define time stamped folders and preserve file system space by recycling older
back-up folders where the -AppendDateTime argument is used.
To protect the integrity of a past back-up or destination folder, the command
will not overwrite the contents and throw an error. To ensure this does not
happen, use the -AppendDateTime argument.
.PARAMETER Path
Specifies the source path to be backed up.
.PARAMETER Destination
Specifies the destination path to back-up too.
.PARAMETER AppendDateTime
Specifies whether to append a date and time folder to the end of the
destination path.
.PARAMETER Recycle
Specifies if back-up folders are to be recycled when used with -AppendDateTime.
Helps preserve disk space.
.PARAMETER RecycleCount
Specifies how many back-ups to keep when used with -AppendDateTime and
-Recycle. Default value is 2.
.EXAMPLE
C:\>SimpleBackup.ps1 -Path "C:\My\Source\Folder" -Destination "C:\My\Backups" -AppendDateTime -Recycle -RecycleCount 2
This command will backup the source folder to a new folder with a date and time
format of (yyyyMMdd_HHmmss) under the destination folder. The command will also
remove any previous back-up folders while retaining the current and previous
back-up.
.EXAMPLE
C:\>SimpleBackup.ps1 -Path "C:\My\Source\Folder" -Destination "C:\My\Backups"
This command will back-up the source folder directly into the specified
destination folder.
#>
[CmdletBinding(DefaultParameterSetName="Path", SupportsShouldProcess=$false)]
param (
[Parameter(Position=0, Mandatory=$true, HelpMessage="Specifies the source path to be backed up.")]
[string] $Path,
[Parameter(Position=1, Mandatory=$true, HelpMessage="Specifies the destination path to back-up too.")]
[string] $Destination,
[Parameter(Position=2, Mandatory=$false, HelpMessage="Specifies whether to append a date and time folder to the end of the destination path.")]
[switch] $AppendDateTime,
[Parameter(Position=3, Mandatory=$false, HelpMessage="Specifies if back-up folders are to be recycled when used with -AppendDateTime. Helps preserve disk space.")]
[switch] $Recycle,
[Parameter(Position=4, Mandatory=$false, HelpMessage="Specifies how many back-ups to keep when used with -AppendDateTime and -Recycle. Default value is 2.")]
[int] $RecycleCount = 2
)
begin {
}
process {
if ($AppendDateTime) {
$datetime_string = get-date -format "yyyyMMdd_HHmmss"
$finalDestination = [System.IO.Path]::Combine($Destination, $datetime_string)
} else {
$finalDestination = $Destination
}
if (Test-Path $finalDestination) {
throw "The destination path ($finalDestination) already exists which is not allowed."
}
Copy-Item -Path $Path -Destination $finalDestination -Recurse -Force
write-host "Path - $Path"
write-host " > Backed up to - $finalDestination"
if ($AppendDateTime -And $Recycle) {
$folder_count = (dir $Destination | where {$_.PsIsContainer} | Measure-Object Name).Count
if ($folder_count -gt $RecycleCount) {
$remove_count = ($folder_count - $RecycleCount)
$folders_to_remove = (dir $Destination | where {$_.PsIsContainer} | sort-object FullName | select FullName -First $remove_count | foreach {$_.FullName})
ForEach ($f in $folders_to_remove -Split "`n") {
$folder = $f.Replace("`r", "")
write-host "Removing old back-up folder - $folder"
Remove-Item -Path $folder -Force -Recurse
}
}
}
}
end {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment