vmcpdir |
#!/bin/sh
# Version 2.0
#=========================================================================
# This script it designed to copy the contents of a ESXi 5.x
# guest folder from a source to destination without the "Sparse"
# files expanding to their allocated size. ( IE - you are using 40gb of a
# not the full size allocated.)
#=========================================================================
# Original Script: http://RichSchreiber.com/vmcpdir.html
#=========================================================================
# Test for valid input
if [ -n "$1" ] || [ -n "$2" ]; then
SRC="$1"
DST="$2"
OPT="$3"
else
echo "Error: Usage $0 [source] [destination] [options]"
echo "Options:"
echo "/dd - Deletes any files in the destination folder before copy."
echo " "
exit
fi
#Test Paths
if [ ! -e "$SRC" ]; then
echo "Source directory does not exists!"
exit
elif [ ! -e "$DST" ]; then
echo "Destination directory does not exists!"
exit
elif [ ! -d "$SRC" ]; then
echo "Source is not a directory!"
exit
elif [ ! -d "$DST" ]; then
echo "Destination is not a directory!"
exit
else
echo "Source and Destination paths are valid."
echo "$SRC will be coppied to $DST"
echo "Processing..."
fi
# Clean up paths
LEN=$((${#SRC}-1))
if [ "${SRC:$LEN}" == "/" ]; then
SRC=${SRC:0:$LEN}
fi
LEN=$((${#DST}-1))
if [ "${DST:$LEN}" == "/" ]; then
DST=${DST:0:$LEN}
fi
# Delete destination data
if [[ "$OPT" == */dd* ]]; then
rm -rv $DST/*
fi
# Create source file array
for k in $SRC/*; do
# Check to see if VM is running
LEN=$((${#k}-4))
if [ "${k:$LEN,,}" == ".lck" ]; then
echo "Lock file detected..."
echo "Virutal Guest is still running!"
echo "==========================================="
echo "Shutdown VM before running vmcpdir command!"
echo "==========================================="
echo "exiting..."
exit
fi
done
# Check to see if file is a vmdk file
for i in $SRC/*; do
LEN=$((${#i}-5))
SHORTNAME=$(basename "$i")
DELTATEST="${i:0:$LEN}-delta.vmdk"
echo "$i -> $DST/$SHORTNAME"
if [ "${i:$LEN,,}" == ".vmdk" ]; then
LEN=$((${#i}-10))
if [ "${i:$LEN,,}" == "-flat.vmdk" ]; then
echo "Flat vmdk file - skipping"
elif [ "${i:$LEN,,}" == "delta.vmdk" ]; then
echo "Delta file detected - Flat copy in progress..."
cp $i $DST/$SHORTNAME
else
if [ -e "$DELTATEST" ]; then
echo "vmdk file linked to a delta file detected - Flat copy in progress..."
cp $i $DST/$SHORTNAME
else
echo "Standard vmdk file found - Sparse file copy inprogress..."
vmkfstools -i "$i" -d thin "$DST/$SHORTNAME"
fi
fi
else
cp $i $DST/$SHORTNAME
fi
done
exit
#vmkfstools -i "$1" -d thin "$2"