Monthly Archives: November 2011

Simple backup script for VMware ESX VMs

– UPDATE 2013-02-04 –
This is a simple backup script for ESX that I use for backing up my virtual machines on my stand alone ESX server at home.
For my data disks I use traditional backup in the OS so I only backup the vmdks containing the OS and exclude the data vmdks where all my big files are located, this is done by touching a .exclude file in the same directory, example “VMName_1-flat.vmdk.exclude”.
The backup can be performed while the VMs are turned on since a snapshot is taken first, releasing the lock on the VMDK file. And when the backup is completed, the snapshot is removed. This means that the script needs to handle the possibility of failed snapshots.
For me, a successful backup is more important than a little downtime on my VMs so I turn of the VM if that happens.

Note: script only tested on ESXi 5.1

#!/bin/sh

# Add cron job to this file: /var/spool/cron/crontabs/root
# 10   0    *   *   *   /vmfs/volumes/datastore1/scripts/backup-vms.sh >/vmfs/volumes/datastore1/scripts/backup-vms.log 2>&1
# Check crond pross id
# cat /var/run/crond.pid
# Kill old crond
# /bin/kill $(cat /var/run/crond.pid)
#  Restart cron jobs (for 5.1)
# /usr/lib/vmware/busybox/bin/busybox crond
# Check again crond pross id (Should not be above id)
# cat /var/run/crond.pid

today=$(date "+%A")
basedir=/vmfs/volumes/datastore1
destdir=/vmfs/volumes/datastore3/backup/$today
VMs="Solaris Tellus-ng Venus Mercury-ng"
#VMs="Test"

if [ ! -d "$destdir" ]; then
  mkdir -p $destdir
fi

for vm in $VMs; do
  echo "Backing up $vm ..."
  VMdir=$basedir/$vm
  if [ -d "$VMdir" ]; then
    vmid=$(vim-cmd vmsvc/getallvms | grep $vm | awk '{ print $1 }')
    if [ "$vmid" -gt "0" ]; then
      echo "Creating a snapshot of the VM ..."
      vim-cmd vmsvc/snapshot.create $vmid backup-snap "Snapshot for backup script" 0 1
      if [ "$?" -ne "0" ]; then
        echo "Failed to create snapshot, shutting it down instead ..."
        vim-cmd vmsvc/power.shutdown $vmid
        echo "Sleeping 60 seconds to wait for VM shutdown ..."
        sleep 60
        echo "Checking if VM state is powered off ..."
        vim-cmd vmsvc/power.getstate $vmid | grep "Powered off"
        if [ "$?" -ne "0" ]; then
          echo "VM is still not powered off , you´re going DOWN bitch!..."
          vim-cmd vmsvc/power.off $vmid
          sleep 5
        fi
      fi
      echo "Starting backup of VM files ..."
      VMdestdir="$destdir/$vm"
      if [ ! -d "$VMdestdir" ]; then
        mkdir $VMdestdir
      else
        rm -rf $VMdestdir/*
      fi
      IFS="
"
      for file in $(ls -1 $VMdir | egrep -v "flat|exclude|delta|vswp"); do
        fext=${file#*.}
        if [ -f "$VMdir/$file.exclude" ]; then
          echo "Excluding $file ..."
        else
          if [ "$fext" == "vmdk" ]; then
            grep parentFileNameHint $VMdir/$file >/dev/null
            if [ "$?" -eq "0" ]; then
              echo "Vmdk is snapshot file , ignoring ..."
            else
              echo "Copying $fext file $file using vmkfstools ..."
              vmkfstools -d thin -i $VMdir/$file $VMdestdir/$file
            fi
          else
            echo "Copying $fext file $file ..."
            cp $VMdir/$file $VMdestdir/
          fi
        fi
      done
      echo "Backup completed ..."
      vim-cmd vmsvc/power.getstate $vmid | grep "Powered off"
      if [ "$?" -eq "0" ]; then
        vim-cmd vmsvc/power.on $vmid
      else
        vim-cmd vmsvc/snapshot.removeall $vmid
        echo "Copying vmx file again (since changed after snap removal) ..."
        cp $VMdir/*.vmx $VMdestdir/
      fi
    else
      echo "vmid is not numeric."
    fi
  else
    echo "VMdir $VMdir does not exist"
  fi
done

Video encoding for different purposes

This is a post for some ffmpeg/mencoder commands for encoding/extracting video/audio etc…

Utilities used:
ffmpeg: http://www.videohelp.com/tools/ffmpeg
mencoder: http://www.videohelp.com/tools/MPlayer

# Encoding to AVI/XVID (Also playable by CorePlayer for Windows Mobile)
# br=92 means audio bitrate is 92 kb/s
# -xy 800 means resolution width is 800 pixels
# fixed_quant=5 is quality factor
mencoder.exe “Movie.mkv” -oac mp3lame -lameopts abr:br=92 -ovc xvid -vf scale -zoom -xy 800 -xvidencopts fixed_quant=5 -o “Movie.avi”

# Same as above but with subtitles
# Try these two settings to set subtitles layout:
# -subfont-text-scale 3.3
# -subpos 96
mencoder.exe “Movie.mkv” -oac mp3lame -lameopts abr:br=92 -ovc xvid -vf scale -zoom -xy 800 -xvidencopts fixed_quant=5 -o “Movie.avi” -sub Movie.srt

# Encoding to mpeg4/aac – Playable on many phones like Sony Ericsson, Samsung, Windows Mobile 5 and higher, etc …
ffmpeg.exe -i “Movie.avi” -acodec libfaac -b 320000 -r 25 -s 320×244 -vcodec mpeg4 -ab 96000 “Movie.mp4″

# Encoding to mpeg4/arm/3gp – Playable on older Sony Ericsson phones, etc
ffmpeg.exe -y -i “Movie.avi” -s qcif -vcodec mpeg4 -acodec libopencore_amrnb -ac 1 -ar 8000 -r 25 -ab 12200 -s 220×176 -b 192000 “Kung Fu Panda.3gp”

# Extract AC3 and re-encode audio from a movie.
# To extract second audio stream, use: -map 0:2
ffmpeg.exe -i “Movie.mkv” -vn -acodec ac3 -ac 6 -ab 320000 “Movie-Audio.ac3″