Scheduling ESXi 6.x configuration backups

The other day I was reading a healthy debate between fellow bloggers on LinkedIn, And it's true, few of us think of programming certain curious tasks such as this one. While it's interesting to back up our ESXi setup… Why don't we schedule it? I leave you a couple of options that will surely fit you to have updated backups of the ESXi configuration.

 

Scheduling a copy job from Windows,

This is perhaps the simplest option and the one that best suits, since simply having VMware vSphere CLI installed on a Windows computer (could be the same vCenter Server) We'd have it ready. We should create a BAT file that will contain the command that exports from each ESXi its config, and save in the configuration file wherever we want .

 

Backup-ESXi-Scheduled-Configuration-01-Bujarra

This would be an example batch file, In my case it's called 'backup_esxi.cmd’ to which I will have created a scheduled task in the Windows Task Scheduler indicating that I run this script for example once a week, and as we can see, it saves us in the path that interests us the configuration file with the name and format that interests us. Obviously, we will add as many lines to the BAT as we want servers, In addition, that we should create a user on the ESXi with read-only privileges or indicate another authentication method, in addition to protecting these files from curious people!

[SourceCode]"%ProgramFiles(x86)%\VMwareVMware vSphere CLIbinvicfg-cfgbackup.pl" –server=DIRECCION_IP_ESXi –username=USER –password=PASSWORD -s "PATHFILE.tgz"[/SourceCode]

 

Backup-ESXi-Scheduled-Configuration-02-Bujarra

In the example, you'll see that I add the date to the end of the configuration name, Maybe something interesting if we have a lot of servers & Copy History. Once we run the BAT manually or it is executed on a scheduled basis, it will create the configuration files for each ESXi! Now… We'll put these backups in good place!

 

Scheduling a copy task from ESXi,

And this option is perhaps cuter since we don't have to have a Windows pending to make the copies, otherwise, from each ESXi we will configure it to export its configuration by cron on a scheduled basis to a VMFS Datastore.

What this method has is a little more labor, since by default we will not be able to work with cron as we are used to, in addition to the fact that when the ESXi is restarted, the changes that we will have made by default are not maintained, therefore, first we will create the script in the ESXi that exports the configuration to us and then we will modify the ESXi so that every time it is restarted it keeps the cron configuration & ready!

 

Backup-ESXi-Scheduled-Configuration-03-Bujarra

We connect via SSH to an ESXi and create the configuration file, for example via 'vi /vmfs/volumes/DATASTORE/Backup_ESXi/backup_esxi.sh', with the following content:

[SourceCode]vim-cmd hostsvc/firmware/sync_config
vim-cmd hostsvc/firmware/backup_config
find /scratch/downloads/ -name *.tgz -exec mv {} /vmfs/volumes/DATASTORE/Backup_ESXi/FILE.tgz \;
[/SourceCode]

As in the previous case, you will see that in the photo, I am calling the TGZ file as the ESXi server in addition to putting the date at the end, As I tell you, I think it will be more than necessary. Let's not forget to make the script executable! With 'chmod +x /PATH/NOMBRE_SCRIPT'.

 

 

Backup-ESXi-Scheduled-Configuration-04-Bujarra

And nothing, If we run it manually, we see how it generates the unique configuration file in a datastore, we can put this task to run in each ESXi and in the same folder of the VMFS datastore it will fill us’ with the configurations of each ESXi!

 

 

Backup-ESXi-Scheduled-Configuration-05-Bujarra

GOOD, Now the question of programming something with the ESXi 6.x cron, The first thing we'll do is give us write permissions in the cron, in addition to adding the programming with which we want to execute the script, in my case, every day at 10pm; The only thing, remember that ESXi use UTC time, so running 'date’ on the command line to check the 'correct' time’ of execution.

[SourceCode]chmod +w /var/spool/cron/crontabs/root
vi /var/spool/cron/crontabs/root (and add at the end):
0 22 * * * /vmfs/volumes/DATASTORE/FOLDER/SCRIPT [/SourceCode]

 

 

Backup-ESXi-Scheduled-Configuration-06-Bujarra

Now we must 'reset'’ cron, the way we have in ESXi 6.0 or 6.5 it would be by looking at the process ID, Killing him and ripping Cron out again:

[SourceCode]cat /var/run/crond.pid
kill IDENTIFIER
crond[/SourceCode]

 

Backup-ESXi-Scheduled-Configuration-07-Bujarra

The next thing will be to edit '/etc/rc.local.d/local.sh’ to allow the ESXi to boot up once (in case of stoppage) Add the content back to the cron, Previously we will stop Cron and in the end it starts again! So just before the end of the file, Before 'exit 0'’ we must add:

[SourceCode]/bin/kill $(cat /var/run/crond.pid)
/BIN/Echo ‘0 22 * * * /vmfs/volumes/DATASTORE/FOLDER/SCRIPT’ >> /var/spool/cron/crontabs/root
/usr/lib/vmware/busybox/bin/busybox crond[/SourceCode]

 

Backup-ESXi-Scheduled-Configuration-08-Bujarra

We run 'autobackup.sh’ Once we have saved all the files and it would be ready for sentencing! In this way we will have some ESXi that will programmatically export their configuration to a VMFS Datastore that we have shared, If necessary, we can always choose and recover a host from there, What sometimes happens! And if not, we have the previous example that Windows would be in charge of making the copy and leaving the configuration files on the network. I hope it helps you and we have defined at least some backups of the config of our VMware ESXi hosts!

 

Recommended Posts

Author

nheobug@bujarra.com
Autor del blog Bujarra.com Cualquier necesidad que tengas, Do not hesitate to contact me, I will try to help you whenever I can, Sharing is living ;) . Enjoy documents!!!