Create a Smart Backup of your Data using PowerShell

This script map a network drive first then create a folder by current date, then copy the data and than it create a log file with the file and foldername and then it send all the details to the mentioned emai id.

SCRIPT:

#System Variable for backup Procedure

$date = Get-Date -Format d.MMMM.yyyy
New-PSDrive -Name “Backup” -PSProvider Filesystem -Root “\\T_Server\Tally”
$source = “D:\Tally\Data\”
$destination = “backup:\$date”
$path = test-Path $destination

#Email Variables

$smtp = “Exchange-server”
$from = “Tally Backup <tally.backup@xyz.com>”
$to = “Aman Dhally <amandhally@gmail.com>”
$body = “Log File of TALLY bacupk is attached, backup happens on of Date: $date”
$subject = “Backup on $date”

# Backup Process started

if ($path -eq $true) {
write-Host “Directory Already exists”
Remove-PSDrive “Backup”
} elseif ($path -eq $false) {
cd backup:\
mkdir $date
copy-Item -Recurse $source -Destination $destination
$backup_log = Dir -Recurse $destination | out-File “$destination\backup_log.txt”
$attachment = “$destination\backup_log.txt”
#Send an Email to User
send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Attachments $attachment -Body $body -BodyAsHtml
write-host “Backup Sucessfull”
cd c:\

Remove-PSDrive “Backup”
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*