Created
October 3, 2024 12:25
-
-
Save r0ckbeard/1f479d021737ba946c45135420c38d04 to your computer and use it in GitHub Desktop.
This script serves as a template for creating backup procedures. It provides a framework for logging, error handling, file compression, and email notifications. Customize the script by adding your specific backup logic in the designated areas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Generic Backup Script Template. | |
| .DESCRIPTION | |
| This script serves as a template for creating backup procedures. It provides a framework for logging, | |
| error handling, file compression, and email notifications. Customize the script by adding your specific | |
| backup logic in the designated areas. | |
| .PARAMETER LogFolder | |
| The directory where log files will be stored. If not specified, defaults to "C:\Logs\Backup". | |
| .PARAMETER BackupFolder | |
| The directory where backup files will be stored. If not specified, defaults to "C:\Backup". | |
| .PARAMETER SmtpServer | |
| The SMTP server to use for sending error emails. If not specified, defaults to "smtp.your.company". | |
| .PARAMETER EmailFrom | |
| The email address to use as the sender for error emails. If not specified, defaults to "backup@your.company". | |
| .PARAMETER EmailTo | |
| The email address to send error notifications to. If not specified, defaults to "admin@your.company". | |
| .EXAMPLE | |
| .\BackupScript.ps1 -LogFolder "D:\CustomLogs" -BackupFolder "E:\Backups" -SmtpServer "smtp.mycompany.com" -EmailFrom "backups@mycompany.com" -EmailTo "admin@mycompany.com" | |
| .NOTES | |
| File Name : Backup-Template.ps1 | |
| Author : Sebastian Rock | |
| Prerequisite : PowerShell V3 or later | |
| Date : 14/06/2024 | |
| #> | |
| [CmdletBinding()] | |
| param ( | |
| [string]$LogFolder = "C:\Logs\Backup", | |
| [string]$BackupFolder = "C:\Backup", | |
| [string]$SmtpServer = "smtp.your.company", | |
| [string]$EmailFrom = "backup@your.company", | |
| [string]$EmailTo = "admin@your.company", | |
| [string]$Subject = "Backup Script Failed" | |
| ) | |
| # Start transcript logging | |
| $TranscriptPath = Join-Path -Path $LogFolder -ChildPath "Backup_Transcript_$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').txt" | |
| Start-Transcript -Path $TranscriptPath -Force | |
| # Ensure the log and backup folders exist | |
| @($LogFolder, $BackupFolder) | ForEach-Object { | |
| if (-not (Test-Path -Path $_)) { | |
| New-Item -Path $_ -ItemType Directory -Force | Out-Null | |
| Write-Verbose "Created directory: $_" | |
| } | |
| } | |
| # Generate a unique log file name | |
| $LogFileName = "Backup_$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').log" | |
| $Global:LogFilePath = Join-Path -Path $LogFolder -ChildPath $LogFileName | |
| Function Log-Message { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory=$true, Position=0)] | |
| [string]$Message, | |
| [Parameter(Mandatory=$false)] | |
| [ValidateSet('Info', 'Warning', 'Error')] | |
| [string]$Severity = 'Info' | |
| ) | |
| $TimeStamp = Get-Date -Format "yyyy/MM/dd HH:mm:ss:fff" | |
| $Line = "$TimeStamp - [$Severity] $Message" | |
| Add-Content -Path $Global:LogFilePath -Value $Line | |
| switch ($Severity) { | |
| 'Info' { Write-Verbose $Line } | |
| 'Warning' { Write-Warning $Line } | |
| 'Error' { Write-Error $Line } | |
| } | |
| } | |
| Function Send-ErrorEmail { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory=$true)] | |
| [string]$LogFilePath | |
| ) | |
| $Body = @" | |
| The backup script encountered an error. Please find the log file attached for more details. | |
| Actions to take: | |
| 1. Review the log file | |
| 2. Troubleshoot the issue | |
| 3. Fix the problem | |
| 4. Run a manual test | |
| "@ | |
| try { | |
| Send-MailMessage -SmtpServer $SmtpServer -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -Attachments $LogFilePath -ErrorAction Stop | |
| Log-Message "Error email sent to $EmailTo." | |
| } catch { | |
| Log-Message "Failed to send error email: $_" -Severity Warning | |
| } | |
| } | |
| # Log start of script and parameters | |
| Log-Message "Script started with parameters: LogFolder=$LogFolder, BackupFolder=$BackupFolder, SmtpServer=$SmtpServer, EmailFrom=$EmailFrom, EmailTo=$EmailTo" | |
| # Main try-catch block for the script | |
| try { | |
| $StartTime = Get-Date | |
| # Your main backup logic goes here | |
| # Example: | |
| # 1. Prepare the data to be backed up | |
| # Log-Message "Preparing data for backup..." | |
| # 2. Perform the backup operation | |
| # Log-Message "Performing backup operation..." | |
| # 3. Verify the backup | |
| # Log-Message "Verifying backup integrity..." | |
| # Compress the backed-up files | |
| $ZipFileName = "Backup_$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').zip" | |
| $ZipFilePath = Join-Path -Path $BackupFolder -ChildPath $ZipFileName | |
| try { | |
| Compress-Archive -Path "$BackupFolder\*" -DestinationPath $ZipFilePath -Force | |
| Log-Message "Successfully compressed backup files to $ZipFilePath." | |
| } catch { | |
| throw "Failed to compress backup files: $_" | |
| } | |
| # Cleanup temporary files if necessary | |
| # Get-ChildItem $BackupFolder -File | Where-Object { $_.Name -ne $ZipFileName } | Remove-Item -Force | |
| # Log-Message "Temporary files removed." | |
| $EndTime = Get-Date | |
| $ElapsedTime = ($EndTime - $StartTime).TotalSeconds | |
| Log-Message "Script completed successfully. Execution Time: $ElapsedTime Seconds" | |
| } catch { | |
| Log-Message "Script encountered a fatal error: $_" -Severity Error | |
| Send-ErrorEmail -LogFilePath $Global:LogFilePath | |
| throw | |
| } finally { | |
| Stop-Transcript | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment