website design - website templates - online systems - consultancy
.tech tips
.latest projects
.biz blog

Free Backup Utility With Powershell

Pelco Consulting has written a very basic backup script that utilises the Windows Powershell interface.

For those that are not familiar with Windows Powershell, it is a command line based scripting tool that allows system administrators to write scripts. In a nut shell, Windows Poershell is the modern day version of the old '.bat' files from DOS.

The beauty of this backup utility is that system administrators can use this script to setup various backups of various directories which can then be set to run as a scheduled task via the Windows Task Scheduler.

One particular use in this respect is the automated backup of Outlook '.pst' files. By using the Windows Task Scheduler, you can ensure the shut down of Outlook via a scheduled task 5 minutes prior to the running of the backup of the '.pst' file.

The script for the automated backup contains a series of options and settings for the backup.

This script must be saved as a '.ps1' script and then set to start via Powershell. You must have enabled the processing of local scripts on your machine by setting the execution policy for Powershell scripts.

You can do you by opening a powershell window and changing your execution policy to 'RemoteSigned' as this will allow for the processing of unsigned scripts that your write on your machine. If you require other signing options, you can find more information on the 'Running Scripts From Within Windows Powershell' on the Microsoft Technet Library.

Once your system can run powershell scripts that you write, you can open the Windows Powershell ISE. This will create a blank '.ps1' file that you can use to create your desired script.

The script requires a few variables. Firstly you must set the source folder. This is the folder you want to copy the files from. The next variable is the backup folder. This is the location you want to backup your files to. This may be a network drive or external hard drive. If it is a network drive you can manage advanced settings via the task scheduler to only run when a specific network connection is available. Alternatively, you could ensure the connection is open by writing a script to run before this that opens the network location. The third and final variable is the location of the log file for this backup script. This is required as the script will create a text record of the file it copied and the last time that the script ran.

  1. # Backup Facility Designed to Copy Files that have been updated in the past 25 hours.
  2. # If set to run every 24 hours, all new files from that period will be copied across
  3. # A 1 hour buffer is included for the copy time to the backup period.
  4.  
  5. # Set Backup Period
  6. $BackupPeriod = (Get-Date).AddHours(-25)
  7.  
  8. # Set Folder to Copy From
  9. $MySourceFolder = "C:\Projects"
  10.  
  11. # Set Copy to Copy To
  12. $MyBackupFolder = "Z:\Projects"
  13.  
  14. # Set Directory for Log File
  15. $MyLogsFile = "Z:\Logs\dailyprojectsbackup.txt"
  16.  
  17. ###############################################
  18. # Get List
  19. Get-ChildItem $MySourceFolder\* -recurse |
  20.  
  21. # Filter by Last Write Time greater than or equal to Backup Period
  22. Where-Object {$_.lastwritetime -ge $BackupPeriod -and $_.PSIsContainer -eq $false} |
  23.  
  24. # Update the Log file to show the files included in this backup
  25. Out-File $MyLogsFile |
  26.  
  27. # Run Copy Process if condition applies.
  28. Copy-Item -destination $MyBackUpFolder
  29.  
  30. #Add timestamp of last run time.
  31. Add-Content $MyLogsFile "Last run at $(Get-Date)" -encoding Unicode

 

For testing purposes, we recommend you create a series of demo folders and run a practice with a few, small files.

The log file will show a listing of files and indicate at the bottom of the file what time the backup last ran. If you do not want to use a log file simply comment out both line 25 and line 31.

A few important items to note are that the first search of files makes use of '-recurse' [Line 19]. This will search that directory and all directories and files inside that directory. If you only want to backup new files in a single directory, you can remove the '-recurse'.

Download Script
Banner
© Copyright 2010 - 2012 Pelco Consulting.