The original created backups to the same datastore then moved them to a nfs share.
I only wanted to copy vms to the datastore that is on my openfiler server. I can perform backups to tape from there.
Code: Select all
#!/bin/bash
#
# Author: KPC
# Modified: kchawk
#
# Description: Simple BASH script that does the following:
# This has been modified from KPC's script
# Searches remote ESXi servers for list of VM's
# Supports multiple datastores
# Snapshots running VM's
# Exports using vmkfstools directly to another DS
# Creates daily directory on target DS
# Removes snapshots of running VM's
# Outputs result to file and sends an email
#
# Restrictions: Will not backup VM's with existing snapshots
# Will not backup VM's with spaces in filename
#
# Requirements: Must have SSH authentication keys setup between
# server running this script and the ESXi host.
# vmdk name must be same as folder
#
# SSH Authorization Setup
# Run 'ssh-keygen -t rsa' on RemoteCLI server
# Copy /root/.ssh/id_rsa.pub to /.ssh on ESXi server
# Rename id_rsa.pub to authorized_keys
# On the ESXi server copy /.ssh to datastore1
# Add following line to /etc/rc.local: cp /vmfs/volumes/datastore1/.ssh/ /.ssh -R
#
# Restore: Copy backup directory files to ESXi datastore
# Browse datastore, Add the vmx file to Inventory
# Leave VM name blank
# Start the VM
#####################################################################################################
# Environment variables
HOSTNAME=host # Hostname or IP address of your ESXi server
USERNAME=root # Username that has permission to create snapshots
PWD=pass # Password of user
LOG_DIR=/var/log/ # Location of log directory
DATE=`date +%m%d%y` # Date format for log filename
LOG=/var/log/vm_$DATE.log # Name of log file
MAILTO=email@email.com # Email of user(s) to recieve email report (use comma to seperate addresses)
DESTDS=/vmfs/volumes/Test # Destination DataStore
# Delete log file older than 7 days
find $LOG_DIR*.log -type f -mtime +5 -print0 | xargs -0 /bin/rm -f
echo "------------------------------------------------------------------------------------" | tee -a $LOG
echo "ESXi Daily Backup [""$HOSTNAME""] - `date`" | tee -a $LOG
echo "------------------------------------------------------------------------------------" | tee -a $LOG
for i in $( vmware-cmd -H $HOSTNAME -U $USERNAME -P $PWD -l ); do
VM_DIR=$(dirname "$i")
VM_NAME=$(basename "$VM_DIR")
echo "["$(basename "$VM_DIR")"]" | tee -a $LOG
# Run the backup only if the VM has no existing snapshots
if [ "$(vmware-cmd -H $HOSTNAME -U $USERNAME -P $PWD "$i" hassnapshot)" == "hassnapshot () = 1" ]; then
echo "Snapshot already exists, $VM_NAME backup will be excluded" | tee -a $LOG
echo "------------------------------------------------------------------------------------" | tee -a $LOG
else
# Create daily folder on Destination DataStore
echo "Creating daily folder on Destination DataStore" | tee -a $LOG
ssh $HOSTNAME "mkdir -p $DESTDS/$HOSTNAME/$DATE/$VM_NAME"
# Copy vmx file before snapshot is taken
echo "Copying VMX file at `date`" | tee -a $LOG
ssh $HOSTNAME "cp $VM_DIR/*.vmx $DESTDS/$HOSTNAME/$DATE/$VM_NAME"
# Snapshot the VM
echo "Creating snapshot at `date`" | tee -a $LOG
[ "$(vmware-cmd -H $HOSTNAME -U $USERNAME -P $PWD "$i" createsnapshot vmbak vmbak 0 0)" ]
# Clone the VM
echo "Cloning at `date`" | tee -a $LOG
ssh $HOSTNAME "vmkfstools -i $VM_DIR/$VM_NAME.vmdk $DESTDS/$HOSTNAME/$DATE/$VM_NAME/$VM_NAME.vmdk"
# Remove snapshots if any were taken
echo "Removing snapshots at `date`"
[ "$(vmware-cmd -H $HOSTNAME -U $USERNAME -P $PWD "$i" removesnapshots)" ]
echo "Completed at `date`" | tee -a $LOG
echo "------------------------------------------------------------------------------------" | tee -a $LOG
fi
done
echo "------------------------------------------------------------------------------------" | tee -a $LOG
echo "For more info see "$LOG"" | tee -a $LOG
# Statement to mail log file to appropriate users
mail -s "ESXi Backup Report for $HOSTNAME" $MAILTO < $LOG
exit