ESXi - ghettoVCB

From The TinkerNet Wiki
Jump to navigation Jump to search

ghettoVCB on GitHub

Installation

Install ghettoVCB

  1. SSH (as root) into the machine
  2. Create a Utilities folder on an existing datastore
    • mkdir "/vmfs/volumes/Admin/Utilities/"
    • (This exact location is specific to my testbed server...)
    • Remember this location... It'll be very useful...
  3. switch to this folder
    • cd "/vmfs/volumes/Admin/Utilities/"
  4. download ghettoVCB
    • wget https://github.com/lamw/ghettoVCB/archive/master.zip
      • (wget doesn't seem to like https...)
    • wget http://web.tinkernow.net/otherfiles/ghettoVCB.zip
      • (inside my local network...)
    • wget http://tinker.farted.net/otherfiles/ghettoVCB.zip
      • (a copy accessible to wget without https)
  5. Unpack & rename ghettoVCB
    • unzip ghettoVCB.zip
    • mv ghettoVCB-master ghettoVCB
  6. switch to the new folder
    • cd ghettoVCB
  7. Install it!
    • esxcli software vib install -v "/vmfs/volumes/Admin/Utilities/ghettoVCB/vghetto-ghettoVCB.vib" -f

Have ya noticed the usefulness of remembering "/vmfs/volumes/Alpha/Utility_Room/" yet?

Create a Backup folder on the datastore

Not needed if your datastores are set up in the "standard" way...

mkdir "/vmfs/volumes/Backups"

ghettoVCB will create subfolders for each VM you backup inside this folder.

Configuration

vi ghettoVCB.conf

VM_BACKUP_VOLUME="/vmfs/volumes/Beta/BACKUPS"
DISK_BACKUP_FORMAT=thin
VM_BACKUP_ROTATION_COUNT=3
POWER_VM_DOWN_BEFORE_BACKUP=0
ENABLE_HARD_POWER_OFF=0
ITER_TO_WAIT_SHUTDOWN=3
POWER_DOWN_TIMEOUT=5
ENABLE_COMPRESSION=0
VM_SNAPSHOT_MEMORY=0
VM_SNAPSHOT_QUIESCE=0
ALLOW_VMS_WITH_SNAPSHOTS_TO_BE_BACKEDUP=0
ENABLE_NON_PERSISTENT_NFS=0
UNMOUNT_NFS=0
NFS_SERVER=
NFS_VERSION=
NFS_MOUNT=
NFS_LOCAL_NAME=
NFS_VM_BACKUP_DIR=
SNAPSHOT_TIMEOUT=15
EMAIL_ALERT=0
EMAIL_LOG=0
EMAIL_SERVER=
EMAIL_SERVER_PORT=25
EMAIL_DELAY_INTERVAL=1
EMAIL_USER_NAME=
EMAIL_USER_PASSWORD=
EMAIL_TO=
EMAIL_ERRORS_TO=
EMAIL_FROM=
WORKDIR_DEBUG=0
VM_SHUTDOWN_ORDER=
VM_STARTUP_ORDER=

(just an example...)

You may note that there are A LOT of things you can configure here.

The important one here is the first line where it points to your backup folder.

Sadly, vi seems to be the only editor available under ESXi. Somebody remind my to write up a tutorial for using it... For now, look here.

Testing

I'm using a VM called "Manager" as my test...

./ghettoVCB.sh -g ./ghettoVCB.conf -m "Manager" -d dryrun

If you see "info: ###### Final status: OK, only a dryrun. ######", it's OK.

Now you can do a real backup...

./ghettoVCB.sh -g ./ghettoVCB.conf -m "Manager"

When you see: "info: ###### Final status: All VMs backed up OK! ######", congratulations, you have a manual backup.

Restoring a VM

Edit the input file for ghettoVCB-restore

vi vms_to_restore

#"<DIRECTORY or .TGZ>;<DATASTORE_TO_RESTORE_TO>;<DISK_FORMAT_TO_RESTORE>;<OPTIONAL_RESTORED_VM_DISPLAY_NAME>"
# DISK_FORMATS
# 1 = zeroedthick
# 2 = 2gbsparse
# 3 = thin
# 4 = eagerzeroedthick
### To restore the VM back to its original name (DELETE the original first!):
#/vmfs/volumes/Beta/BACKUPS/Manager/Manager-2019-09-08_21-01-00;/vmfs/volumes/Alpha;1
### To duplicate the VM under a new name:
/vmfs/volumes/Beta/BACKUPS/Manager/Manager-2019-09-08_21-01-00;/vmfs/volumes/Alpha;1;FooBar

(just an example...

I'm restoring a backup of "Manager", but renaming it to "FooBar" in the process. This is a potential way to rapidly duplicate preconfigured VMs.)

The "/vmfs/volumes/Beta/BACKUPS/Manager/Manager-2019-09-08_21-01-00" part of the non-commented line is the name of the actual backup being restored.

The "/vmfs/volumes/Alpha" part is the folder containing the original VM. (or, at least, the folder you want to restore to...)

If you are restoring to the original VM, delete the original VM first:

rm -R /vmfs/volumes/Alpha/Manager

(do it in the GUI...)

Restore the VM:

./ghettoVCB-restore.sh -c vms_to_restore

Customization

Set up a cron job

I'll have to properly detail this yet...

Meanwhile: Add cron Job to VMware ESX/ESXi

Here's an ugly example based on how I got it working here...

  • /bin/kill $(cat /var/run/crond.pid)
    • This stops cron
  • vi /var/spool/cron/crontabs/root
    • Add the following at the bottom:
    • 1    2    *   *   *   "/vmfs/volumes/Admin/Utilities/ghettoVCB/ghettoVCB.sh" -m "Manager" -g "/vmfs/volumes/Admin/Utilities/ghettoVCB/ghettoVCB.conf"
  • /usr/lib/vmware/busybox/bin/busybox crond
    • This starts cron back up
  • vi /etc/rc.local.d/local.sh
    • The following lines added BEFORE the exit 0 line tell ESXi to do the modification to crontab at boot time:
    • /bin/kill $(cat /var/run/crond.pid)
    • /bin/echo '1    2    *   *   *   "/vmfs/volumes/Admin/Utilities/ghettoVCB/ghettoVCB.sh" -g "/vmfs/volumes/Admin/Utilities/ghettoVCB/ghettoVCB.conf" -m "Manager"' >> /var/spool/cron/crontabs/root
    • /usr/lib/vmware/busybox/bin/busybox crond
  • /sbin/auto-backup.sh
    • Ensures that the changes to local.sh get written back to the boot thumbdrive.


Simplifying manual backups

NOTE: Need to do some sort of error handling. ATM, if you try to backup a VM that doesn't exist, it fails silently.

cd /vmfs/volumes/Admin/Utilities/ghettoVCB/

vi backup.sh

#!/bin/sh                                                                           
                                                                                   
export PATH=/bin:/sbin

for VMName in "$@"
do
echo "###############################"
echo "#  Backing up "$VMName
echo "###############################"
echo

logger "ghettoVCB/backup.sh is backing up "$VMName"."

/vmfs/volumes/Admin/Utilities/ghettoVCB/ghettoVCB.sh \
   -g /vmfs/volumes/Admin/Utilities/ghettoVCB/ghettoVCB.conf \
   -m $VMName

logger "ghettoVCB/backup.sh is finished backing up "$VMName"."

echo
echo "###############################"
echo "# "$VMName" is backed up"
echo "###############################"
done

logger "ghettoVCB/backup.sh finished"

chmod +x backup.sh

Then you can do fun things like:

./backup.sh Manager WebServer Automation Download

& all the VMs will get backed up.

(Now find out how to put it directly in the execution path...)

Running a backup remotely

Note: You'll need to have SSHed into the server previously from the machine you want to run this command from. (SSH is a strange beast... It REALLY wants to set up stuff on your local machine for verification. Whoda Thunkit)

ssh root@ESXiServer "/vmfs/volumes/Admin/Utilities/ghettoVCB/backup.sh VMname"

Simplifying Clone Deployment

cd /vmfs/volumes/Admin/Utilities/ghettoVCB/

vi clone.sh

#!/bin/sh                                                                

export PATH=/bin:/sbin

for VMName in "$@"
do
echo "###############################"
echo "#  Creating "$VMName
echo "###############################"
echo

logger "ghettoVCB/clone.sh is creating "$VMName" from CloneMe."

echo "/vmfs/volumes/Admin/CloneMe/CloneMe-Mint-19.3/CloneMe-Mint-19.3-0;/vmfs/volumes/Admin;1;"$VMName > CloneIt
/vmfs/volumes/Admin/Utilities/ghettoVCB/ghettoVCB-restore.sh -c CloneIt

logger "ghettoVCB/clone.sh "$VMName" finished"

echo
echo "###############################"
echo "# "$VMName" is created"
echo "###############################"
done

chmod +x clone.sh

Then you can do fun things like:

./clone.sh Manager WebServer Automation Download

& all the VMs will be created as clones of CloneMe.

Thoughts

  • Need to create a CloneMe creator script.
    • Multiple CloneMes would be good...
    • Duplicate ghetoVCB.conf to CloneMe.conf
      • Add in,
        • VM_BACKUP_VOLUME="/vmfs/volumes/Admin/CloneMe"
        • VM_BACKUP_DIR_NAMING_CONVENTION="0"
      • Then:
        • ./ghettoVCB.sh -g ./CloneMe.conf -m CloneMe-Mint-19.3
  • Need a way to tell clone.sh which CloneMe to use...
    • This particular rabbit hole has forced the realisation that sh as implemented in ESXi is a poor substitute for bash.
    • The script to provide a menu of available CloneMes in bash is quite elegant.
    • The script to do the same in ESXis sh is a fugly mess of hard-coded information.
  • &, of course, where to actually put the clone.
    • (see comments just above about shell implementation... :( )

Simplifying Restoration of a Backup

Coming SoonTM...

Tricks with ghettoVCB backups

Cloning preconfigured VMs

Yes, this IS a good way to duplicate preconfigured VMs.

Moving & copying VMs to another physical machine

Duplicate the backup onto the destination machine.

  • On the Source machine:
    • change to the folder containing the backups of interest:
      • cd /vmfs/volumes/Beta/BACKUPS/Manager/
    • Create an archive of the backup set required:
      • tar c -z -f Manager.tgz Manager-2019-09-08_21-01-00
  • On the Destination machine:
    • change to the backups folder:
      • cd /vmfs/volumes/Beta/BACKUPS/
    • Create a destination:
      • mkdir Manager/
  • Move the archive to the backups folder on the destination machine (transport method of your choice...)
    • Enter the destination backup folder:
      • cd Manager
    • Extract the archive:
      • tar x -z -f Manager.tgz
    • Now you can simply restore it as normal.

Still to be done

  • Look into partial backups. i.e.: select specific drives on a per-VM basis

Backing up (or copying/moving) the Backups

This is a Work In Progress

  • Over the network
    • Using the web interface
      1. Sign into the machine with a web browser
      2. Select Storage
      3. Click "Datastore browser"
      4. Navigate to the backups
      5. Download them
    • Using NFS
      • ghettoVCB has this built-in, but you'll need an NFS server available full time.
    • Using SCP
  • Using a hot-swap drive
    • If your server has hot-swap bays, it's a simple matter of copying your backups to a datastore located on one of the hot-swappable drives &, well, hot-swapping it.
  • Using an external drive
    • Careful... This is a good way to mess up your server
      • Start here: USB Devices as VMFS Datastore in vSphere ESXi 6.0
      • An example sequence of commands used here:
        • /etc/init.d/usbarbitrator stop
        • ls /dev/disks/
          • My external drive (a 1TB WD My Passport Ultra)shows up as mpx.vmhba36
        • partedUtil mklabel /dev/disks/mpx.vmhba36\:C0\:T0\:L0 gpt
        • partedUtil setptbl /dev/disks/mpx.vmhba36\:C0\:T0\:L0 gpt "1 2048 1953455804 AA31E02A400F11DB9590000C2911D1B8 0"
        • vmkfstools -C vmfs5 -S VMFS-Backups /dev/disks/mpx.vmhba36\:C0\:T0\:L0:1
        • cp -R /vmfs/volumes/Beta/BACKUPS/* /vmfs/volumes/VMFS-Backups/

Reference Links

Using vi

  • Basic vi Commands (CSU)
  • UNIX: vi Editor (City College of SF)
  • VI Text Editor with Commands: Linux/Unix Tutorial (Guru99)
    • Link removed because Guru99 apparently no longer wants traffic