Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Zip Backup
- <#
- Creates a Zip file of a given folder in another location, with the date as the name.
- Parameters:
- SourceLocation - Location of source files (no trailing slash required)
- DestinationLocation - Location of destination (folder - no trailing slash required)
- #>
- $SourceLocation="\\server33\resources\forge"
- $DestinationLocation="e:\backup\forge"
- # Get date and time
- $TodaysDate=get-date -UFormat "%Y%m%d"
- $TodaysTime=get-date -UFormat "%H%M"
- # Set random directory to the users temp directory plus a random number
- $RandomDirectoryName=get-random
- $TemporaryLocation=$env:TEMP+"\"+$RandomDirectoryName
- # Prepare the Destination file name and path
- $DestinationFile=$DestinationLocation+"\"+$TodaysDate+$TodaysTime+".zip"
- # if the temporary path already exists / exit with error
- if (!(Test-Path -path $TemporaryLocation))
- {
- # Create the temporary location
- new-item $TemporaryLocation -type directory
- # Copy the source to the destination
- $CopyResults=copy-item $SourceLocation -destination $TemporaryLocation -recurse -passthru
- if ($CopyResults)
- {
- # Compress the file
- Add-Type -assembly System.IO.Compression.FileSystem
- $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
- [system.io.compression.zipfile]::CreateFromDirectory($TemporaryLocation,$DestinationFile,$CompressionLevel,$false)
- # Delete the temporary location
- remove-item $TemporaryLocation -Force -recurse
- }
- else
- {
- # if the zip fails, produce error
- write-host -BackgroundColor white -ForegroundColor red "Failed creating ZIP file"
- }
- }
- else
- {
- write-host -BackgroundColor white -ForegroundColor Red "Failed - temporary folder already exists. Try again"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement