Powershell Backup Script

Needed a backup script which i can run from the powershell or with use of the Windows schedular.

I’ve created some checks on folders and succesrate. The scripts also can email you when backup is succesful/failed.

If you have any suggestions please let me know!

##################################### Backup Script ###############################################
## Name: Backup_Script.ps1                               
## Creator: Erwin Bierens                     
## CreationDate: 10-02-2017                               
## LastModified: 10-02-2017                                
## Version: 1.0
## Website: https://erwinbierens.com/powershell-backup-script
## 
## Description: Copies the Backupdir to the Destination folder
## Deviations will be written to logfile
## Only Change Variables in Variables Section  
##
## Thx to the followingg scripts i created this one, source information
## http://www.krist-online.nl/index.php/scripts/14-robocopy-powershell-wekelijks-backup-schema
## https://blog.sat.iit.edu/2011/11/backup-script-using-robocopy-and-powershell/
## 
##
## Example: powershell -command "& 'c:\temp\Backup_Script.ps1' "
## 
####################################################################################################

####################
##Variables Section
####################

## Source directory
$source="C:\Temp\Backup\A"
 
## Destination Directory
## you can also use the network path "\\server\share"
$destination="C:\Temp\Backup\B\"
 
## Destination Log File
$logfile="c:\temp\backup\logbestand.txt"
 
## BackupRapport is being used to check if backup is succesfull
$backupReport="c:\temp\logfile_backup.txt"
 
## Make changes for email reports
$EmailFrom="backupscript@bierens.it"
$EmailTo="erwin@bierens.it"
$SMTPServer="192.168.178.32"

## Email Subjects and Body info
$successEmailSubject="Backup Succesfull"
$successEmailBody="The Backup is succesfull"
$failEmailSubject="Backup Failed"
$failEmailBody="The backup is failed, please check the attached log file"

## =========================================================
## =========== DONT CHANGE PARAMETERS BELOW ================
## =========================================================

## Check if destination exists, if no create folder
if (!(Test-Path -path $destination))
{
New-Item $destination -type directory
}
 
## robocopy command
## /XO Excludes older files.
## /COPY:DATSO Specifies the file properties to be copied. D Data, A Attributes, T Time stamps, S NTFS access control list (ACL), O Owner information
## /E /Purge Mirrors a directory tree
## /R:10 Specifies the number of retries on failed copies. The default value of N is 1,000,000 (one million retries)
## /log: Writes the status output to the log file (overwrites the existing log file). use /log+: for appending 
## /NP Specifies that the progress of the copying operation (the number of files or directories copied so far) will not be displayed.
robocopy $source $destination *.* /XO /COPY:DATSO /E /purge /R:10 /log:$logfile /NP
 
## Check variables to compare source and destination
$check_source = Dir $source
$check_destination = Dir $destination
 
## Compare source and destination, write deviations to backup report 
Compare-Object $check_source $check_destination -PassThru > $backupReport
 
## Create and send email after succesfull backup
function sendSuccessEmail{
send-mailmessage -from $EmailFrom -to $EmailTo -subject $successEmailSubject -body $successEmailBody -Attachments "$logfile" -priority High -dno onSuccess, onFailure -smtpServer $SMTPServer
}
 
## Create and send email after unsuccesfull backup
function sendFailEmail{
send-mailmessage -from $EmailFrom -to $EmailTo -subject $failEmailSubject -body $failEmailBody -Attachments "$logfile" -priority High -dno onSuccess, onFailure -smtpServer $SMTPServer
}
 
## If backupreport is empty, backup is succesfull send email
$File = Get-ChildItem $backupReport
if ($File.Length -eq 0) {sendSuccessEmail}
 
## If backup report is not empty, something is going wrong. Email will be send
$File = Get-ChildItem $backupReport
if ($File.Length -gt 0) {sendFailEmail}
#EOF

Challenge yourself to ‘Inbox Zero’

Inbox Zero

Do you also recognize a overloading email inbox, to much unreaded emails and no time to clean up, ‘Inbox Zero’ may be the solution for your problem!

Yesterday i was reading a blog (sorry, it’s in Dutch) from Michel Bouman from Microsoft. Last year he wrote a blog about how he implemented ‘Inbox Zero’ method by Merlin Mann in his life. In short terms, you have 4 folders(categories):

  • Action
  • Archive
  • Sometime
  • Waiting For

Every email you receive you will validate agains the rules below:

– Delete or Archive emails with no action requirered.
– Reply emails with action request, only if possible in two minutes.
– Emails above two minutes move to Action folder.
– Emails with action who you can delegate, you’ll forward inmediately, add your email to BCC to track. (explaination setting this up later in this blog)
– Interesting emails without action, containing a lot of text you can move to the folder  ‘Sometime’. This kind of emails you read in the morning with your first coffee or at the end of the day.

Now that you have categorized your email you’ll have to block some time in your agenda to process the Action emails.

There are many thing that i want to improve in my life, efficiency is one of them (ongoing process)! I spend a lot of my time everyday with chasing, reading and answering emails (Work and Private), This blog was opening my eyes, it’s time to tackle this issue.

Let’s give it a try and Challenge yourself.

Starting the Challenge to ‘Inbox Zero’

Continue Reading