Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Use Task Scheduler to run this script daily.
- # If no changes have been made to pfSense you won't see new files in your backup directory
- $ConfigFileSrcDirectory = "/cf/conf/backup/"
- $DaysToRetainBackups = 30
- $ConfigFileFormat = ".xml"
- $BackupDestDirectory = "\\Shared\Router Backups" #Run script as user with write access
- $ComputerName = "10.10.10.1"
- $password = "password" | ConvertTo-SecureString -asPlainText -Force
- $username = "admin"
- $credential = New-Object System.Management.Automation.PSCredential($username,$password)
- # Test if Posh-SSH module is installed
- if (Get-Module -ListAvailable -Name Posh-SSH) {
- Write-Host "Posh-SSH Module exists"
- } else {
- Write-Host "Module does not exist. Downloading."
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #Because GitHub...
- Invoke-Expression (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev")
- }
- # Create SFTP session and automatically accept server's fingerprint
- New-SFTPSession -ComputerName $ComputerName -Credential $Credential -Verbose -AcceptKey
- #Select the two latest config files
- $ConfigFiles = Get-SFTPChildItem -SessionId 0 -Path $ConfigFileSrcDirectory | Where-Object {$_.FullName -like "*$ConfigFileFormat"} | Sort-Object LastWriteTime | Select-Object -Last 2
- # Download latest config files
- foreach ($File in $ConfigFiles)
- {
- Get-SFTPFile -SessionId 0 -RemoteFile $File.FullName -LocalPath $BackupDestDirectory -Verbose -Overwrite
- }
- # Close SFTP session
- Remove-SFTPSession -SessionId 0 -Verbose
- # Delete old config files from backup location only if there are more than 30
- $Backups = Get-ChildItem -Path $BackupDestDirectory -Filter "*$ConfigFileFormat"
- if ($Backups.Count -gt 30)
- {
- $Backups | where {$_.LastWriteTime -lt (Get-Date).AddDays(-$DaysToRetainBackups)} | Remove-Item -Verbose
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement