Tuesday, November 29, 2011

“Smart Backup” using PowerShell


Hi,
As a Network Admin I always worry about my backups. I was planning to write a simple script which create a new folder by date and copy the desired files and folder in to it recursively.
so i successfully  able to write a script for it and sharing it with you and I hope it is useful to you too. {or at least for tally administrators they always worried about there data, or we can say they this article contain “How to Automate Tally Backup”
I am running this script on my Tally server:
Script Logic:
  1. Map a Network Drive where you want to save the backup
  2. Create a Folder by using Date as Name
  3. Copy the backup
  4. Create a log file name as “backup_log.txt”
  5. When all some send a email to admin ($to) with backup_log.txt as attachment.
  6. Remove the Map Drive
you can download the script from here : http://dl.dropbox.com/u/17858935/Smart_Backup_Create%20folders_by_Date.zip

Note : change \\T_server\Tally with your folder where you want to take backup.
          Change $source with your source folder which you want to backedup

Script:
1: #System Variable for backup Procedure
2:  $date = Get-Date -Format d.MMMM.yyyy
3:  New-PSDrive -Name "Backup" -PSProvider Filesystem -Root "\\T_Server\Tally"
4:  $source = "D:\Tally\Data\"
5:  $destination = "backup:\$date"
6:  $path = test-Path $destination
7: #Email Variables
8:  $smtp = "Exchange-server"
9:  $from = "Tally Backup <tally.backup@xyz.com>"
10:  $to = "Aman Dhally <amandhally@gmail.com>"
11:  $body = "Log File of TALLY bacupk is attached, backup happens on of Date: $date"
12:  $subject = "Backup on $date" 
13: # Backup Process started
14:  if ($path -eq $true) {
15:     write-Host "Directory Already exists"
16:     Remove-PSDrive "Backup"  
17:     } elseif ($path -eq $false) {
18:             cd backup:\
19:             mkdir $date
20:             copy-Item  -Recurse $source -Destination $destination
21:             $backup_log = Dir -Recurse $destination | out-File "$destination\backup_log.txt"
22:             $attachment = "$destination\backup_log.txt"
23: #Send an Email to User 
24:             send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Attachments $attachment -Body $body -BodyAsHtml
25:             write-host "Backup Sucessfull"
26:             cd c:\ 
27:  Remove-PSDrive "Backup"  
28:  }
I hope it may be useful to someone :) 
Thanks 
Aman Dhally

1 comment:

  1. Any suggestions on how to use this script if the drive is already mapped?

    ReplyDelete