NSX-v backup retention script

The NSX-v manager is not being backed up via snapshot-based backups. This isn’t officially supported. Therefore a file/configuration based backup method used to backup the configuration of your NSX manager periodically.

Unfortunately, there is no retention setting available in the NSX manager so if you are not careful the backup directory will grow in a rapid fashion and fill up your disk very fast. Via this easy script I’ve written, you can enable backup retention for your NSX-v backup files.

What does the script do or contain?

When an NSX backup is made, one backup contains three files. The backup file itself with all the data, a <backup-filename>.backupproperties and  a <backup-filename>.checksum file. These files are all being written to the backup directory you have provided in the NSX manager. I was trying to achieve having the last 30 backups. Therefore, I needed to make sure that the backup directory does not contain more than 90 files, and when it does, it needs to delete all the oldest files until there are 90 files in the directory again.

The script looks like this:

$nsxbackupdirectory = 'L:\ftp\NSX-BACKUP\_nsx-manager01'
$amountofnsxbackupfiles = (Get-ChildItem $nsxbackupdirectory | measure).Count;


if (($amountofnsxbackupfiles) -gt 90) 
{ write-host "There are more than 90 files in the backup directory, retention cleanup will run until there are 90 files in the directory" -ForegroundColor Yellow 
Do 
{ write-host Deleting backup files from NSX Backup Directory..
Get-ChildItem $nsxbackupdirectory | Sort CreationTime | Select -First 3 | Remove-Item 
$amountofnsxbackupfiles = (Get-ChildItem $nsxbackupdirectory | measure).Count;
} until ($amountofnsxbackupfiles -lt 91)
} else {
write-host "There are 90 files or less in the directory, no retention cleanup will occur" -ForegroundColor Green
}

I know there probably will be already a ton of other posts with retention scripts for NSX manager backups. But most of them are based on retention by days and not by the number of backups.

**NOTE** 
Do not forget to schedule the script via Task Scheduler or an equivalent application to run the script on a daily basis. Or whatever you wish. I scheduled the task to run every 12 hours. But once a day will be also sufficient. 

Feel free to adjust the script to your own needs. If you have any questions or remarks, please let me know!

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s