PowerShell Backup from Local folder to FTP location

Here is a script that I wrote for backing up some very important files to an off-site ftp location. It backs up only files that are new OR have changed in the last 24 hours. That number of days could be a parameter based on when the backup last ran, however this is the base code that you can start using right away. Just add it to a Task Schedule that runs one a day.


# ==============================================================================================
#Set the Date/Time
# ==============================================================================================
$BackUpdateTime  = (Get-Date).Year.ToString()
$BackUpdateTime += (Get-Date).Month.ToString()
$BackUpdateTime += (Get-Date).Day.ToString()
$BackUpdateTime += (Get-Date).Hour.ToString()
$BackUpdateTime += (Get-Date).Minute.ToString()
$BackUpdateTime += (Get-Date).Second.ToString()
$today = (Get-Date -Format yyyy-MM-dd)

try {
 $ftp = "ftp://ftp.mysite.ca/"
 $user = "ftpuser"
 $pass = "ftppassword" 
 
 $webclient = New-Object System.Net.WebClient
 $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 

 #we specify the directory where all files that we want to upload 
 $Dir="Y:\LocalDirectory\"
 $LogFile="I:\PowerShell\MyBackup_"+$today+".txt"
 Clear-Host
 #Write-Host $LogFile
 "From:"+$Dir+" (on server01) To:"+$ftp | Out-File $LogFile -Append
 "Start: "+(Get-Date) | Out-File $LogFile -Append

 $files = @(Get-ChildItem -Path  $Dir -Recurse | ?{ !$_.PSIsContainer } |Where-Object { $_.lastwritetime -gt (get-date).AddDays(-1)} | Select-Object -ExpandProperty FullName )
 foreach($item in $files)
 {
     if($item -ne $null)
  {
   $uri = New-Object System.Uri($ftp+$item.Substring(3))
   $webclient.UploadFile($uri, $item)
   #Write-Host (Get-Date)$item
   "$(Get-Date): "+$item | Out-File $LogFile -Append
  }
 }
 $webclient.Dispose()

 "End:"+(Get-Date) | Out-File $LogFile -Append
 
 $msg = new-object Net.Mail.MailMessage
 
 # Edit the From Address as per your environment. 
 $msg.From = "Backup (server01) <my.email@mysite.ca>"

 # Edit the mail address to which the Notification should be sent. 
 $msg.To.Add("my.email@mysite.ca")

 # Subject for the notification email. The + “$today” part will add the date in the subject. 
 $msg.Subject = "Backup was Successful for " + "$today"

 # Body or the notification email. The + “$today” part will add the date in the subject. 
 $msg.Body = "Backup was Successful for " + $today + "`r`n`r`n" 
 
 $att = new-object Net.Mail.Attachment($LogFile)
 $msg.Attachments.Add($att)
 
 # IP address of your SMTP server.
 $smtpServer = "smtp.mysite.ca" 
 $smtp = new-object Net.Mail.SmtpClient($smtpServer) 

 $smtp.Send($msg)
 $msg.Dispose()
}
Catch { 
 $ErrorMessage = $_.Exception.Message 

 # Configure the below parameters as per the above. 
 $msg = new-object Net.Mail.MailMessage
 $msg.From = "Backup (server01) <my.email@mysite.ca>" 
 $msg.To.Add("my.email@mysite.ca") 
 $msg.Subject = "Backup Job failed on " + "$today" 
 $msg.Body = "Job failed on " + "$today and the reason for failure was $ErrorMessage." 
 
 $att = new-object Net.Mail.Attachment($LogFile)
 $msg.Attachments.Add($att)
 
 $smtpServer = "smtp.mysite.ca" 
 $smtp = new-object Net.Mail.SmtpClient($smtpServer) 
 
 $smtp.Send($msg)
 $msg.Dispose()
}

Comments are closed