Advertisement
shalafi71

pfSense Backup

Jul 24th, 2018
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Use Task Scheduler to run this script daily.  
  2.  
  3. # If no changes have been made to pfSense you won't see new files in your backup directory
  4.  
  5. $ConfigFileSrcDirectory = "/cf/conf/backup/"
  6. $DaysToRetainBackups = 30
  7. $ConfigFileFormat = ".xml"
  8. $BackupDestDirectory = "\\Shared\Router Backups" #Run script as user with write access
  9. $ComputerName = "10.10.10.1"
  10. $password = "password" | ConvertTo-SecureString -asPlainText -Force
  11. $username = "admin"
  12. $credential = New-Object System.Management.Automation.PSCredential($username,$password)
  13.  
  14. # Test if Posh-SSH module is installed
  15.  
  16. if (Get-Module -ListAvailable -Name Posh-SSH) {
  17.     Write-Host "Posh-SSH Module exists"
  18. } else {
  19.     Write-Host "Module does not exist.  Downloading."
  20.     [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #Because GitHub...
  21.     Invoke-Expression (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev")
  22. }
  23.  
  24. # Create SFTP session and automatically accept server's fingerprint
  25.  
  26. New-SFTPSession -ComputerName $ComputerName -Credential $Credential -Verbose -AcceptKey
  27.  
  28. #Select the two latest config files
  29.  
  30. $ConfigFiles = Get-SFTPChildItem -SessionId 0 -Path $ConfigFileSrcDirectory | Where-Object {$_.FullName -like "*$ConfigFileFormat"} | Sort-Object LastWriteTime | Select-Object -Last 2
  31.  
  32. # Download latest config files
  33.  
  34. foreach ($File in $ConfigFiles)
  35. {
  36.     Get-SFTPFile -SessionId 0 -RemoteFile $File.FullName -LocalPath $BackupDestDirectory -Verbose -Overwrite
  37. }
  38.  
  39. # Close SFTP session
  40.  
  41. Remove-SFTPSession -SessionId 0 -Verbose
  42.  
  43. # Delete old config files from backup location only if there are more than 30
  44.  
  45. $Backups = Get-ChildItem -Path $BackupDestDirectory -Filter "*$ConfigFileFormat"
  46. if ($Backups.Count -gt 30)
  47. {
  48.     $Backups | where {$_.LastWriteTime -lt (Get-Date).AddDays(-$DaysToRetainBackups)} | Remove-Item -Verbose
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement