Automated backups for VMWare ESXi
Setting up automated backups for ESXi can be a little tricky. After quite a bit of searching I finally came up with these steps that worked for me. I don’t take credit for any of these. I’m just providing you with the steps that I followed and the posts that helped me.
DISCLAIMER: There are some unsupported steps here. Proceed at your own risk!
Step 1: Enable ssh access on your ESXi host. To do that, follow these steps (originally from this post).
NOTE: You actually don’t have to enable ssh for this, I just find it convenient to have remote access to the host machine. If you don’t want to enable ssh, perform steps 1-3 below, then skip on to section 2. When you reach section 3, rather than using ssh, you’ll just execute the commands directly on the host machine at the console.
- From the console on your host (actually on the host machine - not using the Remote Client), press ALT-F1
- Type in the text “unsupported” and press ENTER. Note: the text will not appear as you type.
- Enter the root password
- Enter the command
vi /etc/inetd.conf
- Find the line starting with “#ssh”. You can search by pressing / and then typing ssh and pressing Enter.
- Once you’ve found that line, place the cursor on the # and press x. This should remove the # character.
- Now type
:wq!
and press ENTER to save your changes.
- Next you must restart the inetd process. To do that, you first need the process id. At the console, enter
ps | grep inetd
You should get something that looks like this:
1273 1273 busybox inetd
In this case, 1273 is the process ID. To kill the process enter kill -HUP <process id>. So in this case, you would enter the following command:
kill -HUP 1273
- SSH is now enabled on your ESXi host machine. To test this you’ll need an ssh client. I’m using a mac so I can open terminal and enter the following command:
ssh root@<myhost>
If you’re using windows you may need something like PuTTY or Cygwin.
Step 2: Configure your backup datasource
Next you need to figure out where your backups will be stored. It can be on the ESXi host if you like, but really it should be on a SAN or NAS or something like that. I’m using a Netgear ReadyNAS NV+ and it works great.
- Create a share where your backups will be stored and call it something like “backups”. Make sure that anyone can read/write to this share.
- Open the VMWare Infrastructure Client and connect to your host machine as root.
- Go to the Configuration tab for the host and click Storage.
- Click Add Storage… and create a new datastore pointing to the share you created in step 1. Call this datastore something descriptive… like “backups”
Step 3: Set up your backup script.
I found this great backup script. It lets you specify the VMs you want to backup in a separate text file which makes it really easy to add new VMs to the rotation. It also lets you keep a rotating number of VMs if you don’t want to over-write each backup daily.
- Download the backup script.
- Copy it to the same backup share you created in the last section. You can create a directory called _scripts to keep it separate from the actual backups.
- Open the script and customize the variables at the top. In my case, all I had to modify was the VM_BACKUP_VOLUME variable. If you named your datastore “backups” you can edit this line to look like this:
VM_BACKUP_VOLUME=/vmfs/volumes/backups
- Create a new text file in the same directory as the backup script called VMs.txt. In this file enter the name of each VM you want to back up. Each VM name should be on a new line. Your file should look something like this:
VM1 VM2 A_THIRD_VM
- Connect to your host using ssh i.e. ssh root@<myhost>
- Enter this command:
vi /var/spool/cron/crontabs/root
- Move the cursor to the very end of the file. Press a. This will enter edit mode.
- Press Enter to create a new line and then add the following text:
02 02 * * * sh /vmfs/volumes/backups/_scripts/ghettoVCB.sh /vmfs/volumes/backups/_scripts/VMs.txt
This will run the backup script at 2:02 AM every day. You can change the frequency or time of day if you like. This crontab reference is helpful.
- At this point your entire file should look something like this:
#syntax : minute hour day month dayofweek command 01 01 * * * /sbin/tmpwatch.sh 01 * * * * /sbin/auto-backup.sh #first minute of every hour (run every hour) 00,10,20,30,40,50 * * * * /sbin/decodeSel.sh #Every 10 minutes, translate the latest IPMI SEL data 02 02 * * * sh /vmfs/volumes/backups/_scripts/ghettoVCB.sh /vmfs/volumes/backups/_scripts/VMs.txt
If you are satisfied, press Escape.
- Type the following command to save the file and exit:
:wq! - The backups are now scheduled
At this point, you now have backups scheduled to run every day at 2:02 AM. You can change which VMs are backed-up by editing the VMs.txt file you created. You could even set up multiple cron jobs with different backup frequencies, and different VMs.txt files to back up different VMs at different times.
Before finishing however, you might want to do a test run. If you are still connected to the host machine using ssh, enter the following command:
sh /vmfs/volumes/backups/_scripts/ghettoVCB.sh /vmfs/volumes/backups/_scripts/VMs.txt
IMPORTANT: This will take a while if you have large VMs. You may want to edit VMs.txt to target a single small VM as a test run. If it works for that, it should work for the rest.
One last thing. Unfortunately, the crontabs/root file isn’t saved after a reboot. In order to avoid adding the scheduled job after each reboot you may have to follow one of the techniques described here.