Skip to content

Instantly share code, notes, and snippets.

@xrisdoc
Last active August 22, 2019 21:48
Show Gist options
  • Save xrisdoc/e6caccec999aa8b1c4785f0a2dead5fd to your computer and use it in GitHub Desktop.
Save xrisdoc/e6caccec999aa8b1c4785f0a2dead5fd to your computer and use it in GitHub Desktop.
PowerShell: Backup Directory
Param(
[Parameter(Mandatory=$True)]
[string]$directory,
[Parameter(Mandatory=$False)]
[string]$backupDirectory = "C:\Backups\",
[Parameter(Mandatory=$False)]
[boolean]$zip = $False,
[Parameter(Mandatory=$False)]
[string]$fileNameOverride = "",
[Parameter(Mandatory=$False)]
[boolean]$appendDateTime = $False,
[Parameter(Mandatory=$False)]
[boolean]$useDatePath = $False
)
# Get the current date/time/
# We will use this for filename generartion and directory path where relevant.
$now = [System.DateTime]::Now
# Ensure that no wildcards are specified within the specifed file.
if($directory.Contains("*"))
{
Write-Host "Specified source directory cannot contain wildcards: $directory" -ForegroundColor Red
exit 1
}
# Ensure that the specified source file actually exists
if(-not(Test-Path $directory -PathType Container))
{
Write-Host "Specified source directory not found: $directory" -ForegroundColor Red
exit 1
}
# Obtain the filename from the path.
# This will be the default name for the file when uploading to Dropbox.
# This may end up being overridden.
$fileName = Split-Path $directory -leaf
# Check if a value for "-fileNameOverride" has been supplied.
# If it has, then we will use this value as the resulting filename when the file is backed up.
if($fileNameOverride.Length -gt 0)
{
$fileName = $fileNameOverride
}
# Check if the current date/time is to be appended to the filename
if($appendDateTime -eq $True)
{
$fileDate = $now.ToString("yyyyMMdd-HHmmssfff")
$fileName = $fileDate + "-" +$fileName
}
# Start building the backup location path
$backupPath = $backupDirectory;
# Check if the relative directory should contain a date structure.
# If it does, append the relevant date folder to the relative directory.
# This will be useful for organising the backups by date, making it easier to locate the relevant backups.
if($useDatePath -eq $True)
{
$backupPath = $backupPath + "\" + $now.ToString("yyyy") + "\" + $now.ToString("MM-MMMM") + "\" + $now.ToString("dd-dddd")
}
# Fix any issues with path separators
$backupPath = $backupPath.Replace("/", "\").Replace("\\", "\")
# Output the information
""
"---------------------------------------------"
"Attempting to backup the specified directory"
"---------------------------------------------"
""
"Directory Source : $directory"
"Backup To : $backupDirectory"
"Backup Path : $backupPath"
"File Name : $fileName"
"Filename Override : $fileNameOverride"
"Append Date? : $appendDateTime"
"Use Date Path? : $useDatePath"
""
"---------------------------------------------"
""
if($zip -eq $True)
{
$SevenZip = "C:\Data\Apps\7zip\7z1700-extra\7za.exe"
if (test-path $SevenZip)
{
$source = $directory
$target = $backupPath + "/" + $fileName + ".7z"
$target = $target.Replace("/", "\").Replace("\\", "\")
"Source : $source"
"Target : $target"
""
set-alias sz $SevenZip
# NOTE:
# We are not using the -r flag as it didn't work as expcected
# https://superuser.com/questions/1194710/how-do-i-disable-7-zip-directory-scan-for-directories-which-are-not-meant-to-be
# 7zip the requested directory
sz a -mx=9 -tzip "$target" "$source"
#Write-Host "sz a -mx=9 -tzip -r $target $source"
Write-Host "7Zip Compression Complete" -ForegroundColor Green
}
}
else
{
$source = $directory
$target = $backupPath + "/" + $fileName
$target = $target.Replace("/", "\").Replace("\\", "\")
"Copy From : $source"
"Copy To : $target"
if(-not(Test-Path $target))
{
Copy-Item -Path $source -Destination $target -Recurse -Force
}
else
{
Write-Host "Copy To location already exsists" -ForegroundColor Red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment