ghettoHostBackupManagement.pl

    Table of Contents

    • Author
    • Description
    • Category
    • Features
    • Requirements
    • Version Support
    • Configurations
    • Sample Execution
    • Sample Output
    • Restore Backup

     

     

    Author

    William Lam

    Description

    This script allows you to managed and automate/archive ESXi host backups  which can be scheduled and configured with a specific rotation running  on VMware vMA. Along with with logging the backups, there's an  additional html report which is generated and could be sent via email  upon a backup completion. The idea for this script actually came from  the well known www.vm-help.com owner Dave Mishchenko,  the idea being that you could perodically take snapshots of your ESXi  host configuration and revert back to specific configuration or used as a  simple DR plan.

    The script relies on the ESXi host being managed by VMware vMA along  with the use of vi-fastpass. Ensure that all hosts that you would like  to back up is being managed by vMA. For more informationon how to  configure vi-fastpass for your ESXi host, please take a look here: http://engr.ucsb.edu/~duonglt/vmware/#vmware_vima.

    Category

    • Initial Server Set Up / Miscellaneous

     

    Features

    • Email backup reports

     

    Requirements

     

    Version Support

    • Supports ESXi (licensed version only)

     

    Usage

    [vi-admin@scofield skunkworks]$ ./ghettoHostBackupManagement.pl --help
    
    Synopsis: ./ghettoHostBackupManagement.pl OPTIONS
    
    
    Command-specific options:
       --backup_dir (default '/home/vi-admin/ghettoHostBackups')
          Path to backup directory in which ESXi backups will be stored in vMA
       --loglevel (default 'debug')
          Log level [info|debug]
       --logoutput (default '/tmp/ghettoHostBackup.log')
          UPS Log output
       --report (default '/tmp/ghettoHostBackupReport.html')
          Backup Report
       --rotation (default '5')
          Number of backups to keep
    
    Common VI options:
       --config (variable VI_CONFIG)
          Location of the VI Perl configuration file
       --credstore (variable VI_CREDSTORE)
          Name of the credential store file defaults to <HOME>/.vmware/credstore/vicredentials.xml on Linux and <APPDATA>/VMware/credstore/vicredentials.xml on Windows
       --encoding (variable VI_ENCODING, default 'utf8')
          Encoding: utf8, cp936 (Simplified Chinese), iso-8859-1 (German), shiftjis (Japanese)
       --help
          Display usage information for the script
       --passthroughauth (variable VI_PASSTHROUGHAUTH)
          Attempt to use pass-through authentication
       --passthroughauthpackage (variable VI_PASSTHROUGHAUTHPACKAGE, default 'Negotiate')
          Pass-through authentication negotiation package
       --password (variable VI_PASSWORD)
          Password
       --portnumber (variable VI_PORTNUMBER)
          Port used to connect to server
       --protocol (variable VI_PROTOCOL, default 'https')
          Protocol used to connect to server
       --savesessionfile (variable VI_SAVESESSIONFILE)
          File to save session ID/cookie to utilize
       --server (variable VI_SERVER, default 'localhost')
          VI server to connect to. Required if url is not present
       --servicepath (variable VI_SERVICEPATH, default '/sdk/webService')
          Service path used to connect to server
       --sessionfile (variable VI_SESSIONFILE)
          File containing session ID/cookie to utilize
       --url (variable VI_URL)
          VI SDK URL to connect to. Required if server is not present
       --username (variable VI_USERNAME)
          Username
       --verbose (variable VI_VERBOSE)
          Display additional debugging information
       --version
          Display version information for the script
    
    

     

     

    Configuration

    Exclude certain hosts from backup

    If there are certain ESXi hosts that you would like to exclude from the  backup, there is a variable in which define within the script:

    my @exclude_hosts = (
    "esxi4-3.primp-industries.com"
    "esxi4-4.primp-industries.com"
    );
    
    

     

     

    Email Report

    The script has the capablity to email an HTML report on the results of  the backup upon completion, you'll need to fill out the following  variables within the script:

    #################
    # EMAIL CONF
    #################
    
    my $SEND_MAIL = "no";
    my $EMAIL_HOST = "emailserver";
    my $EMAIL_DOMAIN = "localhost.localdomain";
    my $EMAIL_TO = 'William Lam <william@primp-industries.com.com>';
    my $EMAIL_FROM = 'ghettoVCBg2 <ghettoVCBg2@primp-industries.com.com>';
    
    

     


    1. Download and upload ghettoHostBackupManagement.pl to your vMA 4.0 host

    2. Set the script have execute permission:

    [vi-admin@scofield skunkworks]$ chmod +x ghettoHostBackupManagement.pl
    

     

     

    Sample Execution

    Manual Execution

    Execute the script (default rotation is set to 5):

    [vi-admin@scofield skunkworks]$ ./ghettoHostBackupManagement.pl
    Generating Ghetto ESXi Host Backup Report "/tmp/ghettoHostBackupReport.html" ...
    
    

     

    This will generate a log file stored by default in /tmp/ghettoHostBackupReport.log and HTML report stored by default in /tmp/ghettoHostBackupReport.html, both of which can be configured as commandline param using --logoutput and --report.

    The backups will be stored under /home/vi-admin/ghettoHostBackups and can be changed by specifying the commandline param --backup_dir

    Here is an example of a directory structure for the backups:

    [vi-admin@scofield ~]$ tree ghettoHostBackups/
    ghettoHostBackups/
    |-- esxi35u4.primp-industries.com
    |   |-- esxi35u4.primp-industries.com-2010-01-12--1
    |   |   `-- esxi35u4.primp-industries.com.tgz
    |   |-- esxi35u4.primp-industries.com-2010-01-12--2
    |   |   `-- esxi35u4.primp-industries.com.tgz
    |   |-- esxi35u4.primp-industries.com-2010-01-12--3
    |   |   `-- esxi35u4.primp-industries.com.tgz
    |   |-- esxi35u4.primp-industries.com-2010-01-12--4
    |   |   `-- esxi35u4.primp-industries.com.tgz
    |   `-- esxi35u4.primp-industries.com-2010-01-12--5
    |       `-- esxi35u4.primp-industries.com.tgz
    |-- esxi4-1.primp-industries.com
    |   |-- esxi4-1.primp-industries.com-2010-01-12--1
    |   |   `-- esxi4-1.primp-industries.com.tgz
    |   |-- esxi4-1.primp-industries.com-2010-01-12--2
    |   |   `-- esxi4-1.primp-industries.com.tgz
    |   |-- esxi4-1.primp-industries.com-2010-01-12--3
    |   |   `-- esxi4-1.primp-industries.com.tgz
    |   |-- esxi4-1.primp-industries.com-2010-01-12--4
    |   |   `-- esxi4-1.primp-industries.com.tgz
    |   `-- esxi4-1.primp-industries.com-2010-01-12--5
    |       `-- esxi4-1.primp-industries.com.tgz
    `-- esxi4-2.primp-industries.com
        |-- esxi4-2.primp-industries.com-2010-01-12--1
        |   `-- esxi4-2.primp-industries.com.tgz
        |-- esxi4-2.primp-industries.com-2010-01-12--2
        |   `-- esxi4-2.primp-industries.com.tgz
        |-- esxi4-2.primp-industries.com-2010-01-12--3
        |   `-- esxi4-2.primp-industries.com.tgz
        |-- esxi4-2.primp-industries.com-2010-01-12--4
        |   `-- esxi4-2.primp-industries.com.tgz
        `-- esxi4-2.primp-industries.com-2010-01-12--5
            `-- esxi4-2.primp-industries.com.tgz
    

     

    Here is an example out of the log file:

    [vi-admin@scofield skunkworks]$ cat /tmp/ghettoHostBackup.log
            01-12-2010 20:24:05 --  info: Found host: "esxi35u4.primp-industries.com"
            01-12-2010 20:24:05 --  debug: Main: Login by vi-fastpass to: esxi35u4.primp-industries.com
            01-12-2010 20:24:09 --  info: Backing up ESXi conf to /home/vi-admin/ghettoHostBackups/esxi35u4.primp-industries.com/esxi35u4.primp-industries.com-2010-01-12/esxi35u4.primp-industries.com.tgz
            01-12-2010 20:24:09 --  info: Checking rotation ...
            01-12-2010 20:24:09 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi35u4.primp-industries.com/esxi35u4.primp-industries.com-2010-01-12--9 due to rotation max
            01-12-2010 20:24:09 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi35u4.primp-industries.com/esxi35u4.primp-industries.com-2010-01-12--8 due to rotation max
            01-12-2010 20:24:09 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi35u4.primp-industries.com/esxi35u4.primp-industries.com-2010-01-12--7 due to rotation max
            01-12-2010 20:24:09 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi35u4.primp-industries.com/esxi35u4.primp-industries.com-2010-01-12--6 due to rotation max
            01-12-2010 20:24:09 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi35u4.primp-industries.com/esxi35u4.primp-industries.com-2010-01-12--5 due to rotation max
            01-12-2010 20:24:09 --  debug: Main: Disconnect from: esxi35u4.primp-industries.com
    
            01-12-2010 20:24:09 --  info: Found host: "esxi4-1.primp-industries.com"
            01-12-2010 20:24:09 --  debug: Main: Login by vi-fastpass to: esxi4-1.primp-industries.com
            01-12-2010 20:24:12 --  info: Backing up ESXi conf to /home/vi-admin/ghettoHostBackups/esxi4-1.primp-industries.com/esxi4-1.primp-industries.com-2010-01-12/esxi4-1.primp-industries.com.tgz
            01-12-2010 20:24:12 --  info: Checking rotation ...
            01-12-2010 20:24:12 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-1.primp-industries.com/esxi4-1.primp-industries.com-2010-01-12--9 due to rotation max
            01-12-2010 20:24:12 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-1.primp-industries.com/esxi4-1.primp-industries.com-2010-01-12--8 due to rotation max
            01-12-2010 20:24:12 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-1.primp-industries.com/esxi4-1.primp-industries.com-2010-01-12--7 due to rotation max
            01-12-2010 20:24:12 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-1.primp-industries.com/esxi4-1.primp-industries.com-2010-01-12--6 due to rotation max
            01-12-2010 20:24:12 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-1.primp-industries.com/esxi4-1.primp-industries.com-2010-01-12--5 due to rotation max
            01-12-2010 20:24:12 --  debug: Main: Disconnect from: esxi4-1.primp-industries.com
    
            01-12-2010 20:24:12 --  info: Found host: "esxi4-2.primp-industries.com"
            01-12-2010 20:24:12 --  debug: Main: Login by vi-fastpass to: esxi4-2.primp-industries.com
            01-12-2010 20:24:15 --  info: Backing up ESXi conf to /home/vi-admin/ghettoHostBackups/esxi4-2.primp-industries.com/esxi4-2.primp-industries.com-2010-01-12/esxi4-2.primp-industries.com.tgz
            01-12-2010 20:24:15 --  info: Checking rotation ...
            01-12-2010 20:24:15 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-2.primp-industries.com/esxi4-2.primp-industries.com-2010-01-12--9 due to rotation max
            01-12-2010 20:24:15 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-2.primp-industries.com/esxi4-2.primp-industries.com-2010-01-12--8 due to rotation max
            01-12-2010 20:24:15 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-2.primp-industries.com/esxi4-2.primp-industries.com-2010-01-12--7 due to rotation max
            01-12-2010 20:24:15 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-2.primp-industries.com/esxi4-2.primp-industries.com-2010-01-12--6 due to rotation max
            01-12-2010 20:24:15 --  info: Purging /home/vi-admin/ghettoHostBackups/esxi4-2.primp-industries.com/esxi4-2.primp-industries.com-2010-01-12--5 due to rotation max
            01-12-2010 20:24:15 --  debug: Main: Disconnect from: esxi4-2.primp-industries.com
    

     

    Here is an example of the HTML report:
    esxi_backup.png

    Automated backup scheduled via cron

    1. Create a cron entry (Do a search online for "cron" if you have further questions)

    [vi-admin@scofield ~]$ sudo cat /etc/crontab
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    HOME=/
    
    LD_PRELOAD=/opt/vmware/vima/lib64/libvircli.so:
    LD_LIBRARY_PATH=/opt/vmware/vima/lib64:
    
    # run-parts
    01 * * * * root run-parts /etc/cron.hourly
    02 4 * * * root run-parts /etc/cron.daily
    22 4 * * 0 root run-parts /etc/cron.weekly
    42 4 1 * * root run-parts /etc/cron.monthly
    0 * * * * root /home/vi-admin/skunkworks/ghettoHostBackupManagement.pl --rotation 24
    
    

     

    Note: You'll need to add two additional variables in the crontab:

    LD_PRELOAD=/opt/vmware/vima/lib64/libvircli.so:
    LD_LIBRARY_PATH=/opt/vmware/vima/lib64:
    

     

    In this example, I'm backing up the ESXi host configurations every hour  and rotating after 24 backups. Once you've saved and exited /etc/crontab  file, just sit back and let the backups take place.

     

    Restore Backup

    When you want to restore an ESXi host from a specific backup, you'll  want to ensure there are no running VMs and put and change into the  specific directory that has the backup file on your vMA host that you  would like to restore from.

    [vi-admin@scofield esxi35u4.primp-industries.com-2010-01-12--1]$ pwd
    /home/vi-admin/ghettoHostBackups/esxi35u4.primp-industries.com/esxi35u4.primp-industries.com-2010-01-12--1
    
    [vi-admin@scofield esxi35u4.primp-industries.com-2010-01-12--1]$ ls
    esxi35u4.primp-industries.com.tgz
    

     

    Let's say we would like to restore esxi35u4.primp-industries.com.tgz back to ESXi host:

    [vi-admin@scofield esxi35u4.primp-industries.com-2010-01-12--1]$ esxcfg-cfgbackup --server esxi35u4.primp-industries.com --username root -l esxi35u4.primp-industries.com.tgz
    
    Enter password:
    The restore operation will reboot the host.
    Type 'yes' to continue:
    yes
    Uploading config bundle to configBundle.tgz ...
    Performing restore ...
    
    

     

    The host will now reboot and once it finishes loading, you're done with the restore!